Section : Documentation > Development

Coding Standards

Here we provide an overview of the coding standards employed in DataNucleus. If you want to work on DataNucleus or contribute code to DataNucleus you are expected to use these coding standards. We know everyone has their own preference but these are ours so you follow them or any contributed code will not be directly included as is. They may differ from Oracles coding conventions but then those are the conventions of some US company and that doesnt mean that they are necessarily the best, the official or any such title. These are ours, so best get used to it ;-).

If you are using Eclipse then we have an XML Configuration to specify in Eclipse.

Examples

Example of brace policy and other things

package mypackage;

import java.util.LinkedList;

public class MyIntStack
{
    private final LinkedList fStack;

    public MyIntStack()
    {
        fStack = new LinkedList();
    }

    public int pop()
    {
        return ((Integer) fStack.removeFirst()).intValue();
    }

    public void push(int elem)
    {
        fStack.addFirst(new Integer(elem));
    }

    public boolean isEmpty()
    {
        return fStack.isEmpty();
    }
}

Example of indentation

class Example
{
    int[] myArray = {1, 2, 3, 4, 5, 6};
    int theInt = 1;
    String someString = "Hello";
    double aDouble = 3.0;

    void foo(int a, int b, int c, int d, int e, int f)
    {
        switch (a)
        {
            case 0 :
                Other.doFoo();
                break;
            default :
                Other.doBaz();
        }
    }

    void bar(List v)
    {
        for (int i = 0; i < 10; i++)
        {
            v.add(new Integer(i));
        }
    }
}

Example with if else

class Example
{
    void bar()
    {
        do
        {
        }
        while (true);
        try
        {
        }
        catch (Exception e)
        {
        }
    }

    void foo2()
    {
        if (true)
        {
            return;
        }
        if (true)
        {
            return;
        }
        else if (false)
        {
            return;
        }
        else
        {
            return;
        }
    }

    void foo(int state)
    {
        if (true)
        {
            return;
        }
        if (true)
        {
            return;
        }
        else if (false)
        {
            return;
        }
        else
        {
            return;
        }
    }
}

Licensing

All contributions to the DataNucleus Project must adhere to the Apache 2 license. Notwithstanding the above, at the discretion of the PMC, DataNucleus Project downloads may include separately licensed code from third parties as a convenience and where permitted by the third party license, provided this is clearly indicated.

All contributions must contain the following copyright notice.

/**********************************************************************
Copyright (c) 2006 {your name} and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
{year} {contributor1} - {description of contribution}
{year} {contributor2} - {description of contribution}
    ...
**********************************************************************/

References

In this document we describe just a small set of guidelines. Some references are really worth a read, though our particular guidelines above override some of the things here.