DEV Community

Cover image for Code Smell 162 - Too Many Parentheses
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

8 1

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

  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)))

Enter fullscreen mode Exit fullscreen mode

Right


schwarzschild = (2 * GRAVITATION_CONSTANT * mass) / (LIGHT_SPEED ** 2)

Enter fullscreen mode Exit fullscreen mode

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.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (3)

Collapse
 
macielti profile image
Bruno do Nascimento Maciel

You should checkout Clojure.

Collapse
 
ervin_szilagyi profile image
Ervin Szilagyi

A minor correction,

I assume that the example wants to implement the Schwarzschild radius. Since this post is tagged with Javascript, ^ operator represents the XOR operation, not the "exponentiation" or "power". The would be ** operator (starting of ES2016).

Collapse
 
mcsee profile image
Maxi Contieri

Nice !

Thank you. I fixed it!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay