Ok, first of all; why are boolean parameters bad?
Because they are not input data to the method, but control values. This means that someone from the outside (e.g. the upper level method that calls yours) has explicit control over the method: It simply tells the function what to do. It’s like someone talks to you like: “Draw a smiley face USING A PENCIL” and after that “Sign this paper WITH YOUR PEN”.
You probably know how a drawing is usually created, and you don’t need anyone to tell you what tool to use. The same is true for the signature. There’s a very good example which is usually used for explaining encapsulation: “I don’t need to know how an engine works in order to be able to drive a car”. It’s similar with methods. The caller should not know how the called method achieves a certain goal; it knows that the lower method CAN achieve that goal and that’s it.
They’re also bad because they make your code unreadable. You see that you have to pass boolean value to a method, but you have no idea what is it used for. So you go to the implementation and check it, wasting precious seconds (or even minutes, if the code is complicated enough)
So, how do we get rid of such methods? Simple. Write two functions:
- copy your original function, and give it a self-explanatory name
- eliminate the parameter from the newly created method. Replace all the occurrences of the parameter with the value ‘true’
- replace the old method call with the new one everywhere where the method was called with true
- now rename your original method
- eliminate the parameter and replace it with the value ‘false’
- solve your compilation problems
- refactor the new methods, simplifying boolean expressions (remember? you have trues and falses explicitly hardwired into your code. Those should be eliminated), extracting the common parts and renaming if its needed
- be happy, you’ve got rid of your control values
If your booleans are getting out of control, you can try to consider introducing polymorphism. That may lead to even more beautiful design.