Java Code Standards for Liferay
Our standards will be based on Effective Java 2nd Edition available from Amazon and from Alibris. A 3rd edition is available. It is for newer OpenJDKs and will not be referenced here. Most of our deviations from the book are an effort to make unit testing faster and easier to achieve.
Keep in mind the principles represented by the acronym SOLID.
Unit Test All Logic
This includes a single if statement, loop, stream, lambda, switch, etc.
Avoid Integration Testing
Do not place logic in Liferay classes that need the Liferay environment to be tested. Move any logic to a separate class.
Use Dependency Inversion/Injection
In practice we use a manual version of Dependency Injection. Liferay based classes are used to instantiate all needed objects, without introducing logic. Then create a separate class to which you will pass all the needed objects to the constructor.
Validate All Constructor Parameters
All constructor parameters should be checked for nulls except where nulls are desireable. Other acceptable values should be checked for. In all cases throw and IllegalArgumentException where values do not meet expectations. Pass a string argument to the exception providing additional information, when needed. Liferay code cannot be relied upon to guarantee non-null values. Using the Optional class is the desired pattern. Example below.
Check constructor parameters
Favor Streams over Loops
When using lists, streams are to be favored over for loops or while loops.
Code Relationships Should Look Like a Star
If you favor composition over inheritance then your code should look like a star when diagramed. It shouldn’t look like a hiking trail or highway.
tags: liferay - java - code - standard - code standard