Reputation: 132
I did a code review with a junior developer and a discussion got brought up about Booleans which he read something in a book called 'Clean Code' by Robert Martin. He said that the book explained that using booleans in parameters is bad practice and makes the code harder to test. Specifically, we are talking about code similar to the below:
public static void setActivateValidation(Boolean activate)
{
System.validation = activate;
update System.validation;
}
and he is making the claim that is a very bad practice saying instead it should be 2 methods like:
public static void turnOffValidation()
{
System.validation = false;
update System.validation;
}
public static void turnOnValidation()
{
System.validation = true;
update System.validation;
}
Is there any reason the first example would be considered bad practice? I personally dislike the second version since I feel like it's just duplicating code. Unit tests would be very similar in both whereas I would need 2 tests either way. I don't see how one is more complex than another to test.
Upvotes: 1
Views: 906
Reputation: 683
No problem in the first version of code. Just a setter with a simple logic.
IMHO, a situation where boolean as parameter is not good is when you use this parameter to branch logics inside the method. An easy example:
public void apply(boolean applyForAllUsers) {
if (applyForAllUser) {
// some logics to apply some operations for all users
} else {
// some logics to apply some operations for one users
}
}
Bad things can happen to this method:
In this case it makes more sense refactoring the code then the first version of your code.
Upvotes: 5