Favor Java API Over DIY

 class​ Inventory {
 
 private​ List<Supply> supplies = ​new​ ArrayList<>();
 
 int​ getQuantity(Supply supply) {
 if​ (supply == ​null​) {
»throw​ ​new​ NullPointerException(​"supply must not be null"​);
  }
 
 int​ quantity = 0;
 for​ (Supply supplyInStock : supplies) {
 if​ (supply.equals(supplyInStock)) {
  quantity++;
  }
  }
 
»return​ quantity;
 
  }
 }

In the early days of programming, you had to do everything by yourself. In C, for instance, you still need to create a String using a char[] or implement your own list. You had to do this for all kinds of data structures and algorithms. Although this is a nice exercise, it’s also time-consuming and error-prone.

Times have changed. The Java API is huge, and it comes with many classes that can help you get the job done, such as String or List. Instead of reimplementing functionality from the API, you should reuse it whenever possible. Experts have written and optimized the Java API over time, resulting in a fast and practically bug-free standard library.

You already know the inventory system from Avoid Collection Modification During Iteration and other comparisons. Above, you can see the getQuantity() method, which informs us about the quantity of a Supply in stock.

At a first glance, the code looks good. The method validates its input parameter to avoid null values. It uses an abstract type for its internal data structure, as we recommend in Favor Abstract Over Concrete Types. It also iterates over the data structure with a for-each loop instead of a regular for loop, as we recommend in Favor For-Each Over For Loops.

We can vastly improve this code—it’s much more verbose than it needs to be.

 class​ Inventory {
 
 private​ List<Supply> supplies = ​new​ ArrayList<>();
 
 int​ getQuantity(Supply supply) {
» Objects.requireNonNull(supply, ​"supply must not be null"​);
 
»return​ Collections.frequency(supplies, supply);
  }
 }

Now that’s much shorter! This version yields the same result, but it uses the functionality of the Java API instead of custom code. Now that it’s shorter, it’s much more readable and easier to understand.

The utility class Collections provides a frequency() method that counts the number of occurrences of objects in a Collection. Plus, we have used the method requireNonNull() from the utility class Objects you’ve seen before, for example in Avoid Switch Fallthrough. This method throws a NullPointerException with a message if an object is null. With these methods, we’re able to condense twelve lines of code into a single return statement!

The Java class library, the Java API for short, is huge. It contains thousands of classes with all sorts of useful functionality that’s available to you at any given point in your code. It also offers several utility classes, which you can usually spot by an appended s, such as the Collections class for Collection-related helper methods or Objects for general-purpose operations on Objects.

You can often solve problems in your code much more concisely if you know the API. And just as important, the API received and receives extensive testing worldwide, which is probably more than you can do. Chances are that your code is more prone to bugs than the API.

Knowing the API well is what makes a true Java professional. You save time if you don’t reimplement (and test) functionality that’s already there.

There is a lot more functionality beyond what we have shown here—such as the conversion of time values with TimeUnit. We encourage you to explore the Java API!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.228.138