Effective java

I just finished a great book a friend recommended to me about java. Although I don't have the knowledge to understand everything (it's quite complete and complex), I learned a lot of things. Here are some of them :

Consider static factory methods instead of constructors. And by declaring a private constructor, one can enforce control over the number of instantiation. It also come in handy for object with a lot of optional parameters with default values.

Eliminate obsolete reference. A garbage collector doesn't ensure there is no memory leaks.

Obey the general contract when overriding equals. This one is tricky and I won't detail it here, it's a remainder for myselfif I do that later.

Design and document for inheritance or else prohibit it. It's simple to inherit something, but can be difficult to support for in the long term, and even more difficult to chage if it's part of an API.

Don't use raw type in new code, and prefer list to array. For similar reasons, various errors which stem from casting object are detected at compile time.

Prefer for-each loops to traditionnal loops. With three exceptions : when one wants to delete some elements, transform some elements, or do parallel iteration over multiple loops.

Know and use the libraries. When I was young, I wanted to understand everything, and so I wanted to do everything by myself. Now I'm wiser, but still, this item is really important. Here is a little example from the book :

private static final Random rnd = new Random();
// Common but deeply flawed!
static int random(int n) {
return Math.abs(rnd.nextInt()) % n;
}

Which has 3 flaws (I only found one and half when I first read it)

Optimize judiciously. Write good programs rather than fast ones. Because if it's too slow at the end, a good architecture allow ones to optimize the program easily.

Prefer empty arrays or collections, not null. Special case should not be handled using null elements.

Prefer executors and tasks to threads. Better abstractions then thread exist now to do background tasks and manage queue. And, remember to document thread safety (immutable, unconditionally thread safe, conditionally thread safe, not thread safe, thread hostile).

 

I can go on for other items but I think it's enough for the time. I recommend this book for any object programmer who wants to write better code. It's not a book for beginner, but even if one is not a master of java, or just doesn't know java very well, it's still very useful.