Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Made more readable, fixed some formatting differences from the source to the current wiki. (Cloudstack Hackathon 2013)

...

http://www.apache.org/legal/src-headers.html#headers

Java Coding Conventions

Wiki MarkupThese 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

  1. Names representing types must be nouns and written in mixed case starting with upper case. E.g.,   StoragePool
  2. Variable names must be in mixed case starting with lower case, E.g.,   virtualRouter
  3. Names representing constants (final variables) must be all uppercase using underscore to separate words. E.g., MAX_TEMPLATE_SIZE_MB
  4. Names representing methods must be verbs and written in mixed case starting with lower case. E.g.,   copyTemplateToZone
  5. Abbreviations and acronyms should not be uppercase when used as name. E.g.,   startElbVm
  6. 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.
  7. Static variables are prefixed with s_. E.g_., s_logger_
  8. is prefix prefix should be used for boolean variables and methods. E.g.,   isFinished
  9. Exception classes should be suffixed with with Exception.
  10. Default interface implementations can be prefixed by Default or if intended to be subclassed, suffixed by Base. E.g., DefaultExternalNetworkElement or  or NetworkElementBase
  11. There are various naming conventions used to convey the design patterns common in Cloudstack, listed in the Appendix.

...

  1. File content must be kept within 120 columns
  2. Continuation of lines should be obvious:
    Code Block
    
    totalSum = a + b + c +

    
        d + e;
    
  3. # Must indent with space not tabs. Indentation = 4 spaces
  4. Line endings should be LR (Mac / Linux format)
  5. White space:
    1. - Operators should be surrounded by a space character.
    2. - Java reserved words should be followed by a white space.
    3. - Commas should be followed by a white space.
    4. - Colons should be surrounded by white space.
    5. - Semicolons in for statements should be followed by a space character.
  6. Block layout should be as illustrated below . Class, Interface and method blocks should also use this layout.
    Code Block
    
    while (!done)
    {
        doSomething
     {
        doSomething();

        done =
    
        done = moreToDo();

    {color:#777777}}
    #
    
    }
    
  7. If-else clauses must use the following layout:
    Code Block
    
    if (condition) {
        statements;
    } else {
        statements;
    }
    
  8. {
        statements;
    {color:#777777}} else {{color}
        statements;
    {color:#777777}}# The try-catch block follows the if-else example above

...