DEV Community

Cover image for Simplify your code

Simplify your code

Jimmy Klein on March 08, 2024

Over the years, I have become very interested in clean code, calisthenics objects, etc. And over time, readings, videos, discussions and katas, my ...
Collapse
 
pedrorfpacheco profile image
Pedro Pacheco

I like your approach to the comment section. Depending on the problems, I either use the constant with a name that I can clearly understand or I extract it into a function that I test.

Regarding the "early return" topic, it was one of the first things I learned in the professional world and it really makes sense and optimizes the process.

Collapse
 
klnjmm profile image
Jimmy Klein

Thanks for your comment !

About early return, I don’t know nowdays how the « one return onlyΒ Β» is teached at school, but it’s a shame to learn early return in the profession l world…

Collapse
 
xwero profile image
david duymelinck • Edited

Great article!

side node: I would not let anyone that is an adult drive a car. There are already enough accidents with drivers that have a drivers license :)

Collapse
 
klnjmm profile image
Jimmy Klein

🀣

Collapse
 
sreno77 profile image
Scott Reno

This is good information!

Collapse
 
klnjmm profile image
Jimmy Klein

Thank you πŸ™

Collapse
 
iwane021 profile image
iwanprs

thank you for sharing

Collapse
 
madalitsonyemba profile image
Madalitso Nyemba

Insightful πŸ’―

Collapse
 
kriqn profile image
KriqN • Edited

Nice article!

About the "early return" - most of the time there are more things to check (fail) before reaching the "happy path" and that's why it is usually placed at the end of the method. Your example should looks like:

function canDriveACar(User $user): bool
{
    if (! $user->isAdult()) {
        return false;
    }

    if (! $user->learnInAccompaniedDriving) {
        return false;
    }

    return true;
}
Enter fullscreen mode Exit fullscreen mode