![]() |
#1 |
Участник
|
Greg On Dynamics Ax: Refactoring a long parameter list of boolean flags
Источник: http://feedproxy.google.com/~r/GregO...3/fT2Fe7WicdQ/
============== 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:
public void print(DEV_PrintOptions _opts){if ((_opts & DEV_PrintOptions::Proforma) == DEV_PrintOptions::Proforma)// Do proforma stuffif ((_opts & DEV_PrintOptions::HideFooter) == DEV_PrintOptions::HideFooter)// Hide the footerif ((_opts & DEV_PrintOptions::HideHeader) == DEV_PrintOptions::HideHeader)// Hide the headerif ((_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); ![]() Источник: http://feedproxy.google.com/~r/GregO...3/fT2Fe7WicdQ/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
![]() |
#2 |
Участник
|
По мойму, в таких случаях лучше сделать параметры свойствами класса. В конструкторе инициализировать их значениями по умолчанию. Сделать на каждый параметр свой parmMethod, и перед вызовом метода print задать необходимые опции.
X++: obj.parmProforma(true); obj.parmHideHeader(true); obj.print(); |
|
|
|