Do not use Boolean function parameters

Function parameters may be of any type. So why should we try to avoid Boolean parameters? Because they have two disadvantages. The main issue is that a Boolean value may lead to a function which does several things. Furthermore the created source code is hard to understand.

Let’s look at an example. Within the first example we want to manage all customers of a company. This manager shall offer a function to get a list of all customers. Furthermore we divide between active and inactive customers. Active ones are all which have made an order in the last five years.

We may create the following function:

GetCustomers(bool excludeInactiveCustomers)

This looks like a good solution but it has few disadvantages. The function contains a Boolean value to control the behavior of the query. So the parameter leads to two separate functionalities. Therefore, in such a case, it is better to create two separated functions.



In this example the whole functionality was controlled by the parameter. But what shall be done if the parameter controls a part of the function only? I think in this case, especially if it is a small part only, you should not create two functions. But you may remove the Boolean value by an enumerator to increase the readability of the source code.

For example our customer manager shall provide a function which exports all customer data into a file. In case the file already exists the function shall throw an error or overwrite the file. To implement this use case you may create the following function.

ExportCustomerData(string fileName, boolean overwriteExistingFile)

In this case the Boolean value does not change the functionality of the functions at all. It only controls a part of the functionality, in this case the error handling. Therefore you should not create two functions. But you should consider to replace the Boolean value as the resulting source code is not easy to read. For example a function call may be following:

costumerManager.ExportCustomerData(“C:\\data.csv”, true);

If someone reads the code, he will not know what the second parameter does. Therefore he must stop reading at this line of code and read the function documentation to understand the meaning of the second parameter.

Therefore in such cases I recommend to exchange the Boolean parameter with an enumerator. For example you may create an enumerator “FileSecurity” with the values “OverwriteExistingFile” and “KeepExistingFile”.

The function will now be changed to:

ExportCustomerData(string fileName, FileSecurity fileSecurity)

And the function call is changed to:

costumerManager.ExportCustomerData(“C:\\data.csv”, FileSecurity.OverwriteExistingFile);


You should try to avoid Boolean function parameters. They are not evil at all but in most cases Boolean function parameters will result in dirty code. Very often they can be replaced by an enumerator or it may be better to create two functions instead of one with a Boolean parameter.

Dieser Beitrag wurde unter .NET, C#, Clean Code veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

Du kommentierst mit Deinem Abmelden / Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s