Java Coding Standards
Java Coding Standards
Naming Convention
Use full English descriptors that accurately describe the variable/field/class/interface
Use terminology applicable to the domain
Use mixed case to make names readable
Use abbreviations sparingly, but if you do so then use then intelligently and document it
Avoid long names (<15 characters is a good tradeoff)
Avoid names that are similar or differ only in case
Java Comments
Comments should add to the clarity of code.
Comment Type
Documentation Starts with /** and ends with */
C style Starts with /* and ends with */
Single line Starts with // and go until the end of the line
Techniques for Writing Clean Code:
Specify the order of Operations: Use extra parenthesis to increase the readability of the code using AND
and OR comparisons. This facilitates in identifying the exact order of operations in the code
Write short, single command lines Code should do one operation per line So only one statement should
be there per line
Indentation
Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces
vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).
Wrapping Lines
When an expression will not fit on a single line, break it according to these general principles:
Break after a comma.
Break before an operator.
Prefer higher-level breaks to lower-level breaks.
Align the new line with the beginning of the expression at the same level on the previous line.
If the above rules lead to confusing code or to code that's squished up against the right margin,
just indent 8 spaces instead.
Initialization
Try to initialize local variables where they're declared. The only reason not to initialize a variable where
it's declared is if the initial value depends on some computation occurring first.
Blank Lines
Blank lines improve readability by setting off sections of code that are logically related.
1)Two blank lines should always be used in the following circumstances:
Between sections of a source file
Between class and interface definitions
2)One blank line should always be used in the following circumstances:
Between methods
Between the local variables in a method and its first statement
Before a block or single-line comment
Between logical sections inside a method to improve readability
Coding Conventions:
Avoid Serialization and Deserialization
Use String Buffer to Concatenate Strings
Assign null to Variables That Are No Longer Needed
Declare Constants as static final
Avoid Finalizers
Declare Method Arguments final
Synchronize Only When Necessary
Use Data Handlers for SOAP Attachments