60 lines
1.1 KiB
Plaintext
60 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Flag variables are boolean parameters used to control the behavior of a method. Specifying such a parameter may seem innocuous, particularly if it's well named.
|
|
|
|
|
|
Unfortunately, the maintainers of the code calling the method won't see the parameter name, only its value. They'll be forced either to guess at the meaning or to take extra time to look the method up.
|
|
|
|
|
|
Instead, separate methods should be written.
|
|
|
|
|
|
This rule flags methods where a boolean parameter is used to control the majority of the code in the method.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,text]
|
|
----
|
|
public void getDressed(boolean isCold) {
|
|
|
|
if (isCold()) {
|
|
turtleneck();
|
|
sweater();
|
|
thickSocks();
|
|
heavyPants();
|
|
boots();
|
|
} else {
|
|
tshirt();
|
|
shorts();
|
|
sandals();
|
|
}
|
|
}
|
|
|
|
getDressed(false); // does this mean go naked?
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,text]
|
|
----
|
|
public void getDressedColdWeather() {
|
|
|
|
turtleneck();
|
|
sweater();
|
|
thickSocks();
|
|
heavyPants();
|
|
boots();
|
|
}
|
|
|
|
public void getDressedWarmWeather() {
|
|
tshirt();
|
|
shorts();
|
|
sandals();
|
|
}
|
|
|
|
getDressedWarmWeather();
|
|
----
|
|
|