Use less if for parameter verification in the general sense

Source: Internet
Author: User

Each time you write the public method, the first thing is parameter verification. In the pastCodeAppears:

 
If (oneoftheargument = NULL) {Throw new argumentnullexception ("oneoftheargument", stringtable. errorclassnameargnull );}

However, the parameter table of the method sometimes takes a long time, and there are many combinations of logical judgments. At this time, the number of such statements is sometimes 6 to 7. Such code is overwhelming and less readable (from the perspective of pure data, the complexity of the circle will also be relatively high ). So now all of them are replaced with this method:

 
If oneoftheargument is null, the system throws callback. Error. argument. isnull (oneoftheargument); // overload. Error. argument. isnull (oneoftheargument, stringtable. errorclassnameargnull) with custom message prompts );

You can also:

// Check whether the value is null or only contains white space characters. If it is null, // throw argumentnullexception. If it only contains white space characters or/string. empty, argumentexception is thrown. error. argument. isnullorwhitespacestring (value, parametername, message); // if the value is smaller than minvalue, argumentoutofrangeexception is thrown. error. argument. lessthan (value, minvalue, message); // if the value is out of a specified range, argumentoutofrangeexception is thrown. error. argument. outofrange (value, minincl, maxincl, message); // there are many other...

In this way, the code looks much more neat.

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.