Difference between revisions of "Coding style"

From Code::Blocks
 
Line 24: Line 24:
  
 
=== Variables naming ===
 
=== Variables naming ===
Non-class member variables should use capital letter at the start of each word, ''except for the first''.
+
Class names do not have to start with 'C' (as in CObject, CWindow, etc). Only abstract base classes (interfaces) should start with 'I'.
 +
But they all have to start with a capital letter.
  
 
Class member variables should start with <tt>m_</tt>. If the variable is a pointer, use <tt>m_p</tt> as a prefix.
 
Class member variables should start with <tt>m_</tt>. If the variable is a pointer, use <tt>m_p</tt> as a prefix.
 
Following the prefix is the variable's name. Use descriptive names and use capital letters at the start of each word (the first too). So a fictious variable holding a value for position would be named as <tt>m_ValueForPosition</tt>.
 
Following the prefix is the variable's name. Use descriptive names and use capital letters at the start of each word (the first too). So a fictious variable holding a value for position would be named as <tt>m_ValueForPosition</tt>.
 +
 +
Non-class member variables should not start with a capital letter.
 +
 +
Function names should always start with a capital letter.
  
 
Examples:
 
Examples:
Line 34: Line 39:
 
int m_SomeInt;
 
int m_SomeInt;
 
void* m_pSomePointer;
 
void* m_pSomePointer;
 +
bool SomeFunc();
  
 
// Bad
 
// Bad
Line 39: Line 45:
 
int m_someInt; // no capital letter 'S'
 
int m_someInt; // no capital letter 'S'
 
void* m_SomePointer; // no m_p prefix
 
void* m_SomePointer; // no m_p prefix
 +
bool someFunc(); // function with non-capital first letter
 
</pre>
 
</pre>
  
Line 54: Line 61:
 
int &someInt; // & not after type
 
int &someInt; // & not after type
 
void* pPtr, pSomeOther; // multiple declarations in one line
 
void* pPtr, pSomeOther; // multiple declarations in one line
 +
</pre>
 +
 +
 +
=== Spacing ===
 +
Prefer spaces over tabs. If you want to use tabs you won't be prosecuted (!) but please use a tab size of 4 characters.<br>
 +
Code indentation should be 4 characters.<br>
 +
In class declarations, the keywords <tt>public:</tt>, <tt>protected:</tt> and <tt>private:</tt> should be indented. The same goes for all the class members.
 +
 +
Examples:
 +
<pre>
 +
// Good
 +
class AClass
 +
{
 +
    public:
 +
        AClass(){ ... }
 +
    protected:
 +
        int m_Member;
 +
};
 +
 +
// Bad
 +
class AClass
 +
{
 +
public:
 +
    AClass(){ ... }
 +
protected:
 +
    int m_Member;
 +
};
 
</pre>
 
</pre>

Revision as of 09:45, 15 December 2005

NOTE: this is yet unfinished

The following are guidelines for the code formatting to use in Code::Blocks (C::B) source code. Anyone writing code or a patch for C::B, is required to follow these.

The general coding style is the ANSI one:

Example:

namespace FooSpace
{
    int Foo()
    {
        if (isBar)
        {
            Bar();
            return 1;
        }
        else
            return 0;
    }
}


Variables naming

Class names do not have to start with 'C' (as in CObject, CWindow, etc). Only abstract base classes (interfaces) should start with 'I'. But they all have to start with a capital letter.

Class member variables should start with m_. If the variable is a pointer, use m_p as a prefix. Following the prefix is the variable's name. Use descriptive names and use capital letters at the start of each word (the first too). So a fictious variable holding a value for position would be named as m_ValueForPosition.

Non-class member variables should not start with a capital letter.

Function names should always start with a capital letter.

Examples:

// Good
int aLocalVar;
int m_SomeInt;
void* m_pSomePointer;
bool SomeFunc();

// Bad
int SomeInt; // either no m_ prefix if it's a class variable, or capital first letter if it's a local variable
int m_someInt; // no capital letter 'S'
void* m_SomePointer; // no m_p prefix
bool someFunc(); // function with non-capital first letter


Variables declaration

One variable declared each time on a different line. For pointer/reference variables, put the * or & right after the type not before the variable name.

Examples:

// Good
int& someInt;
void* pPtr;

// Bad
int &someInt; // & not after type
void* pPtr, pSomeOther; // multiple declarations in one line


Spacing

Prefer spaces over tabs. If you want to use tabs you won't be prosecuted (!) but please use a tab size of 4 characters.
Code indentation should be 4 characters.
In class declarations, the keywords public:, protected: and private: should be indented. The same goes for all the class members.

Examples:

// Good
class AClass
{
    public:
        AClass(){ ... }
    protected:
        int m_Member;
};

// Bad
class AClass
{
public:
    AClass(){ ... }
protected:
    int m_Member;
};