Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upstandard rejects a common use case for the `while` loop #324
Comments
This comment has been minimized.
This comment has been minimized.
|
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))) {
...
} |
This comment has been minimized.
This comment has been minimized.
|
Doesn't make it any less confusing IMHO. I guess the following is an alternative anyway: while (true) {
value = getValue(something)
if (!value) break
} |
This comment has been minimized.
This comment has been minimized.
|
@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
closed this
Nov 12, 2015
This comment has been minimized.
This comment has been minimized.
|
well we have two suggested alternative styles but we need one-to-rule-them-all, so let's go with @LinusU 's approach |
This comment has been minimized.
This comment has been minimized.
|
@75lb well, you can't rule the latter out, as it is a valid construct in many different situations. 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. |
This comment has been minimized.
This comment has been minimized.
|
Yeah, both approaches are perfectly fine :) |
This comment has been minimized.
This comment has been minimized.
|
i just realized the double-parens rule is not documented... we should fix that |
This comment has been minimized.
This comment has been minimized.
|
it might be helpful to point out that this is common in if (m = text.match(/...*?/)) {
// ...
} else if (m = text.match(/...*?/)) {
// ...
} else {
// ...
} |
This comment has been minimized.
This comment has been minimized.
|
@rstacruz Yep, agree. Do you want to send a PR? |
75lb commentedNov 11, 2015
this is one of the most common use cases for the
whileloop:unfortunately, standard rejects it (
Expected a conditional expression and instead saw an assignment.) Is there any particular reason?