May 18, 2022, 09:51:59 pm

The Gang Garrison 2 Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

NOTICE: Due to a rise in bot activity, new posters need to be approved before posting.

Join the community Discord server!

Pages: [1] 2 3 4

Author Topic: Coding style conventions  (Read 7010 times)

MedO

  • Owns this place
  • *****
  • Karma: 151
  • Offline Offline
  • Posts: 1751
Coding style conventions
« on: February 19, 2011, 10:58:53 am »

This has relatively low priority, but cspotcode and Chris had started with this once and we even led a lengthy discussion about brace styles on IRC later, so I think we should finally put up some guidelines. To start this up I'm pasting the old coding style doc from cspotcode that was lost in some later release, and maybe wareya can post the result of that discussion here (he wrote the results down iirc).

I know everyone has preferences here, but let's not put too much time and energy into discussing this. I don't think it's that important which style we stick to, as long as it's consistent (to prevent cases where people change everything they come across to their personal preference - it makes diffs longer and increases the risk of merge conflicts.)

So here's the old codingStyle.txt:
Quote
Coding Style:
Please try to stick to these rules to keep the code looking clean and readable.

Variable names should start with a lowercase letter, with subsequent words capitalized (intercapping).
Example: thisIsAVariable

Object names should start with a capital letter and be intercapped after that.
Example: DeathCam

Script names should start with a lowercase letter and be intercapped after that.
Example: setChatBubble

Constants are all capital letters, with underscores to separate words.
Examples: PROTOCOL_UUID or CHANGE_MAP

Put a semicolon at the end of a line of code, even though it's not required.

Indent ///How many spaces?///
  I like 2 spaces.  GM's default is 4, but it can be set to 2.

