Rule Definition
Using the implementation class of a collection as the return type of your method ties your code to this specific implementation and makes it difficult to change the implementation class in the future.
For example, if you use the java.util.Collection interface, you can change your implementation class from a Set to a List or to a Queue according to your needs and performance requirements.
Remediation
Declare the type as an interface of a more appropriate collection.
Violation Code Sample
/** * @return the list of all dataset boxes present on the graphic view */ public ArrayList<DataSetBaseBox> getDataSetBoxes() { ArrayList<DataSetBaseBox> dataSetBaseBoxes = new ArrayList<DataSetBaseBox>(); //VIOLATION for (Component c : getComponents()) { if (c instanceof DataSetBaseBox) { dataSetBaseBoxes.add((DataSetBaseBox)c); } } return dataSetBaseBoxes; }
Fixed Code Sample
/** * @return the list of all dataset boxes present on the graphic view */ public List<DataSetBaseBox> getDataSetBoxes() { List<DataSetBaseBox> dataSetBaseBoxes = new List<DataSetBaseBox>(); //Fixed for (Component c : getComponents()) { if (c instanceof DataSetBaseBox) { dataSetBaseBoxes.add((DataSetBaseBox)c); } } return dataSetBaseBoxes; }
Reference
http://java.sun.com/docs/books/tutorial/collections/interfaces/index.html https://en.wikipedia.org/wiki/Liskov_substitution_principle
Related Technologies
JEE
Technical Criterion
Programming Practices - Modularity and OO Encapsulation Conformity
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.