Every time I see a method call as follows:
Enum. TryParse (stringRepresentationOfEnum, true, out enumValue );
What does the 'true' parameter mean. In this example, based on my past experience, I know that in this method, it represents the ignoreCase parameter.
However, what if this is a completely different method you have never seen before:
Service. DoOperation (DateTime. Now, true, false, false, true, currentValue, userName );
This is a special XXX method. But assume that this is the signature required for this method. Sadly, it is difficult for this method to understand the meaning of the value of the boolean parameter,
If we cannot change the interface, what else can we do to make the code clearer?
First, we can pass the named variable for each boolean value:
Bool includeHeader = true;
Bool checkSecurity = false;
Bool validateDate = false;
Bool fireCompletionEvent = true;
Service. DoOperation (DateTime. Now, includeHeader, checkSecurity, validateDate, fireCompletionEvent, currentValue, userName );
//
In addition to the code being a little less concise, this is indeed much better.
Of course, in C #4.0, we do not need to do this. Instead, we can use the naming parameters:
Service. DoOperation (DateTime. Now, includeHeader: true, checkSecurity: false, validateDate: false, fireCompletionEvent: false, currentValue, userName );
Unfortunately, it cannot run properly as scheduled. The last two parameters cannot appear after the named parameters.
This means we must add some additional code:
Service. DoOperation (DateTime. Now, includeHeader: true, checkSecurity: false, validateDate: false, fireCompletionEvent: false, currentValue: currentValue, userName: userName );
This is why we recommend that you put the Boolean parameter at the end of the parameter list:
Service. DoOperation (DateTime. Now, currentValue, userName, includeHeader: true, checkSecurity: false, validateDate: false, fireCompletionEvent: false );
This is indeed much clearer, but it does need to be kept in mind when designing your method signature.