This blog post summarizes my thoughts after reading one of David Brady's blog posts. It gives me the opportunity to talk about something that bothers me often during peer reviews: the dogmatic lack of parenthesis.
His argument is based on 2 points:
- Parentheses make code readable only because programmers are used to seeing parentheses in other languages.
- Avoiding parentheses forces you to think better of the code so that it is more readable.
Readability is not solely a question of habit. The eye goes faster when it has visual cues (colors, symbols, alignment, etc.). Parentheses make it easier to distinguish the subparts of a piece of code (which attributes go to which method, etc.). It is not a question of habit, it has to do with how the human brain works better when it is given cues (not the meaning but the shaping). We can push the line a bit and say that we skip a line between two paragraphs and that we put a capital letter at the beginning of a sentence for the same reasons: to help reading, to allow the eye/brain to scan the text at a glance and identify its parts. The parentheses in the code serve the same purpose.
When the author of the blog post says he doesn't use parentheses for his code to quickly get gross so that he feels obliged to edit it, this makes me cringe. If you want to write readable code, you don't need an external constraint; you make your code work, then you edit it:
- Did I name things correctly?
- Are the different parts clearly identifiable?
- Is there a syntactic sugar that would make the intention of some instruction more explicit?
- Is there some cumbersome instruction that would benefit from being split into multiple lines?
- Is there a complexity somewhere that would benefit from being extracted into a separate method/class so that the main method says what it does and not how it does it?
In short, the developer who wants to write clean code doesn't need a coding style that makes their code disgusting just to force themselves to edit it. Intrinsic motivation vs. extrinsic motivation.
This, however, is to be differentiated from having a common coding style for a dev team. A common coding style is important because there is not one single right way to write code, and the lack of consistency in a codebase is just as brain-consuming as the absence of parentheses. Reducing the mental load of the reader/reviewer is almost as important as the optimization of the code (reducing the mental load of the server).
In short, putting parentheses absolutely everywhere or never putting any are two approaches that are just as extreme and therefore as stupid. Parentheses are a tool that should be used when it is beneficial and should be left aside when it unnecessarily weighs things down. And that, of course, is an eternal assessment that is part of the job of being a developer. It would be easier to just decide I'll always do X so I don't have to bother
thinking with this question anymore, but dogmatic positions are rarely good.
As always in IT (and not only IT): it depends.
Thank you for reading!