I have been told that flag arguments(boolean arguments) are a bad thing because it complicates the signature of the method and means that the method does more than one thing.

An issue I have with this rule is what if I have a html factory class that builds raw html from arguments passed in. Then lets say I want an option to make the form element required. This would obviously make most sense as a boolean argument. But this is contradictory to what I have been told.

So my questions is: is this an exception to this rule, does this rule even make sense, or what is the correct way to do a required option in an html factory class?


Every rule in programming is just a guidance(Just like this exact statement, I bet there are a few rules that are not a guidance :)). There are many ways of achieving the same thing, but some are better than others.

In your case, using flags is totally fine, if you see that they are truly flags(yours seems to be).

Another important moment to consider is that when yo have a flag, it's best when it is used in the method directly, and not completely changing the scope of execution for everything below. A.K.A Simple flag - in your case it's as simple as it gets:

return String.Format("<input type=\"text\" class=\"required-{0}\" />", isRequired);

The problem of using boolean parameters to me, is that when you encounter a call to such method, it is unclear what that boolean is representing. You would have to look it up, while I would rather focus on the problem I'm trying to solve.

I find it more readable to create a seperate method for it, such as addRequiredTextBox() and addOptionalTextBox() instead of addTextBox(true|false). To prevent code duplication, have the addRequiredTextBox() and addOptionalTextBox() call to a private method addTextBox(true|false).

  • Or a small special-purpose enum that only has optionalField and requiredField values. – Sebastian Redl Jun 25 '15 at 12:57
  • Or just utilize what modern IDEs supply for you - intellisense. – Alexus Jun 25 '15 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.