Coding Standard the II.

Discussions related to programming Transport Empire.

Moderator: Transport Empire Moderators

Post Reply
User avatar
iLess
Engineer
Engineer
Posts: 38
Joined: 23 Apr 2006 01:21

Coding Standard the II.

Post by iLess » 01 May 2006 23:45

Is the coding standard on the official page fixed now? Or can there still be made some changes?

User avatar
Hyronymus
Tycoon
Tycoon
Posts: 13190
Joined: 03 Dec 2002 10:36
Location: The Netherlands
Contact:

Post by Hyronymus » 02 May 2006 13:13

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.

User avatar
iLess
Engineer
Engineer
Posts: 38
Joined: 23 Apr 2006 01:21

Post by iLess » 02 May 2006 14:56

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 ;-)

Hellfire
Transport Empire Developer
Transport Empire Developer
Posts: 699
Joined: 03 Feb 2003 09:30
Location: Back at the office

Post by Hellfire » 02 May 2006 20:55

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.
Feel free to contact me over Email! My current timezone: Europe/Amsterdam (GMT+1 or GMT+2)

Code: Select all

+------------Oo.------+
| Transport Empire -> |
+---------------------+
[ General TE Discussion ] [ TE Development ] [ TE Coding ]
Under construction...

User avatar
iLess
Engineer
Engineer
Posts: 38
Joined: 23 Apr 2006 01:21

Post by iLess » 03 May 2006 10:37

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.

Grunt
Route Supervisor
Route Supervisor
Posts: 449
Joined: 03 Oct 2003 20:22
Location: Edmonton, Alberta
Contact:

Post by Grunt » 03 May 2006 10:44

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.
Grunt
(aka Stephan Grunt, CEO of Grunt Transport Inc. since 1994.)

User avatar
iLess
Engineer
Engineer
Posts: 38
Joined: 23 Apr 2006 01:21

Post by iLess » 03 May 2006 10:53

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'

User avatar
DominionSpy
Tycoon
Tycoon
Posts: 1429
Joined: 03 Oct 2003 23:59
Location: Lancashire, UK
Contact:

Post by DominionSpy » 03 May 2006 17:08

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;
}
Image
You're saying I'm a Dominion spy, and don't even know it! - Dr. Bashir
That's the Joker in my avatar, not me. No wait it is me.

Hellfire
Transport Empire Developer
Transport Empire Developer
Posts: 699
Joined: 03 Feb 2003 09:30
Location: Back at the office

Post by Hellfire » 03 May 2006 18:47

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?
Feel free to contact me over Email! My current timezone: Europe/Amsterdam (GMT+1 or GMT+2)

Code: Select all

+------------Oo.------+
| Transport Empire -> |
+---------------------+
[ General TE Discussion ] [ TE Development ] [ TE Coding ]
Under construction...

User avatar
iLess
Engineer
Engineer
Posts: 38
Joined: 23 Apr 2006 01:21

Post by iLess » 03 May 2006 21:03

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; 
}

Hellfire
Transport Empire Developer
Transport Empire Developer
Posts: 699
Joined: 03 Feb 2003 09:30
Location: Back at the office

Post by Hellfire » 04 May 2006 17:16

Hmm. Different interpretation. I interpreted it as "starts with a lower case character."

Anyway. I guess it's fine like that.
Feel free to contact me over Email! My current timezone: Europe/Amsterdam (GMT+1 or GMT+2)

Code: Select all

+------------Oo.------+
| Transport Empire -> |
+---------------------+
[ General TE Discussion ] [ TE Development ] [ TE Coding ]
Under construction...

User avatar
iLess
Engineer
Engineer
Posts: 38
Joined: 23 Apr 2006 01:21

Post by iLess » 05 May 2006 00:04

k. good. Now I'm happy ;-).

User avatar
uzurpator
Transport Empire Moderator
Transport Empire Moderator
Posts: 2134
Joined: 10 Jan 2003 12:21
Location: Wroclaw, Poland / Katowice, Poland

Post by uzurpator » 08 May 2006 22:23

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?
All art and vehicle stats I authored for TT and derivatives are as of now PUBLIC DOMAIN! Use as you see fit
Just say NO to the TT fan-art sprite licensing madness. Public domain your art as well.

Post Reply

Return to “Transport Empire Coding”

Who is online

Users browsing this forum: No registered users and 1 guest