Brief Summary of Coding Style and Practices used in JGAP

[JGAP Home]


The following is a short list of guidelines to follow when contributing code or patches to the JGAP project. Before we commit any code to the CVS repository, we need to make it follow these guidelines (for protection of our own sanity), so it's a lot easier on us if your code simply adheres to the guidelines when you submit it :-) Thanks!

A sort of ironic compilation of dont do's can be found in Roedy Green's article, it's worth a read.

Formatting of Code

  1. All new code files must have a copyright statement at the top that reads:

    /*
     * This file is part of JGAP.
     *
     * JGAP offers a dual license model containing the LGPL as well as the MPL.
     *
     * For licensing information please see the file license.txt included with JGAP
     * or have a look at the top of class org.jgap.Chromosome which representatively
     * includes the JGAP license policy applicable for any file delivered with JGAP.
     */        
  2. Add the lines

    /** String containing the CVS revision. Read out via reflection!*/
    private static final String CVS_REVISION = "$Revision: 1.15 $";

    after each class declaration line at top of the class file. For interfaces omit the "private" statement.

  3. When importing classes with the import statement, use the generic form and don't import single classes:

    import java.util.*;    // correct
              import java.util.List; // WRONG
  4. Do not format javadocs by adding unnecessary spaces or indentation (javadoc output will do this!)

  5. Do not start a javadoc for parameters with a capital letter. Do not end such text with a period.

  6. In methods: Order of javadoc elements should be:

  7. All classes, methods, and fields require javadocs, including private methods and fields and the main method.

  8. All in-line comments should be "underlined." For example:

    // This is an in-line comment.
    // Notice that it is underlined.
    // -----------------------------
    doSomeWork();
  9. Indent 4 spaces. No tabs, please.

  10. Lines of code should not exceed 79 characters in length. Please take this very seriously!

  11. Curly braces go on the same line, no extra indentation. For example:

    public void foo() {
      doSomeWork();
    }
  12. Always but the statements after an if or else-statement in curly braces, like:

    if (condition) { // this curly brace is required
      statement1;
      statement2;
    }
  13. Class names always start with a capital letter and contain no underscores. THIS IS VERY IMPORTANT!

  14. Class and instance variables, other than constants, should start with m_. For example:

    private int m_myField;
  15. Method arguments should always begin with "a_". For example,

    public void foo(int a_someValue);
  16. Constants should be in all capitals. For example,

    public static final int MY_CONSTANT_VALUE = 1;
  17. In interfaces, don't use the key word public for declared methods.

Programming

  1. Favor clean, readable, maintainable code over optimized code. Let a profiler determine where the bottlenecks exist and then optimize as little as possible to achieve desired performance. When you do optimize, please leave a very detailed comment explaining that the code has been optimized and what it is doing.

  2. Currently, we are not using Java 1.4 assertions in the JGAP code. We may begin doing so in the future, but for now please leave them out.

  3. If you end up creating a new package, please also create a package.html file that explains the purpose of that package. Javadoc will use this file to generate a package description.

  4. In general, favor descriptive method and variable names over in-line comments. For example, instead of:

    // Calculate area of circle
    // ------------------------
    x = 2 * PI * r * r;        

    Do:

    areaOfCircle = getAreaOfCircle( circleRadius );
  5. When adding a new class or new method, you could add the RFE number in the javadoc comment.

Test cases

  1. For each new class introduced there should be one new (JUnit) test class created extending junit.framework.TestCase (see the JGAP unit tests for examples)

  2. Each test case class is added to a test suite class containing a reference to all unit tests in the package the test suite class is lying. See org.jgap.impl.AllImplTests for example. 

  3. Test clase and test suite classes are lying in a different directory branch than the source code classes. Please see the JGAP CVS: src is the toplevel directory for source code and test is the toplevel directory for test code. Please notice, that the src and test directory both have the same package hierarchy!

  4. For test cases, the class heading javadoc should write (e.g. for class StringGene):

    /*
      * Tests the StringGene class
      * ...
      * @author ...
      * @since <JGAP version the test was first introduced in, e.g: @since 3.6, see main page for current version or ask the admin>
      */
  5. Don't add any outputs to the console (System.out or System.err) in your test cases. In nearly any case this could be avoided completely by using explanatory messages with exceptions or the JUnit fail(...) method.

  6. It might be useful looking at the existing unit tests to get some hints about how to do exception handling etc. Also see Test-Driven Development: Useful Techniques, Resources.

  7. The blog entry Intentionally failing JUnit tests could be a useful resource.

  8. If you don't feel sure when to write your tests (before, during or after coding), the blog entry When TDD is not optimal might be useful.

CVS

  1. For developers, it's ok to commit in-progress code to CVS that doesn't work, but please try to avoid committing code that doesn't compile or that completely breaks everything. If you're going to be doing a lot of work that will likely break everything, we can consider creating a branch for you to work on until your code is at least semi-stable.

  2. It is better to check in quite often than to wait too long with committing your work.

  3. When checking in something to the CVS, you should do this per class as you could then add a useful comment. It is not forbidden adding the RFE number (the request number from the JGAP sourceforge RFE page) in the comment!

  4. If using WinDiff: It's much more comfortable using an external viewer (such as WinMerge) than using the internal textual representation.

In general

  1. Finally, you may find JGAP code that doesn't adhere to one of the rules. That doesn't mean it's ok to disregard them in your code! ;-)

  2. If you think an important functionality is missing or there is a design flaw in JGAP, feel free to post this as a RFE or drop a short note to the JGAP admin (see main page).

 


[JGAP Home]
 
   
      SourceForge Logo