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

standard rejects a common use case for the `while` loop #324

Closed
75lb opened this issue Nov 11, 2015 · 9 comments

Comments

@75lb
Copy link

commented Nov 11, 2015

this is one of the most common use cases for the while loop:

  while (value = getValue(something)) {
    ...
  }

unfortunately, standard rejects it (Expected a conditional expression and instead saw an assignment.) Is there any particular reason?

@LinusU

This comment has been minimized.

Copy link
Member

commented Nov 11, 2015

Wrap it in double parenthesis to explicitly say that you want assignment. This is a great rule since it catches errors where you've missed an equal sign.

while ((value = getValue(something))) {
  ...
}
@dcousens

This comment has been minimized.

Copy link
Member

commented Nov 11, 2015

Doesn't make it any less confusing IMHO. I guess the following is an alternative anyway:

while (true) {
  value = getValue(something)
  if (!value) break
}
@feross

This comment has been minimized.

Copy link
Member

commented Nov 12, 2015

@dcousens I've seen the double-paren frequently used as a way to indicate you meant assignment not an equality test. But I agree that this code is generally best avoided. Sometimes though, it's the cleanest way to solve a problem.

@feross feross closed this Nov 12, 2015

@75lb

This comment has been minimized.

Copy link
Author

commented Nov 12, 2015

well we have two suggested alternative styles but we need one-to-rule-them-all, so let's go with @LinusU 's approach 👍

@dcousens

This comment has been minimized.

Copy link
Member

commented Nov 13, 2015

@75lb well, you can't rule the latter out, as it is a valid construct in many different situations.
Compare that to the approach by @LinusU, which is only useful for this (IMHO).

I'm OK with either, just pointing out that if a choice was to be made, you could only meaningfully rule out the approach by @LinusU.

@feross

This comment has been minimized.

Copy link
Member

commented Nov 13, 2015

Yeah, both approaches are perfectly fine :)

@rstacruz

This comment has been minimized.

Copy link
Member

commented Nov 14, 2015

i just realized the double-parens rule is not documented... we should fix that

@rstacruz

This comment has been minimized.

Copy link
Member

commented Nov 14, 2015

it might be helpful to point out that this is common in if conditions too:

if (m = text.match(/...*?/)) {
  // ...
} else if (m = text.match(/...*?/)) {
  // ...
} else {
  // ...
}
@feross

This comment has been minimized.

Copy link
Member

commented Nov 14, 2015

@rstacruz Yep, agree. Do you want to send a PR?

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
5 participants
You can’t perform that action at this time.