DEV Community

Discussion on: Simplify your code

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