DEV Community

Claudio Guedes
Claudio Guedes

Posted on • Edited on

1

Simple clean code #1

Change this:

if (user.active === true && user.age >= 18 && user.email !== null && user.email !== undefined && externalParams === true) return true;
Enter fullscreen mode Exit fullscreen mode

To this:

const { active: userIsActive, age: userAge, email: userEmail } = user;

const minimalAge = 18;
const userAgeIsValid = userAge >= minimalAge;
const userHasConditionsToRegister = userIsActive && userAgeIsValid && userEmail && externalParams;

if (userHasConditionsToRegister) return true;
Enter fullscreen mode Exit fullscreen mode

You might be thinking, "But you've increased the code by a lot, before, I only had one line!" And yes, you're absolutely right! However, fewer lines of code aren't always better than more lines.

When working with other developers and frequently maintaining the codebase, small details like the ones I've shown can make a big difference. In just a few seconds, anyone can understand the logic and rules behind the code. Additionally, if you need to add another condition, it becomes easy and clear to do so.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

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

Okay