I agree with all of that, except that I prefer 4 spaces. It's also easier to get right because it's the default. Please don't use actual tab characters.
On the subject of braces, I prefer this style:
Code: [Select]
if(cloak) {
    backstab();
} else {
    shoot();
}
To put that into rules:
- Always put conditions of if/while or similar statements in parentheses
- Opening brace is on the same line as the statement the block belongs to
- Prefer adding braces even for one-line blocks (though that's just a weak preference for me)

I'd say we set down the rules in a democratic manner, i.e. everyone can propose things to add, and the majority decides whether to add it this way or not. As I said above, please don't get into big discussions, It usually doesn't lead anywhere. Just state your preference and hope that others share it.
Logged
Quote from: Alfred North Whitehead
It is the business of the future to be dangerous; and it is among the merits of science that it equips the future for its duties.

Quote from: John Carmack
[...] if you have a large enough codebase, any class of error that is syntactically legal probably exists there.

Vindicator

  • Developer
  • ******
  • Karma: 84
  • Offline Offline
  • Posts: 2432
    • http://www.kylemoy.org/
Re: Coding style conventions
« Reply #1 on: February 19, 2011, 03:32:29 pm »

hurrah for camelcase!

Also, please use underscores to split conditionals into multiple lines if it is really long, it's really annoying to scroll left and right.
Logged

Phantom Brave

  • All Hail Classicwell
  • Moderator
  • *****
  • Karma: 70
  • Offline Offline
  • Posts: 12701
  • Another one --
Re: Coding style conventions
« Reply #2 on: February 19, 2011, 06:19:44 pm »

As for brackets, I'm personally opposite to Medo in that I think that whether a braces is on the same line as a conditional or not isn't a big deal unless you have a multiline conditional, but feel like having braces on one statement blocks is a big deal.
I also dislike } else { because of how it looks, but that's another thing.
Logged

http://steamcommunity.com/id/wareya/
ladies and gentlemen i would like to announce that the fact of the matter is up that the fact of the matter is a fact and it matters

Vindicator

  • Developer
  • ******
  • Karma: 84
  • Offline Offline
  • Posts: 2432
    • http://www.kylemoy.org/
Re: Coding style conventions
« Reply #3 on: February 20, 2011, 06:27:48 pm »

As for brackets, I'm personally opposite to Medo in that I think that whether a braces is on the same line as a conditional or not isn't a big deal unless you have a multiline conditional, but feel like having braces on one statement blocks is a big deal.
I also dislike } else { because of how it looks, but that's another thing.

I personally like having braces on their own line, but it's not a big deal lol.
Logged

Phantom Brave

  • All Hail Classicwell
  • Moderator
  • *****
  • Karma: 70
  • Offline Offline
  • Posts: 12701
  • Another one --
Re: Coding style conventions
« Reply #4 on: February 20, 2011, 07:19:03 pm »

As for brackets, I'm personally opposite to Medo in that I think that whether a braces is on the same line as a conditional or not isn't a big deal unless you have a multiline conditional, but feel like having braces on one statement blocks is a big deal.
I also dislike } else { because of how it looks, but that's another thing.

I personally like having braces on their own line, but it's not a big deal lol.
I also do, but there are occasions where they make sense, like when you have a lot of simple blocks. Of course consistency would be better, but I'm just going against my personal preference because I know it's not always better.
Having an else on the same line as an ending bracket, though, just makes my brain fall apart.
Logged

http://steamcommunity.com/id/wareya/
ladies and gentlemen i would like to announce that the fact of the matter is up that the fact of the matter is a fact and it matters

Chris P. Bacon

  • Administrator
  • *****
  • Karma: 195
  • Offline Offline
  • Posts: 770
  • Retired gg2 dev
Re: Coding style conventions
« Reply #5 on: February 21, 2011, 07:59:17 am »

I'm not that picky, I prefer to use a style like this, but it really isn't that important.  Even then, sometimes I keep the opening { on the same line as the if or else, and some times I don't.  Maybe I shouldn't be in talks about consistency  :hehe:

Code: [Select]
if (condition)
{
    do stuff;
}
else
{
    do other stuff;
}
« Last Edit: February 21, 2011, 08:00:37 am by Chris P. Bacon »
Logged

MedO

  • Owns this place
  • *****
  • Karma: 151
  • Offline Offline
  • Posts: 1751
Re: Coding style conventions
« Reply #6 on: February 21, 2011, 01:30:06 pm »

So wareya, Vindicator and Chris are in favor of having braces on their own lines. Let me try to put that in a rule:
- The opening and closing brace of a block should each be in an otherwise empty line, at the outer indentation level:
Code: [Select]
if (condition)
{
    do stuff;
}
else
{
    do other stuff;
}

Please vote yay or nay  :hehe:

I think we can also vote on these in one go:
- Always put conditions of if/while or similar statements in parentheses
- The things in codingStyle.txt (without the question of indentation size)
- Use four spaces per level of indentation

Whether to prefer braces around single line blocks depends on the brace style for me, so I'd like to wait with that until the braces are decided.

My vote is nay to the first and yay to the other three.
« Last Edit: February 21, 2011, 01:48:34 pm by MedO »
Logged
Quote from: Alfred North Whitehead
It is the business of the future to be dangerous; and it is among the merits of science that it equips the future for its duties.

Quote from: John Carmack
[...] if you have a large enough codebase, any class of error that is syntactically legal probably exists there.

Phantom Brave

  • All Hail Classicwell
  • Moderator
  • *****
  • Karma: 70
  • Offline Offline
  • Posts: 12701
  • Another one --
Re: Coding style conventions
« Reply #7 on: February 21, 2011, 03:27:58 pm »

I say yay to all four, of course.
Logged

http://steamcommunity.com/id/wareya/
ladies and gentlemen i would like to announce that the fact of the matter is up that the fact of the matter is a fact and it matters

Chris P. Bacon

  • Administrator
  • *****
  • Karma: 195
  • Offline Offline
  • Posts: 770
  • Retired gg2 dev
Re: Coding style conventions
« Reply #8 on: February 21, 2011, 03:36:05 pm »

vote yay all four :D
Logged

Vindicator

  • Developer
  • ******
  • Karma: 84
  • Offline Offline
  • Posts: 2432
    • http://www.kylemoy.org/
Re: Coding style conventions
« Reply #9 on: February 21, 2011, 04:35:26 pm »

What is this about four spaces per indent? Is that a setting, or are we supposed to hit space four times rather than using the tab key?
Logged

MedO

  • Owns this place
  • *****
  • Karma: 151
  • Offline Offline
  • Posts: 1751
Re: Coding style conventions
« Reply #10 on: February 21, 2011, 04:39:30 pm »

As far as I can tell, using the tab key (and/or Ctrl-i) in the GM editor with default settings actually gives you four spaces.
« Last Edit: February 21, 2011, 04:40:00 pm by MedO »
Logged
Quote from: Alfred North Whitehead
It is the business of the future to be dangerous; and it is among the merits of science that it equips the future for its duties.

Quote from: John Carmack
[...] if you have a large enough codebase, any class of error that is syntactically legal probably exists there.

Psychopath

  • Developer In Training
  • ******
  • Karma: 167
  • Offline Offline
  • Posts: 6696
  • Stop telling me to do things
Re: Coding style conventions
« Reply #11 on: February 21, 2011, 04:40:43 pm »

What is this about four spaces per indent? Is that a setting, or are we supposed to hit space four times rather than using the tab key?
By default it puts 4 spaces when pressing tab (but they are still deleted as one whole when deleting them), I do not recall if it's possible to modify that. I would need to check around.
Logged
Quote from: IRC
(8:01:46 PM) Psychopath: I'm just wondering what the next hot thing to fall on my lap will be
(8:01:57 PM) Lynn1: a girl maybe?
(8:02:01 PM) Psychopath: :o

Phantom Brave

  • All Hail Classicwell
  • Moderator
  • *****
  • Karma: 70
  • Offline Offline
  • Posts: 12701
  • Another one --
Re: Coding style conventions
« Reply #12 on: March 16, 2011, 05:09:11 pm »

Bump for awareness.
Logged

http://steamcommunity.com/id/wareya/
ladies and gentlemen i would like to announce that the fact of the matter is up that the fact of the matter is a fact and it matters

NAGN

  • Developer
  • ******
  • Karma: 146
  • Offline Offline
  • Posts: 16769
  • Yeah so now I have an idea
Re: Coding style conventions
« Reply #13 on: March 16, 2011, 05:28:25 pm »

under preferences, is it under automatic indentation (the tab/4 spaces option)?
Logged

Psychopath

  • Developer In Training
  • ******
  • Karma: 167
  • Offline Offline
  • Posts: 6696
  • Stop telling me to do things
Re: Coding style conventions
« Reply #14 on: March 16, 2011, 05:29:04 pm »

How about some consistency with the and/&& as well as the or/||

I've seriously seen conditionals that use combinations of them like the following one...

Code: [Select]
if(other.id != ownerPlayer.object and other.team != team && other.ubered == 0  && other.hp > 0) {
Logged
Quote from: IRC
(8:01:46 PM) Psychopath: I'm just wondering what the next hot thing to fall on my lap will be
(8:01:57 PM) Lynn1: a girl maybe?
(8:02:01 PM) Psychopath: :o
Pages: [1] 2 3 4
 

Page created in 0.036 seconds with 36 queries.