OpenCDS CodingGuidelines

From OpenFrag

Jump to: navigation, search

This page describes the guidelines that should be considered when contributing to OpenCDS.

Contents

License and package declaration

  1. /*
  2.     OpenCDS
  3.     Copyright (C) 2007 - 2008 The OpenCDS team
  4.  
  5.     This program is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU General Public License
  7.     as published by the Free Software Foundation; either version 2
  8.     of the License, or (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  18.     ----------------------------------------------------------------------------
  19.  *  Author: <Your name>
  20.     ----------------------------------------------------------------------------
  21. */
  22. package org.opencdspowered.opencds.core.util;

As author, fill in your name. Package names need to be clear, descriptive, and consistent. New line after the package declaration.

Imports

OpenCDS imports at the top. 2 or more classes needed from a package, use a asterisk.

  1. import org.openfrag.OpenCDS.core.plugin.*;
  2. import org.openfrag.OpenCDS.core.logging.Logger;
  3. import java.util.Random;
  4. import java.io.*;

New line before and after the import(s).

Class declaration

Use of javadoc is a must.

  1. /**
  2.  * The BlaatjeBlaat class. It initializes the initializer, which in turn initializes
  3.  *  some other initializors.
  4.  *
  5.  * @author <Your name>
  6. */
  7. public class BlaatjeBlaat extends Nothing
  8. {
  9. }

As author, fill in your name.

Method declaration

  1. /**
  2.   * Removes the sheep from the hedge.
  3.   *
  4.   * @param   amount    The amount of sheep to remove.
  5.   * @param   kill          Whether the sheep have to be killed or not.
  6.   * @return   True if successful, false if not.
  7. */
  8. public boolean removeSheep(int amount, boolean kill)
  9. {
  10.      return true;
  11. }

Javadoc is very important. Be sure to use it for every single method. You don't have to use javadoc for private methods, but I'd rather you did. I didn't in some situations (like methods that fire listeners). Method names start with a lower case letter. New 'words' start with a upper case letter.

Variables

Class variables

  1. /**
  2.  * The BlaatjeBlaat class. It initializes the initializer, which in turn initializes
  3.  *  some other initializors.
  4.  *
  5.  * @author <Your name>
  6. */
  7. public class BlaatjeBlaat extends Nothing
  8. {
  9.     public boolean m_Initialized = false;
  10. }

All class member variables start with m_ and the descriptive name starts with a capital letter.

Method variables

  1. /**
  2.   * Removes the sheep from the hedge.
  3.   *
  4.   * @param   amount    The amount of sheep to remove.
  5.   * @param   kill            Whether the sheep have to be killed or not.
  6.   * @return   True if successful, false if not.
  7. */
  8. public boolean removeSheep(final int amount, final boolean kill)
  9. {
  10.      int blaat = 3;
  11.      int sheepKiller = 4;
  12.      return true;
  13. }

Method variables start with a lower case letter. The second 'word' in the variable starts with a upper case letter.

Use 'final' where you can. Alot of variables aren't altered, especially ones passed to methods, you can use final there.

Expressions, statements

  1. if (blaat == blaat)
  2. {
  3.      // this is a tab.
  4. }
  5. else if (blaatje == blaatjeblaat)
  6. {
  7.      //
  8. }
  9. else
  10. {
  11.     //
  12. }
  13.  
  14. for (; blaat != blaatje; ++blaat)
  15. {
  16.     //
  17. }
  18.  
  19. while (true)
  20. {
  21.     //
  22. }

Etcetera. New lines everywhere!

Switch statement

  1. switch (t)
  2. {
  3.      case 1:
  4.            
  5.          break;
  6.      case 2:
  7.      default:
  8. }

Expressions

  1. int i = 4;
  2. int k = 345;
  3.  
  4. i += (k * i) - i;

Try and catch

All on a new line.

  1. try
  2. {
  3.     tryingSomething();
  4. }
  5. catch (Exception e)
  6. {
  7.     e.printStackTrace();
  8. }
  9. finally
  10. {
  11. }

Comments

  1. // One line comments max of 2 lines like this
  2. //  see, it isn't that hard. As soon as it gets longer, make it a
  3. /*
  4.  * block.
  5. */

Always start with a space between // and the actual comment. Same goes for the /* block. Continued comments start with a whitespace. For example

  1. // Blaatjeblaat, oh my line is ending, so I'll just
  2. //  start with a whitespace on the new line.
  3.  
  4. /*
  5.  * Oh my line is ending already, so I'll just start
  6.  *  with a whitespace on the new line.
  7. */
Personal tools
OpenFrag