Put the boolean parameter at the end

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.