DEV Community

loading...

Discussion on: Tips on naming boolean variables - Cleaner Code

Collapse
niemeier23 profile image
niemeier23

I created an account here just to <3 this comment.

It's not important at all that the auxiliary verb (is, has, was, etc) is a prefix, or not. It's not important that it be singular, and not plural. It's not important what temporal tense it's in.

It's about expressing a boolean value so that any English reading person could understand it, even if s/he doesn't know any programming principles. Make it literal and readable.

everyUserIsActive
someUsersAreActive
wasSuccessfullyUpdated
user.isAdmin

The variable should not beg the question, "what does this even mean?" or "what is this referring to?" It should be perfectly clear by simply reading it.


My other boolean pet peeve is this:

if($user->isAdmin) {
return true;
}
else {
return false;
}

Why not:
return $user->isAdmin;
?