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

dequeue and dequeueEnd empty queue behavior #1

Closed
jgmcelwain opened this issue Jan 4, 2022 · 0 comments
Closed

dequeue and dequeueEnd empty queue behavior #1

jgmcelwain opened this issue Jan 4, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@jgmcelwain
Copy link
Owner

jgmcelwain commented Jan 4, 2022

Currently both dequeue and dequeueEnd methods return null if the queue is empty when they're called. This is a fairly unobtrusive implementation but does have two fairly significant drawbacks:

  • Instances will indefinitely return null whilst their queue is empty. This means that users must manually check to see if their latest dequeue/dequeueEnd call emptied the queue.
  • If the user adds null to the queue they have no way of knowing if the null that dequeue/dequeueEnd returns is an inserted value or the "queue is empty" value. Again, they must manually check if the queue is empty instead.

Proposal

A potential solution is to instead throw an error when dequeue/dequeueEnd are called when the queue is empty. This solves both above issues. The instance will inform the user when the queue is empty, since they will have to deal with an error, and null values returned can safely be assumed to be values in the queue.

const queue = new Qewe();

try {
  const value = queue.dequeue();
  // do something with value
} catch {
  // queue is empty, do something else
}

Drawbacks

This behavior change does come with a little overhead. Users would have to handle errors, likely with a try/catch block, or their application would crash.

Alternative

An alternative is to suggest users check if the queue is empty before they call dequeue/dequeueEnd.

const queue = new Qewe();

if (queue.isEmpty === false) {
  const value = queue.dequeue();
  // do something with value
} else {
  // queue is empty, do something else
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant