We have two sets of coding standards. First are the firm standards, which must be adhered by all code contributed to the system. Second are the guidelines, which should generally be followed; however, we fully recognize that exceptions may occur.
Firm Standards
Modified lines should have trailing spaces stripped.
- To do this in Intellij: Preferences > Editor > General, Other section. "Strip trailing spaces on Save:" make certain that the drop down has "Modified Lines" selected.
- To do this in Eclipse: Preferences > Java > Editor > Save Actions > Perform the selected actions on save > Format source code > Format edited lines
All imports should be explicitly listed.
- Intellij makes this difficult. Go to Preferences > Editor > Code Style > Java, Imports tab. Check Use single class import. For "Class count to use import with '*'" choose some improbably high number, like 200. Or higher.
- In Eclipse: Preferences > Java > Code Style > Organize Imports. For "Number of imports needed for .*", choose some improbably high number.
Avoid static imports.
- In Intellij, go to Preferences > Editor > Code Style > Java, Imports tab. For "Names count to use static import with '*'": choose an improbably high number; again, 200 seems decent. When Intellij offers a helper which tempts you with a static import, do not choose that foul option.
- In Eclipse: Preferences > Java > Editor > Content Assist: Uncheck "Use static imports".
Have IDE add license headers.
- In Intellij, go to Preferences > Editor > File and Code templates, Files tab. In the various file types, add the proper code template. Be aware that Intellij supports directives like ${YEAR}, which simplifies template creation.
- In Eclipse, there is a different menu path to configure each file type. Eclipse also supports directives like ${YEAR}.
- For Java, go to Preferences > Java > Code Style > Code Templates > Comments > Files, and add the proper code template. Check "Automatically add comments for new methods and types". Note that the default comment for Types includes an @author tag, which should be removed.
- For Javascript, go to Preferences > JavaScript > Code Style > Code Templates > Comments > Files, and add the proper code template.
- For XML, go to Preferences > XML > XML Files > Editor > Templates > xml declaration, and add the proper code template.
- For JSP and tag files, go to Preferences > Web > JSP Files > Editor > Templates. Select the appropriate file types and add the proper code template.
Properly indent Java code: 4 spaces for a regular line and 4 spaces for a continuation indentation.
- In Intellij, go to Preferences > Editor > Code Style > Java, Tabs and Indents tab. Make sure that "Use tab character" is NOT checked. Indent should be 4 spaces, Continuation Indent should be 4 spaces.
- In Eclipse, go to Preferences > Java > Code Style > Formatter > Edit > Indentations tab. Set "Indentation size" and "Tab size to 4", and set "Tab Policy" to "Spaces only".
Property indent JavaScript code: 4 spaces for a regular line and 4 spaces for a continuation line.
- In Intellij, go to Preferences > Editor > Code Style > JavaScript, Tabs and Indents tab. Make sure that "Use tab character" is NOT checked. Indent should be 4 spaces, Continuation Indent should be 4 spaces.
- In Eclipse, go to Preferences > JavaScript > Code Style > Formatter > Edit > Indentations tab. Set "Indentation size" and "Tab size to 4", and set "Tab Policy" to "Spaces only".
Property indent XML code, too. 4 spaces per indentation.
- In Intellij, go to Preferences > Editor > Code Style > XML, Tabs and Indents tab. Make sure that "Use tab character" is NOT checked. Indent should be 4 spaces.
- In Eclipse, go to Preferences > XML > XML Files > Editor. Select "Indent using spaces" and set "Indentation size" to 4.
All conditionals should have brackets, even those which are a single line long.
Guidelines
A single line of whitespace between methods is all that is needed.
Avoid JavaDocs which add no explanation beyond what can be easily deduced from the code.
For the most part, classes should be laid out in the following order:
LOG
fields
constructors
overridden methods (public)
protected/private methods
getters and setters with logic
- getters/setters with no logic