gbaswath / effective-java-programming

Effective Java - 3rd Edition Source Code & its Documentation with Test Cases
MIT License
1 stars 0 forks source link

Item 59: Know and use the libraries #10

Closed gbaswath closed 2 years ago

gbaswath commented 2 years ago

Don’t reinvent the wheel.

Highlights

gbaswath commented 2 years ago

Random Number Generation

To write a version of the random method without flaws, we have to know a fair amount about pseudorandom number generators, number theory, and two’s complement arithmetic. Luckily, there exists Random.nextInt(int). By using a standard library, you take advantage of the knowledge of the experts who wrote it and the experience of those who used it before us.

Ref - 37e086d

As of Java 7, we should no longer use Random. For most uses, the random number generator of choice is now ThreadLocalRandom. It produces higher quality random numbers, and it’s very fast. On my machine, it is 3.6 times faster than Random. For fork join pools and parallel streams, use SplittableRandom.

Ref - 09df54d

gbaswath commented 2 years ago

Extract URL Contents

To print the contents of a URL specified on the command line (which is roughly what the Linux curl command does). Prior to Java-9, this code was a bit tedious, but in Java 9 the transferTo() method was added to InputStream.

Standard Libraries

The libraries are too big to study all the documentation [Java9-api], but every programmer should be familiar with the basics of java.lang, java.util, and java.io, and their subpackages. Several libraries bear special mention. The collections framework and the streams library (Items 45–48) should be part of every programmer’s basic toolkit, as should parts of the concurrency utilities in java.util.concurrent. This package contains both high-level utilities to simplify the task of multithreaded programming and low-level primitives to allow experts to write their own higher-level concurrent abstractions.

Ref - 954d30d

gbaswath commented 2 years ago

Advantages

  1. By using a standard library, we take advantage of the knowledge of the experts who wrote it and the experience of those who used it before us.
  2. We don’t have to waste our time writing ad hoc solutions to problems that are only marginally related to our work. we would rather spend time working on our application than on the underlying plumbing.
  3. A third advantage of using standard libraries is that their performance tends to improve over time, with no effort on our part. Many of the Java platform libraries have been rewritten over the years, resulting in dramatic performance improvements.
  4. A fourth advantage of using libraries is that they tend to gain functionality over time. If a library is missing something, the developer community will make it known, and the missing functionality may get added in a subsequent release.
  5. A final advantage of using the standard libraries is that you place our code in the mainstream. Such code is more easily readable, maintainable, and reusable by the multitude of developers.
gbaswath commented 2 years ago

Summary

If we need to do something that seems like it should be reasonably common, there may already be a facility in the libraries that does what we want. If there is, use it; if we don’t know, check. Generally speaking, library code is likely to be better than code that we’d write ourself and is likely to improve over time.

This is no reflection on your abilities as a programmer. Economies of scale dictate that library code receives far more attention than most developers could afford to devote to the same functionality.