PEDN> Main Web>NPL > DeveloperDoc>CodingStyle (2008-05-27, LiXizhi) Change language en zh-cn? EditAttach

Coding Style in ParaEngine

coding conventions used in C++, NPL, C# projects in ParaEngine. Edited by LiXizhi

References

Naming Convention

The first task you encounter when you start to write code is how to name your variables, objects, and functions. The importance of naming conventions can not be underestimated. Providing proper naming will result in self-documenting code, easily comprehended by others. Unless you want to obfuscate the source code, you should follow these guidelines.

Use a name that unambiguously describes a variable or object. A function should contain in its name a verb of the action that it implements.

Windows style (MFC applications, prepend Hungarian prefixes to identify the variable type):

INT nSomeVariable;
FLOAT fBarWeight;
DWORD dwUsersNumber;
BOOL bIsEngineStarted;
DOUBLE dCircleArea;
DOUBLE m_dCircleArea;  //class private variable, prepend m_ prefix

PVOID pSomeVoidPointer;

const INT USERS_NUMBER;

int i, j, n, m, tmp;  //some local loops variables, temporary variables

namespace MyNameSpace;

vector<int> nUsers;
vector<char *> pszUserNames;

class CSomeClass;

INT DoWork(INT nTimeOfDay);
FLOAT CalculateRadius(const& Circle rCircle);
INT StartIOManager();
INT OpenDvdPlayer(); //if abbreviation takes more than
// 2 letters do not capitalize the whole word

.NET platform (Hungarian notation, and prefixes are obsolete):

int someVariable;
float barWeight;
unsigned int usersNumber;
bool isEngineStarted;
double circleArea;
double circleArea; //refer to a class private variable
// in code as this->circleArea

void^ someVoidPointer;

const int UsersNumber;

int i, j, n, m, tmp;  //some local loops variables, temporary variables

namespace MyNameSpace;

array<int> users;
array<String> userNames;

class SomeClass;

int DoWork(int timeOfDay);
float CalculateRadius(const& Circle circle);
int StartIOManager();
int OpenDvdPlayer();  //if abbreviation takes more than 2 letters
// do not capitalize the whole word

Spaces, Braces, and Parenthesis

void some_function(int param)
{
        //function body
}

class SomeClass
{
        //class body
};

// this is preferred by LiXizhi, because it is more clear about scoping. And code does not appear too clouded
if (some_condition)
{
        //...
}

// this is preferred by K&R, because it is shorter. If you use this, use it consistently.
if (some_condition) {
        //...
}

The spaces in math expressions should follow this style:


float a, b, c, d;
int e, f, g, h;

a = (b + d) / (c * d);

e = f - ((g & h) >> 10);    //good
e =f-( (g&h ) >>10);        //bad
Topic revision: r1 - 2008-05-27 - 09:21:01 - LiXizhi
 

ParaEngine Developer Network

This site is powered by the TWiki collaboration platformCopyright © 2004-2008 ParaEngine Corporation
Ideas, requests, problems regarding ParaEngine platform Send feedback