Parentheses are free of charge. Aren't they?
TL;DR: Use as few parentheses as possible.
Problems
Readability
Syntactic complexity
Solutions
- Remove all not necessary parentheses
Context
We read code from left to right (at least in western culture).
Parentheses often break this flow, adding cognitive complexity
Sample Code
Wrong
schwarzschild = ((((2 * GRAVITATION_CONSTANT)) * mass) / ((LIGHT_SPEED ** 2)))
Right
schwarzschild = (2 * GRAVITATION_CONSTANT * mass) / (LIGHT_SPEED ** 2)
Detection
[X] Automatic
This is a fully automated code smell.
It is based on syntax trees.
Many tools detect it.
Exceptions
On some complex formulas, we can add extra parenthesis for terms readability.
Tags
Readability
Bloaters
Relations
Code Smell 02 - Constants and Magic Numbers
Maxi Contieri ・ Oct 21 '20
Conclusion
We write code once and read it too many times.
Readability is king.
Disclaimer
Code Smells are just my opinion.
Credits
Photo by Nick Fewings on Unsplash
If someone claims to have the perfect programming language, he is either a fool or a salesman or both.
Bjarne Stroustrup
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.