The use of preconditions in Google guava makes pre-condition judgments more elegant

Source: Internet
Author: User
Tags array length google guava

These days to see fresco and glide code, found that they have used the preconditions to do pre-judgment conditions, found that the code becomes both elegant and easy to debug. the use of preconditions

OK, first look at the usual code to write how we do the parameter judgment.
Argument, throws an exception if the parameter is configured incorrectly

    Int[] Intarray = {1, 2, 3, 4, 5, 6};

    private void Testpreconditions (Boolean precondition, int[] array, int position) {
        if (!precondition) {
            throw new I Llegalargumentexception ("Precondition not allow!!");
        }
        if (array = = null) {
            throw new NullPointerException ("Array is null!!");
        }
        if (Array.Length = = 0) {
            throw new illegalargumentexception ("Array length is 0!!");
        }
        if (Position > Array.Length | | Position < 0) {
            throw new arrayindexoutofboundsexception ("position error!!");
        }
        Do something ...

    }

It looks like nothing here, but if my class is big, there are a lot of ways. See the entire "if (xx==null) throw{...} How to feel.
Let's see if we use preconditions to make a pre-judgment.

    private void Testpreconditions (Boolean precondition, int[] array, int position) 
    {
        preconditions.checkargument (precondition);
        Preconditions.checknotnull (array);
        Preconditions.checkelementindex (position, array.length, "position error!");
          Do something ...
    }

is not much fresher, using preconditions can express the program intention more clearly . Why to use preconditions

Then you may ask, why use preconditions, if the null pointer is in use, he will throw an exception.
But what we expect is to throw an exception as early as possible, rather than wait until the data is passed through the layer, passing it to a very deep location, so wasting system resources is less conducive to developing precise positioning errors.
Therefore, it is recommended to check the data before the entry of the method, or before the operation begins.
For example:

       int length = Datas.get (5). GetDescription (). Concat ("XX"). Split (","). Length;

Like this code, even if you throw a null pointer and do not know whether the object is a null pointer, you need to step-by-step debugging

This is preconditions. Another function: early detection of errors, precise control of the location of the error extraction preconditions

Preconditions is a function of guava, project address: Https://github.com/google/guava
Direct reference using the method

dependencies {
  compile ' com.google.guava:guava:19.0 '
}

The birth of guava is designed to expand the Java collection to improve development efficiency, but with years of development it has covered all aspects of Java development, in Java 8, you can see a lot of API is from the guava in the intact reference.
For example, initialize a collection and use the JDK to do so

JDK
list<string> List = new arraylist<string> (); 
List.add ("a");
List.add ("B");
List.add ("C");
List.add ("D");

But the use of guava can be very concise expression

Guava
list<string> List = lists.newarraylist ("A", "B", "C", "D");

But this library contains about 14k of methods, it's a big library, and here we just need his preconditions class.
Address: Https://github.com/google/guava/blob/master/guava/src/com/google/common/base/Preconditions.java

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.