...
http://www.apache.org/legal/src-headers.html#headers
Java Coding Conventions
Wiki Markup |
These are mostly taken from \[[1]\|http://geosoft.no/development/javastyle.html\] Image Added with modifications to reflect current style in Cloudstack. That document is itself derived from Sun’s original Java Code Conventions document.
Naming Conventions
- Names representing types must be nouns and written in mixed case starting with upper case. E.g., StoragePool
- Variable names must be in mixed case starting with lower case, E.g., virtualRouter
- Names representing constants (final variables) must be all uppercase using underscore to separate words. E.g., MAX_TEMPLATE_SIZE_MB
- Names representing methods must be verbs and written in mixed case starting with lower case. E.g., copyTemplateToZone
- Abbreviations and acronyms should not be uppercase when used as name. E.g., startElbVm
- Private class variables should have underscore underscore prefix. E.g_., _downloadTimer_. Exception: Transfer Objects (TOs), Database objects (VOs), Command objects:- private class variables in these classes have no underscores. The exception is justified since these are usually logged and are more readable without underscores.
- Static variables are prefixed with s_. E.g_., s_logger_
- is prefix prefix should be used for boolean variables and methods. E.g., isFinished
- Exception classes should be suffixed with with Exception.
- Default interface implementations can be prefixed by Default or if intended to be subclassed, suffixed by Base. E.g., DefaultExternalNetworkElement or or NetworkElementBase
- There are various naming conventions used to convey the design patterns common in Cloudstack, listed in the Appendix.
...
- File content must be kept within 120 columns
- Continuation of lines should be obvious:
Code Block |
---|
totalSum = a + b + c + |
- # Must indent with space not tabs. Indentation = 4 spaces
- Line endings should be LR (Mac / Linux format)
- White space:
- - Operators should be surrounded by a space character.
- - Java reserved words should be followed by a white space.
- - Commas should be followed by a white space.
- - Colons should be surrounded by white space.
- - Semicolons in for statements should be followed by a space character.
- Block layout should be as illustrated below . Class, Interface and method blocks should also use this layout. {
doSomething
done =
{color:#777777}}
# - If-else clauses must use the following layout:
Code Block |
---|
if (condition) {
statements;
} else {
statements;
}
|
- {
statements;
{color:#777777}} else {{color}
statements;
{color:#777777}}# The try-catch block follows the if-else example above
...