DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 970,177 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Code Smell 62 - Flag Variables
Maxi Contieri
Maxi Contieri

Posted on • Updated on • Originally published at maximilianocontieri.com

Code Smell 62 - Flag Variables

Flags indicate what happened. Unless their name is too generic.

Problems

  • Readability

  • Maintainability

  • Coupling

Solutions

  1. Use meaningful names

  2. Try to avoid flags. They generate coupling.

Sample Code

Wrong

Right

Detection

We can search all the code for bad named flags.

Tags

  • Readability

Conclusion

Flags are widespread on production code. We should restrict their usage and use clear and intention revealing names.

Relations

More Info

Wikipedia


If you lie to the compiler, it will get its revenge.

Henry Spencer


Top comments (1)

Collapse
 
michaelcurrin profile image
Michael Currin

You suggested to restrict usage but didn't say how or provide and example.

Can I provide an alternative without the flag? Which is shorter and more readable.

while(true) {
  $elementSatisfies = doSomething();
  if (!$elementSatisfies) {
    break;
  }
}
Enter fullscreen mode Exit fullscreen mode

Or even

while(doSomething()) {
  pass
}
Enter fullscreen mode Exit fullscreen mode

🌚 Life is too short to browse without dark mode