Free and Open Source real time strategy game with a new take on micro-management

Difference between revisions of "CodingConventions"

From Globulation2

Jump to: navigation, search
m (Getting and setting)
m
 
(10 intermediate revisions by 6 users not shown)
Line 2: Line 2:


This page lists the coding conventions actually in use within Glob2. The intended purpose is to have coherent-looking code through the project. Sorry if this different to your usual conventions.
This page lists the coding conventions actually in use within Glob2. The intended purpose is to have coherent-looking code through the project. Sorry if this different to your usual conventions.

=== KISS: Keep it Simple Stupid ===
All new code written in Glob2 should be obvious. Don't use clever tricks. Everything should be kept as simple as possible, but no simpler. This is a fundamental principle, and all new code should be kept with KISS in mind.

=== Maintainability Is God ===
If code is readable, if its maintainable, then it is good. Sometimes people get haggled about using pure OO or get/set. Make sure that your code is maintainable. If you change one portion, always try to minimize the amount of other code segments that need updated. This encompasses many techniques. Encapsulation is one of the best.


===Indentation===
===Indentation===
Line 11: Line 17:
Classes should begin with capitals, new words should be denoted by a capital, and words should not be separated by underscores or dashes. In other words,
Classes should begin with capitals, new words should be denoted by a capital, and words should not be separated by underscores or dashes. In other words,


<code>class TotoIsMyFriend { };</code> <span style="color: green">// correct</span>
class TotoIsMyFriend { }; <span style="color: green">// correct</span>


instead of:
instead of:


<code>class toto_is_my_friend{};</code> <span style="color: red">// wrong</span>
class toto_is_my_friend{}; <span style="color: red">// wrong</span>


Class members, variables, and C function follow the same convention but without the initial capital:
Class members, variables, and C function follow the same convention but without the initial capital:


<code>void totoIsMyFriend(void);</code> <span style="color: green">// correct</span>
void totoIsMyFriend(void); <span style="color: green">// correct</span>


instead of:
instead of:


<code>void Toto-is-my-friend(void);</code> <span style="color: red">// wrong</span>
void Toto-is-my-friend(void); <span style="color: red">// wrong</span>




Line 85: Line 91:


enum foo { foo1, foo2, ... fooN };
enum foo { foo1, foo2, ... fooN };
static const int foo_min = foo1;
static const int fooMin = foo1;
static const int foo_max = fooN;
static const int fooMax = fooN;
static const int foo_count = foo_max - foo_min + 1;
static const int fooCount = fooMax - fooMin + 1;

===Getting and setting===

At present, it's standard to get values with <code>getFoo()</code> and set them with <code>setFoo(value)</code>. While this is clear, it doesn't fit with the C++ convention of using <code>foo()</code> and <code>foo(value)</code>. It also doesn't distinguish between the cases where we are supposed to think of <code>getFoo()</code> and <code>setFoo()</code> as simple wrappers around private variables vs. where we should expect more significant processing to be done. I propose that we use <code>getFoo()</code> only where the user should expect significant processing to be done. For example, <code>GAGCore::getFilename()</code> searches an entire list of directories for a file, whereas <code>debugLevel()</code> just sets an internal variable.


=== documentation ===
When doing documentation, a comment section for a function should look something like this:

<pre>
/**
* Any comments go here
* @param string $foo
* @param int $bar
* @return boolean
* @access private
* @todo document
*/
</pre>

keep in mind not all of these items are always needed. if it doesn't take any parameters, that section is not needed.<br />
so if it doesn't take any parameters, you can leave @param out

[[Category:en]]
[[Category:Developer Resources]]

Latest revision as of 04:48, 2 July 2012

Coding Conventions

This page lists the coding conventions actually in use within Glob2. The intended purpose is to have coherent-looking code through the project. Sorry if this different to your usual conventions.

KISS: Keep it Simple Stupid

All new code written in Glob2 should be obvious. Don't use clever tricks. Everything should be kept as simple as possible, but no simpler. This is a fundamental principle, and all new code should be kept with KISS in mind.

Maintainability Is God

If code is readable, if its maintainable, then it is good. Sometimes people get haggled about using pure OO or get/set. Make sure that your code is maintainable. If you change one portion, always try to minimize the amount of other code segments that need updated. This encompasses many techniques. Encapsulation is one of the best.

Indentation

Code should be indented by tabs. A tab should be equivalent to 4 spaces. Please note that in the code below, non-breaking spaces are used because tabs don't display correctly in web pages.

Class and struct naming

Classes should begin with capitals, new words should be denoted by a capital, and words should not be separated by underscores or dashes. In other words,

class TotoIsMyFriend { }; // correct

instead of:

class toto_is_my_friend{}; // wrong

Class members, variables, and C function follow the same convention but without the initial capital:

void totoIsMyFriend(void); // correct

instead of:

void Toto-is-my-friend(void); // wrong


Bracket placement

Brackets {} should be placed alone on a line, except if there is no or only one function call within them.

class Toto
{
    int i;
    void totoIsMyFriend(void) { return i; };
}; // correct

instead of:

class Toto {
    int i;
    void Toto-is-my-friend(void) {
        return i;
    };
}; // wrong

Emacs

If you use Emacs as your editor, you can make it apply these rules to Globulation 2 files by putting the following in the .emacs file in your home directory:

(defconst glob2-c-style
 '((c-basic-offset	. 4)
   (c-offsets-alist            . ((substatement-open . 0)
				   (inline-open	. 0)
				   ))
   )
 "Globulation 2 programming style")

;; Customizations for all modes in CC Mode.
(defun glob2-c-mode-common-hook ()
 (c-add-style "glob2" glob2-c-style)
 )

(add-hook 'c-mode-common-hook 'glob2-c-mode-common-hook)

And adding the following to the top line of each of your source files:

/* emacs settings information: -*- mode: c++; tab-width: 4; c-file-style: "glob2"; -*- */

Proposed Conventions

This section discusses proposals for programming style rules that have not yet been widely accepted

Counting

When referring to the total number of items of a particular type that there are, the current rule is to use numberOfFoo. I find this confusing, as I expect numberOfFoo to refer to the index number of a particular instance of foo. Some alternatives include:

  • fooCount
  • fooTotal
  • fooSize

fooCount seems the easiest to read of the three, fooTotal also makes sense, and fooSize, while confusing, mirrors the foo.size() convention used in STL containers.

By extension, where there is an ordered list of things (most likely an enum), we should always have the following definitions:

enum foo { foo1, foo2, ... fooN };
static const int fooMin = foo1;
static const int fooMax = fooN;
static const int fooCount = fooMax - fooMin + 1;

Getting and setting

At present, it's standard to get values with getFoo() and set them with setFoo(value). While this is clear, it doesn't fit with the C++ convention of using foo() and foo(value). It also doesn't distinguish between the cases where we are supposed to think of getFoo() and setFoo() as simple wrappers around private variables vs. where we should expect more significant processing to be done. I propose that we use getFoo() only where the user should expect significant processing to be done. For example, GAGCore::getFilename() searches an entire list of directories for a file, whereas debugLevel() just sets an internal variable.


documentation

When doing documentation, a comment section for a function should look something like this:

/**
 * Any comments go here
 * @param string $foo 
 * @param int $bar
 * @return boolean
 * @access private
 * @todo document
 */

keep in mind not all of these items are always needed. if it doesn't take any parameters, that section is not needed.
so if it doesn't take any parameters, you can leave @param out

Misc