Stop Using Dirty Diamonds in Java
Fellow Java developers, I’m tired of seeing generics like this.
List<String> strings = new ArrayList<String>();
You see that “<String>” in the constructor for the ArrayList? Yeah, that’s a dirty diamond. As of Java 7, dirty diamonds are not necessary. You should use clear diamonds, like this.
List<String> strings = new ArrayList<>();
As long as the parameterized type is included within the declaration, then the Java compiler will know how to parameterize the constructor. In this case, because List is parameterized with as a String, the compiler will know that the ArrayList will contain Strings.
This also applies to methods.
Foo<T> bar(){ //Some code that processes a bazz and something return new Foo<T>(bazz, something);
}
Another dirty diamond! The compiler knows that the method will return a Foo<T>, so you don’t need to specify the type in the return statement.
Foo<T> bar(){//Some code that processes a bazz and somethingreturn new Foo<>(bazz, something);
}
There, a clear diamond.
Java is already verbose. Let’s not make it worse.