Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readability improvement for SchedulerMinHeap. #20557

Closed

Conversation

andysongs
Copy link

Refactor the implementation of min-heap to make the logic clearer.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 7, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f9cc0ba:

Sandbox Source
React Configuration

@sizebot
Copy link

sizebot commented Jan 7, 2021

No significant bundle size changes to report.

Size changes (experimental)

Generated by 🚫 dangerJS against f9cc0ba

@sizebot
Copy link

sizebot commented Jan 7, 2021

No significant bundle size changes to report.

Size changes (stable)

Generated by 🚫 dangerJS against f9cc0ba

Copy link

@rpiosi rpiosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion this proposal introduces unsafe code and unnecessary abstractions.

const first = heap[0];
return first === undefined ? null : first;
if (heap.length === 0) return null;
return heap[0];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As previous code states, heap is possibly undefined and your code tries to read length on it what may lead to a runtime error.

Copy link
Author

@andysongs andysongs Jan 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems no usage that passes a undefined to it. If there is, it seems that the line of code const first = heap[0]; will lead to an error too.

Copy link

@rpiosi rpiosi Jan 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's true and I was thinking about something else in fact. Your code will fail if the first element of heap is undefined. Remember, arrays can have empty elements. So you could write something like
if (typeof heap[0] === 'undefined') return null;.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right but we operate the heap by pop and push, what you worry about will not happen.

let minIndex = index;
if (rightIndex < length) {
// As a heap is a complete binary tree, node with right child must have the left one.
minIndex = smaller(heap, minIndex, smaller(heap, leftIndex, rightIndex));
Copy link

@rpiosi rpiosi Jan 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me adding more abstraction here isn't necessarily good for readability, often it is much better to read bare logic even if it takes a few more lines.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me here are too many statements for describing How to do. Maybe to describe What to do is better?

@gaearon
Copy link
Collaborator

gaearon commented Jan 7, 2021

Thanks for the PR, but we generally don't take PRs that make things "cleaner". They very rarely help and usually tend to introduce subtle bugs. In general, we prefer verbose low-level code that's inlined instead of outlined helpers even when they're more legible.

@gaearon gaearon closed this Jan 7, 2021
@andysongs
Copy link
Author

@gaearon Got it. Thanks for your explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants