-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Discretional use of parenthesis nullifies omitting unnecessary parenthesis #278
Comments
A lot of the places I've see them be helpful is clarifying where an equality check is being used as a single boolean: let thing = thingA == thingB
// vs.
let thing = (thingA == thingB) Order of operations there makes it clear that This is also helpful with compound statements, though sometimes they're necessary to clarify order of operations as well: if thingC && (thingD || thingE) { ... Basically, it's mostly about scannability: if it takes you even a second longer to parse as you read through it, that gets old real quick. And if it takes junior developers way longer to reason about what's happening, it can open the door to disaster. |
I agree with @designatednerd. Personally, I also prefer using parentheses around expressions. I'm also a minimalist but I believe that code should be readable and clear for people; otherwise, the compiler doesn't really care. The way I look at it is that I expect to see a simple assignment after let thing = ( /* some short or long logic here, skip it */ ) ? foo : bar |
@samkim102 if I read your question correctly, you're not asking about the cases that the other comments addressed but, instead, are asking about the use of parens around the condition of an If that's the case, please help me understand how to more clearly state that parens should not be used in that case. The current wording of the guide seems unequivocal to me. |
Observation: The rule could be changed to say to use parenthesis only when it promotes clarity. I believe the original guideline was put there to prevent: // go for a Sunday drive
if (day.isSunday) {
vehicle.drive()
} On the other hand, if you are eliding parenthesis because you know the precedence of the other operators involved, you are probably being too clever for your own good. |
The problem, as I understand it, is that some folks seem to think parens are required for clarity in all cases. Clearly, that's not true in the vast majority of cases in If I don't hear more from the original author in the next week, I'm going to close this for lack of activity. |
Closed for lack of response. |
We recently decided to adopt this style guide, but the following statement caused us some headache:
To be precise, larger expression is very subjective and a developer who is more accustomed to having parenthesis around conditional statements will insist the use of parenthesis will enhance readability. As a minimalist, I would argue against any use of parenthesis if it does not alter the meaning of the conditional statement.
Any thoughts?
The text was updated successfully, but these errors were encountered: