Page 1 of 1

Coding Standard the II.

Posted: 01 May 2006 23:45
by iLess
Is the coding standard on the official page fixed now? Or can there still be made some changes?

Posted: 02 May 2006 13:13
by Hyronymus
You're free to suggest changes to the current coding standard. If you can convince the coders of the importance of the changes I'm sure the changes will be executed.

Posted: 02 May 2006 14:56
by iLess
1.) Variables are always written lowercase (thus it can be easily distinguished between Variables, Type and Methods/Functions.)
2.) Class member variables are written as 'm_varname' instead of 'mVarName' thus separating the member property of the variable and its name in a visual more clearly way and maintaining rule 1.)
3.) static Class member variables and global static variables are written as 's_varname'
4.) Method argument names are written lowercase too.

instead of writing rName we would write r_name and the same with pName -> p_name
but i don't think that we really need to use the r and p thingie.

if we want to be really pickie we could define global statics with gs_
and locals with ls_ and class statics with ms_ ... but i think using s_ for all of them should be fine.
That's the style I've been using for some time now.

Oh. almost forgot to mention. remove those statics http://www.tt-forums.net/viewtopic.php?t=24887. If Urzupator really wants them he can put them in himself ;-)

Posted: 02 May 2006 20:55
by Hellfire
I'm not a fan of prefixes. That's why they don't occur in my code. ;)

1) Yea.
2) Nay @ Prefix. Lower case is fine by me.
3) Nay. Prefix. ;)
4) Yea.

As for variable naming, I always try to keep names consistent and clear.

E.g. If we want to store two timestamps, a start timestamp and an end timestamp, I'd call them "tsStart" and "tsEnd". At the front, there is a word or abbreviation of what's in the variable. So, the number of players would be "numPlayers" or "numberOfPlayers". I can live with "num_players" or "number_of_players", but I don't like typing underscores.

In case you're wondering, yes, my mind has been heavily poisoned by Java. ;)

Please don't take these comments personally. A coding standard is just a matter of taste. It's discussable.

Posted: 03 May 2006 10:37
by iLess
Hellfire wrote: 2) Nay @ Prefix. Lower case is fine by me.
You mean not even 'mVarname' ?. Some kind of prefix should be used for classmembers IMO, because:

1.) if you are using method arguments and classmembers in a method body it should be easy to identify which are members and which are parameters and which are static.

2.) if you have a member called 'name' in your class and the method takes an argument which is being used for setting this member variable you have to look for another name for the method argument

Code: Select all

class T
{
T(int number)
: number(number) //ambiguous but seems to work on gcc
{
}

void SetNumber(int number)
{
number=number; //this does nothing
}

void SetNumber(int now_i_need_to_use_my_imagination_for_another_var_name)
{
 number= now_i_need_to_use_my_imagination_for_another_var_name;
}

int number;
};
I overexagerated a little bit ;-) but I really do have sometimes trouble to find good names for method parameters that make sense (especially if they have to be in english), because the name that really fits best is already taken for the class member.

Posted: 03 May 2006 10:44
by Grunt
iLess wrote:if you have a member called 'name' in your class and the method takes an argument which is being used for setting this member variable you have to look for another name for the method argument

Code: Select all

class T
{
T(int number)
: number(number) //ambiguous but seems to work on gcc
{
}

void SetNumber(int number)
{
number=number; //this does nothing
}

void SetNumber(int now_i_need_to_use_my_imagination_for_another_var_name)
{
 number= now_i_need_to_use_my_imagination_for_another_var_name;
}

int number;
};
I overexagerated a little bit ;-) but I really do have sometimes trouble to find good names for method parameters that make sense (especially if they have to be in english), because the name that really fits best is already taken for the class member.
There's nothing wrong with

Code: Select all

void SetNumber(int number)
{
this->number=number; //this does what you would expect
}
apart from probably the unimaginable hassle of having to type out six extra characters.

Posted: 03 May 2006 10:53
by iLess
oh, heh. This i had in mind too, but just forgot to mention it. But this speaks too for the solution to use 'm_name'

Posted: 03 May 2006 17:08
by DominionSpy
iLess wrote:oh, heh. This i had in mind too, but just forgot to mention it. But this speaks too for the solution to use 'm_name'
I agree with iLess - having to use this-> can be confusing especially if you don't use it consistently:

Code: Select all

int number;
int difference;

void  SetNumber(int number, int number2)
{
  this->number = number;
  difference = number - number2;
}

Posted: 03 May 2006 18:47
by Hellfire
Very well then. Prefixes it is. So, a class would look like this:

Code: Select all

class ClassName {
public:
  void SetNumPlayers(int);
protected:
  int m_numPlayers;
};

void ClassName::SetNumPlayers(int numPlayers) {
  m_numPlayers = numPlayers;
}
Or do you have something else in mind?

Posted: 03 May 2006 21:03
by iLess
Not quite.


You forgot 4.)
it would look like

Code: Select all

class ClassName 
{ 
public: 
  void SetNumPlayers(int numplayers); 
protected: 
  int m_numplayers; 
}; 

void ClassName::SetNumPlayers(int numplayers) 
{ 
  m_numplayers = numplayers; 
}

Posted: 04 May 2006 17:16
by Hellfire
Hmm. Different interpretation. I interpreted it as "starts with a lower case character."

Anyway. I guess it's fine like that.

Posted: 05 May 2006 00:04
by iLess
k. good. Now I'm happy ;-).

Posted: 08 May 2006 22:23
by uzurpator

Code: Select all

this->yadda.yadda.yadda[x];
against this

Code: Select all

yadda.yadda.yadda[x];
I am pro option 1 - mostly because my IDE will show the fields and stuff to click&fill :P

As for the naming conventions - pick any, I mean - any. And be faithful to it.

Now - how does the coding standard see the option to use STL containers or heavy use of templates? Or inherintance and polymorphism?