Code Smell 162 – Too Many Parentheses

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 t…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxi Contieri

Parentheses are free of charge. Aren't they?

TL;DR: Use as few parentheses as possible.

Problems

  • Readability

  • Syntactic complexity

Solutions

  1. 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

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

This article is part of the CodeSmell Series.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxi Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maxi Contieri | Sciencx (2022-09-10T00:32:59+00:00) Code Smell 162 – Too Many Parentheses. Retrieved from https://www.scien.cx/2022/09/10/code-smell-162-too-many-parentheses/

MLA
" » Code Smell 162 – Too Many Parentheses." Maxi Contieri | Sciencx - Saturday September 10, 2022, https://www.scien.cx/2022/09/10/code-smell-162-too-many-parentheses/
HARVARD
Maxi Contieri | Sciencx Saturday September 10, 2022 » Code Smell 162 – Too Many Parentheses., viewed ,<https://www.scien.cx/2022/09/10/code-smell-162-too-many-parentheses/>
VANCOUVER
Maxi Contieri | Sciencx - » Code Smell 162 – Too Many Parentheses. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/10/code-smell-162-too-many-parentheses/
CHICAGO
" » Code Smell 162 – Too Many Parentheses." Maxi Contieri | Sciencx - Accessed . https://www.scien.cx/2022/09/10/code-smell-162-too-many-parentheses/
IEEE
" » Code Smell 162 – Too Many Parentheses." Maxi Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/09/10/code-smell-162-too-many-parentheses/. [Accessed: ]
rf:citation
» Code Smell 162 – Too Many Parentheses | Maxi Contieri | Sciencx | https://www.scien.cx/2022/09/10/code-smell-162-too-many-parentheses/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.