2016. december 6., kedd

Use standard Java APIs instead of reimplementing it


While we are joyfully solving our tasks, we tend to forget, how much help we can get form standard Java API. Before implementing something very exciting, elegant and nice, it is always a useful tactic to stop ourselves, take a big breath, and check if the function is already available somewhere.

While making code reviews for my colleagues, I keep finding methods and functions, that are long time in Java, or in some commonly used utility library. It is embarrassing, while using the existing solution you could have spare the time for recreating, testing and maintaining your custom solution.

I believe, that knowing the basic Java functionality tells a lot about the programmer, and the way of coding, he works in the daily life.

Advantages of using standard Java API:

  • hundred percent tested
  • stable, long term available code 
  • in most cases, the existing solution is a way more elegant and effective implementation, than the one you come up with

So my advice is, try to find the function, you need, first in the default Java API, then in a commonly used library. Implement your solution, only if you are really sure, that it has not been done yet.

Sources to be checked:



Here are some examples of using Java API, instead of reimplement them:

Null-safe compare

Comparing two objects, considering that they both can be null, is a tricky one. I have seen some elegant and some less elegant implementation of it. But the best one, I saw is in the Objects class of Java ;)

Null-safe toString

If you use code like this

return delta == null ? "" : delta.toString()

you should consider using Objects.toString() instead. It provides a null-safe conversion, and avoids returning Strings, containing "null".

Do not create empty collections

Use built in constants in java.util.Collections

  • EMPTY_SET
  • EMPTY_LIST
  • EMPTY_MAP

Use already existing exceptions

A huge amount of predefined exceptions exists already in default Java packages. Why to implement your own exceptions, when we have so many, like:
  • java.lang.IllegalStateException
  • java.lang.IllegalArgumentException
  • java.util.MissingResourceException
  • java.util.IllegalFormatException

Array operations

Programmers tend to implement operations on arrays, only because ArrayUtils form commons does not contain the required function. They forget, that some of them is not implemented on purpose, while Arrays form java.util already contains them. Most useful ones, for my point of view are:

  • copyOf
  • fill
  • toString
  • deepEquals

Using builder for toString

Not everyone is so lucky, to be allowed to use the Lombok project for dynamic code generation, via annotations. Without a proper framework developers tend to let the IDE generate the toString method for classes. The problem is with the maintainability. Namely, it is often forgotten to regenerate the method, if the class changes.

The solution is the ToStringBuilder form Apache Commons. You have the possibility to generate toString via reflection.

At the end of your development phase, when it is probably, that your code will seldom be changed, you can generate a toString, without reflection. For this purpose, you can configure Eclipse to use ToStringBuilder instead of the built-in generator.









Nincsenek megjegyzések:

Megjegyzés küldése