OpenCDS CodingGuidelines
From OpenFrag
This page describes the guidelines that should be considered when contributing to OpenCDS.
Contents |
License and package declaration
- /*
- OpenCDS
- Copyright (C) 2007 - 2008 The OpenCDS team
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- ----------------------------------------------------------------------------
- * Author: <Your name>
- ----------------------------------------------------------------------------
- */
- 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.
- import org.openfrag.OpenCDS.core.plugin.*;
- import org.openfrag.OpenCDS.core.logging.Logger;
- import java.util.Random;
- import java.io.*;
New line before and after the import(s).
Class declaration
Use of javadoc is a must.
- /**
- * The BlaatjeBlaat class. It initializes the initializer, which in turn initializes
- * some other initializors.
- *
- * @author <Your name>
- */
- public class BlaatjeBlaat extends Nothing
- {
- }
As author, fill in your name.
Method declaration
- /**
- * Removes the sheep from the hedge.
- *
- * @param amount The amount of sheep to remove.
- * @param kill Whether the sheep have to be killed or not.
- * @return True if successful, false if not.
- */
- public boolean removeSheep(int amount, boolean kill)
- {
- return true;
- }
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
- /**
- * The BlaatjeBlaat class. It initializes the initializer, which in turn initializes
- * some other initializors.
- *
- * @author <Your name>
- */
- public class BlaatjeBlaat extends Nothing
- {
- public boolean m_Initialized = false;
- }
All class member variables start with m_ and the descriptive name starts with a capital letter.
Method variables
- /**
- * Removes the sheep from the hedge.
- *
- * @param amount The amount of sheep to remove.
- * @param kill Whether the sheep have to be killed or not.
- * @return True if successful, false if not.
- */
- public boolean removeSheep(final int amount, final boolean kill)
- {
- int blaat = 3;
- int sheepKiller = 4;
- return true;
- }
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
- if (blaat == blaat)
- {
- // this is a tab.
- }
- else if (blaatje == blaatjeblaat)
- {
- //
- }
- else
- {
- //
- }
- for (; blaat != blaatje; ++blaat)
- {
- //
- }
- while (true)
- {
- //
- }
Etcetera. New lines everywhere!
Switch statement
- switch (t)
- {
- case 1:
- break;
- case 2:
- default:
- }
Expressions
- int i = 4;
- int k = 345;
- i += (k * i) - i;
Try and catch
All on a new line.
- try
- {
- tryingSomething();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- }
Comments
- // One line comments max of 2 lines like this
- // see, it isn't that hard. As soon as it gets longer, make it a
- /*
- * block.
- */
Always start with a space between // and the actual comment. Same goes for the /* block. Continued comments start with a whitespace. For example
- // Blaatjeblaat, oh my line is ending, so I'll just
- // start with a whitespace on the new line.
- /*
- * Oh my line is ending already, so I'll just start
- * with a whitespace on the new line.
- */

