Common Mistakes I found in intern developers: Part 1: Collection API

Dulaj Rajitha
2 min readMay 26, 2019
https://www.maxpixel.net/

With the experience I got by working with software engineering interns, I noticed some of common mistakes and programming patterns.

Here I have listed down some of common mistakes and bad practices which will be done by new software developers. Therefore this will help the new java developers to identify and correct their mistakes in future.

Most common errors in Model Classes are associated with Java Collection type objects.

Null return for collections

Return null instead of empty collections or maps will add an extra complexity to your code. This will lead to have explicit null checks to avoid null point exceptions. Therefore it’s a good practice to return empty (collections, arrays and maps).

Collection class implementations as method parameters, return types or public fields

The main objective of having a Collection classes like List and Set is to have hide the implementation (abstraction) of the specific collection. Always it is a good practice to store such collection classes in proper parent class.

private List<String> data = new ArrayList<>();

In the above example the List class can have a ArrayList or a LinkedList based in the requirement. But both of those classes support the basic functionality of a List (since it is the parent class). If we don’t want to maintain an index, we can even store them in the most basic Collection as well.

Having a method parameter with Specific collection type is also violate the above objective. If someone used a ArrayList as a public method parameter, everyone calling that methods will have to have ArrayLists and someone with a LinkedList may not be able to call the above method directly.

The above rule is also applicable for Map classes of as well.

Mutable collection store and return

Mutable objects are objects that the state can be changed.

Therefore exposing mutable class fields should not be return since they can be changed from outside the class (which is not the intended way). Therefore when dealing with the mutable collections like List, we will have to take care of the above practice.

If you want to modify the content of the class now it is only possible with setter and add method only. Otherwise we can use the getItem method and call add method to modify the items.

Then the most common mistakes are happen in the class Constructors.

Let’s see those errors in the part two of the article series.

Please leave a comment below if you have any questions or feedback!.

If you like this post, follow me on Medium for more similar posts.

If you have any concerns or questions, please use the comment section below.

--

--