Dashboard > DHIS Documentation > ... > Development standards and conventions > Code Conventions
  DHIS Documentation Log In | Sign Up   View a printable version of the current page.  
  Code Conventions
Added by krin, last edited by Anders Gjendem on Feb 20, 2007  (view change)
Labels: 
(None)

Code conventions

The DHIS 2 project has a set of code conventions to improve maintainability and readability.

Java source files

Ordering

The java source files should have the following ordering:

  1. Package declaration
  2. License
  3. Import statements
  4. Author and version information
  5. Class or interface declaration

Formatting rules

  • Open brace "{" appears at a new line
  • Closing brace "}" appears at a new line by itself, matching the corresponding open brace
  • implements and extends statements on separate lines
  • Methods are separated by a blank line
  • Use spaces inside parentesis, except when casting
  • Each line should only contain one statement
  • Always use braces in if, for, while, do, ... statements
  • Use spaces (four) instead of tabs

Formatting templates for IDEs

The license text

/*
 * Copyright (c) 2004-2007, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * * Neither the name of the HISP project nor the names of its contributors may
 *   be used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

Author and version information

Add the following before the first class/interface declaration in every file, using your own name. Also, make sure you enable the $Id$ keyword in Subversion (see Setting svn:keywords in Subversion)

/**
 * @author Example User
 * @version $Id$
 */

Comments

Create nice comments like this to group/explain code. Let the horisontal lines reach and touch the 80 character print margin (see Show print margin in Eclipse on how to display this line in Eclipse).

// -------------------------------------------------------------------------
// Getters and setters
// -------------------------------------------------------------------------

Example

DefaultExample.java
package org.hisp.example;

/*
 * Copyright (c) 2004-2007, University of Oslo
 * All rights reserved.
 * ...
 */

import java.util.Collection;
...

/**
 * @author Example User
 * @version $Id: example $
 */
public class DefaultExample
    extends AbstractExample
    implements Example, Serializable
{
    private int test;

    private List testCollection = new ArrayList();

    // -------------------------------------------------------------------------
    // Constructor
    // -------------------------------------------------------------------------

    public Example( Collection testCollection )
    {
        this.testCollection = (ArrayList) testCollection;
    }

    // -------------------------------------------------------------------------
    // Test
    // -------------------------------------------------------------------------

    public void setTest( int test )
    {
        this.test = test;
    }

    public int getTest()
    {
        if ( test < 10 )
        {
            return test;
        }
        else
        {
            return 10;
        }
    }

    // -------------------------------------------------------------------------
    // Example implementation
    // -------------------------------------------------------------------------

    public void doMagic()
        throws ExampleException
    {
        try
        {
            ...
        }
        catch ( MagicException e )
        {
            throw new ExampleException( "Magic failed", e );
        }
    }
}

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.6 Build:#812 Aug 06, 2007) - Bug/feature request - Contact Administrators