Skip to main content

Command Palette

Search for a command to run...

Code Smell 62 - Flag Variables

Published
1 min read
Code Smell 62 - Flag Variables
M

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

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

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


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

Henry Spencer



This article is part of the CodeSmell Series.

D

Flags cause me to flip out. I had the unfortunate experience of refactoring another developer's work that had a function with 7 input flags. The whole thing could've been achieved with a single Enum.

M

I agree

I'd check if I can double dispatch the enum, and then create polymorphic objects

1

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.