cover-img

Code Smell 51 - Double Negatives

Not operator is our friend. Not not operator is not our friend.

22 February, 2023

8

8

0

TL;DR: Avoid double negatives in boolean expressions

Problems

  • Readability

Solutions

  1. Name your variables, methods and classes with positive names.

Sample Code

Wrong

if (!work.isNotFinished())

Right

if (work.isDone())

Detection

This is a semantic smell. We need to detect it on code reviews.

We can tell linters to check for Regular Expressions like !not or !isNot etc as a warning.

Tags

  • Readability

Conclusion

Double negation is a very basic rule we learn as junior developers.

There are lots of production systems filled with this smell.

We need to trust our test coverage and make safe renames and other refactors.

Relations

Code Smell 24 - Boolean Coercions

Code Smell 07 - Boolean Variables

Code Smell 06 - Too Clever Programmer

More Info

What exactly is a name - Part II Rehab

https://refactoring.com/catalog/removeDoubleNegative.html

https://levelup.gitconnected.com/knot-of-nots-avoiding-negative-names-for-boolean-methods-641896a94a42

Credits

Photo by Daniel Herron on Unsplash

___

It’s harder to read code than to write it.

Joel Spolsky

Software Engineering Great Quotes

___

This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code

8

8

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.