Guava Learning notes using the selection and pre-judgment of the guava to make the code concise

Source: Internet
Author: User
Tags google guava

1, this article translates from the http://eclipsesource.com/blogs/2012/06/06/cleaner-code-with-guava-optionals-and-preconditions/, has said the wrong place , Welcome to treatise.

2, when I developed software, towards clean code development is my privilege, for some time, I have used in almost all of my projects Google melon (Google guava), the reason is very simple, guava for me to write beautiful code provides a great convenience, today, I want to show you how I use pre-judgment to avoid unnecessary if/throw declarations and use choices to improve the logic of your code.

Pre-judgment is not new, Apache Commons project has similar functions, but there is no simple solution provided by the melon, pre-judgment is used to verify the parameters of the method, state and so on, when the condition judgment result is false, the pre-judgment will throw the expected anomaly, the expected means that the following circumstances, When checking the status, you can use the preconditions.checkstate (condition), if the condition judgment result is false, will throw an illegal state exception, again, another kind of pre-judgment checkargument will throw illegal parameter exception, of course, Using the pre-judgment you only need to be explicit when using static import, let's take a look at an example.

Given is a method to accept list as a parameter, when this method is called, we need to check that the list is not null and is not empty, the general Java solution is as follows:

 Public void dosomething( List<Object>){  if (= =null) {    thrownewillegalargumentexception("List must Not is null ");  }  if( list.  IsEmpty()){    throw new  IllegalArgumentException("List must not being empty");  }  dosomethingmore(); }

When using Guava's pre-judgment, the number of codes is significantly reduced and the solution is as follows.

 Public void dosomething( List<Object>){  checkargument (! =null"List must not is null") ;  Checkargument(! List. IsEmpty ()"List must not being empty");  Dosomethingmore(); }

This is definitely an upgrade, but when this method combines the choice of the melon with the real sexy, the choice is a concept that is designed to avoid the morbid null concept (read here to understand me as God horse called her "morbid"), here there is obviously an object container to avoid null references, for example, NULL Most of the time is used to check if an object is not present, and if it does not exist, a null pointer exception occurs, as shown in the following code:

 Public void dosomething(){  if(this.  Field= =null){    thrownew  IllegalStateException("Field is not initialized");  }  dosomethingmore(); }

Instead of saving this object directly into field, I often use the selection. This avoids the null pointer exception in my code and looks more syntactically, and the above example combines the selection into the following:

 Public void dosomething(){  checkstate( field.  IsPresent()"Argument is not initialied");  Dosomethingmore(); }

Finally, it is up to you to decide whether this code is better and more concise by using pre-judgment and selection. From my point of view, this is more readable, and I look forward to hearing your point in the comments.

Guava Learning notes using the selection and pre-judgment of the guava to make the code concise

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.