DEV Community

Discussion on: Writing Beautiful Code

Collapse
 
6temes profile image
Daniel

If you want to make your code even more beautiful:

$validateEmail = filter_var($emailAddress, FILTER_VALIDATE_EMAIL);
$validateMessage = strlen($message->text()) < 1000;

if (!$validateEmail || !$validateMessage) {
  return false;
}

return true;

would be:

$isEmailValid = filter_var($emailAddress, FILTER_VALIDATE_EMAIL);
$isMessageValid = strlen($message->text()) < 1000;

return $isEmailValid && $isMessageValid

(PHP is not my first language, so bear with me if there are missing brakets somewhere)

Collapse
 
restoreddev profile image
Andrew Davis

Good call! Iโ€™ve seen several recommendations for using is/has in Boolean variable names.

Collapse
 
6temes profile image
Daniel • Edited

Well, is/has is a convention. In Ruby it is common to use "question mark" instead.

But, more importantly, I would not use an imperative verb to name this boolean variable.

Imperative would more appropriate for methods (or functions) that have side effects. In this case, you are using the variable to store the result of a computation.

Collapse
 
bgadrian profile image
Adrian B.G.

This will not work in a real project, most likely you will have to provide a message on why exactly the invalidation failed.

One liners are always on my radar on "smelly code", they usually lead to nasty problems.

Collapse
 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€

Magic number

Collapse
 
6temes profile image
Daniel

I said, "more beautiful", not "perfect" :P

Thread Thread
 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€

Exactly, prettiest is perfect. One semi colon and a constant away from perfection and we can close this thread. Haha sorry I know this isn't a peer review, old habits and all that.

Thread Thread
 
6temes profile image
Daniel

Ah, yes! Semi colons... that thing. xD