Transport Tycoon Forums

The place to talk about Transport Tycoon
It is currently Mon Dec 18, 2017 5:17 am

All times are UTC




Post new topic  Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Coding Standard the II.
PostPosted: Mon May 01, 2006 11:45 pm 
Offline
Engineer
Engineer
User avatar

Joined: Sun Apr 23, 2006 1:21 am
Posts: 38
Is the coding standard on the official page fixed now? Or can there still be made some changes?


Top
   
 Post subject:
PostPosted: Tue May 02, 2006 1:13 pm 
Offline
Tycoon
Tycoon
User avatar

Joined: Tue Dec 03, 2002 10:36 am
Posts: 13161
Location: The Netherlands
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.

_________________
Image
Dutch Trainset for OpenTTD | Dutch Trainset Topic | Combined Roadset v0.10


Top
   
 Post subject:
PostPosted: Tue May 02, 2006 2:56 pm 
Offline
Engineer
Engineer
User avatar

Joined: Sun Apr 23, 2006 1:21 am
Posts: 38
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 ;-)


Top
   
 Post subject:
PostPosted: Tue May 02, 2006 8:55 pm 
Offline
Transport Empire Developer
Transport Empire Developer
User avatar

Joined: Mon Feb 03, 2003 9:30 am
Posts: 699
Location: Back at the office
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:
+------------Oo.------+
| Transport Empire -> |
+---------------------+

[ General TE Discussion ] [ TE Development ] [ TE Coding ]
Under construction...


Top
   
 Post subject:
PostPosted: Wed May 03, 2006 10:37 am 
Offline
Engineer
Engineer
User avatar

Joined: Sun Apr 23, 2006 1:21 am
Posts: 38
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:
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.


Top
   
 Post subject:
PostPosted: Wed May 03, 2006 10:44 am 
Offline
Route Supervisor
Route Supervisor
User avatar

Joined: Fri Oct 03, 2003 8:22 pm
Posts: 449
Location: Edmonton, Alberta
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:
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:
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.)


Top
   
 Post subject:
PostPosted: Wed May 03, 2006 10:53 am 
Offline
Engineer
Engineer
User avatar

Joined: Sun Apr 23, 2006 1:21 am
Posts: 38
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'


Top
   
 Post subject:
PostPosted: Wed May 03, 2006 5:08 pm 
Offline
Tycoon
Tycoon
User avatar

Joined: Fri Oct 03, 2003 11:59 pm
Posts: 1429
Location: Lancashire, UK
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:
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.


Top
   
 Post subject:
PostPosted: Wed May 03, 2006 6:47 pm 
Offline
Transport Empire Developer
Transport Empire Developer
User avatar

Joined: Mon Feb 03, 2003 9:30 am
Posts: 699
Location: Back at the office
Very well then. Prefixes it is. So, a class would look like this:

Code:
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:
+------------Oo.------+
| Transport Empire -> |
+---------------------+


[ General TE Discussion ] [ TE Development ] [ TE Coding ]

Under construction...


Top
   
 Post subject:
PostPosted: Wed May 03, 2006 9:03 pm 
Offline
Engineer
Engineer
User avatar

Joined: Sun Apr 23, 2006 1:21 am
Posts: 38
Not quite.


You forgot 4.)
it would look like
Code:
class ClassName 
{
public:
  void SetNumPlayers(int numplayers);
protected:
  int m_numplayers;
};

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


Top
   
 Post subject:
PostPosted: Thu May 04, 2006 5:16 pm 
Offline
Transport Empire Developer
Transport Empire Developer
User avatar

Joined: Mon Feb 03, 2003 9:30 am
Posts: 699
Location: Back at the office
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:
+------------Oo.------+
| Transport Empire -> |
+---------------------+


[ General TE Discussion ] [ TE Development ] [ TE Coding ]

Under construction...


Top
   
 Post subject:
PostPosted: Fri May 05, 2006 12:04 am 
Offline
Engineer
Engineer
User avatar

Joined: Sun Apr 23, 2006 1:21 am
Posts: 38
k. good. Now I'm happy ;-).


Top
   
 Post subject:
PostPosted: Mon May 08, 2006 10:23 pm 
Offline
Transport Empire Moderator
Transport Empire Moderator
User avatar

Joined: Fri Jan 10, 2003 12:21 pm
Posts: 2125
Location: Wroclaw, Poland / Katowice, Poland
Code:
this->yadda.yadda.yadda[x];


against this

Code:
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.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 13 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000-2017 phpBB Limited

Copyright © Owen Rudge/The Transport Tycoon Forums 2001-2017.
Hosted by Zernebok Hosting.