Home > Web Front-end > JS Tutorial > Simple clean code #1

Simple clean code #1

DDD
Release: 2024-12-02 08:01:14
Original
132 people have browsed it

Simple clean code #1

Change this:

if (user.active === true && user.age >= 18 && user.email !== null && user.email !== undefined && externalParams === true) return true;
Copy after login

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;
Copy after login

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.

The above is the detailed content of Simple clean code #1. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template