Archive for the ‘Design’ Category
Refactoring a long parameter list of boolean flags
If you have had the need to have a method accept many boolean arguments, you might design the method to accept a number of optional boolean parameters, such as:
public void print(boolean _proforma = false, boolean _hideFooter = false, boolean _hideHeader= false, boolean _archive = false)
This is not the best design. For a start if you are happy to accept all the defaults but want to archive the printout you’ll need to call the method like this:
print(false, false, false, true);
It also suffers from what refactoring practitioners call a “bad smell” – this one being a long parameter list, which makes it harder to read and understand, and gets worse over time as new arguments are added.
A refactoring to improve the design might be to introduce a (single) settings object to replace the parameters.
If you consider that to be overkill you may want to consider this alternative: replace the parameters with a single base Enum type:
Each enumeration element then has a value that maps to the two base binary. So the values for this base enum would be:
- Proforma – 1
- HideFooter – 2
- HideHeader – 4
- Archive – 8
This enables us to declare our print method like so:
public void print(DEV_PrintOptions _opts) { if ((_opts & DEV_PrintOptions::Proforma) == DEV_PrintOptions::Proforma) // Do proforma stuff if ((_opts & DEV_PrintOptions::HideFooter) == DEV_PrintOptions::HideFooter) // Hide the footer if ((_opts & DEV_PrintOptions::HideHeader) == DEV_PrintOptions::HideHeader) // Hide the header if ((_opts & DEV_PrintOptions::Archive) == DEV_PrintOptions::Archive) // Archive the printout }
Which allows the method to be called in a more flexible way, specifying only the options you wish to override, for example:
// print out proforma and archive it (accepting the default to print the footer and header): print(Dev_PrintOptions::Proforma | Dev_PrintOptions::Archive);