OpenTTD Coding Style and Guidelines

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

iNVERTED
Route Supervisor
Route Supervisor
Posts: 387
Joined: 24 Apr 2005 21:21
Location: Torquay, England
Contact:

Post by iNVERTED » 22 Aug 2006 14:41

FunctionsLookLikeThis(int i_am_a_variable)
{
code...
}
Any reason for the line break before the "{"? IMO, it looks ugly and wastes space.

DaleStan
TTDPatch Developer
TTDPatch Developer
Posts: 10285
Joined: 18 Feb 2004 03:06
Contact:

Post by DaleStan » 22 Aug 2006 14:44

Because that's what the style says. Live with it.
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser

User avatar
Brianetta
Tycoon
Tycoon
Posts: 2566
Joined: 15 Oct 2003 22:00
Location: Jarrow, UK
Contact:

Post by Brianetta » 22 Aug 2006 14:44

iNVERTED wrote:
FunctionsLookLikeThis(int i_am_a_variable)
{
code...
}
Any reason for the line break before the "{"? IMO, it looks ugly and wastes space.
It lines up with the } a the other end. You might think it's ugly, and it wouldn't work in a language with LISP-like syntax, but it's the chosen coding style. Changing the coding style with this much code already in the base is never a good idea, so don't pin your hopes on seeing it change. It won't.

If you dislike it, code in your own preferred manner, then convert it with indent before submitting it.
PGP fingerprint: E66A 9D58 AA10 E967 41A6 474E E41D 10AE 082C F3ED

TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1312
Joined: 31 May 2004 09:21

Post by TrueBrain » 22 Aug 2006 15:01

Brianetta wrote:
iNVERTED wrote:
FunctionsLookLikeThis(int i_am_a_variable)
{
code...
}
Any reason for the line break before the "{"? IMO, it looks ugly and wastes space.
It lines up with the } a the other end. You might think it's ugly, and it wouldn't work in a language with LISP-like syntax, but it's the chosen coding style. Changing the coding style with this much code already in the base is never a good idea, so don't pin your hopes on seeing it change. It won't.

If you dislike it, code in your own preferred manner, then convert it with indent before submitting it.
And we like it. Period.

Brianetta: indent sucks ass :p Never makes indents like I want it :(

iNVERTED
Route Supervisor
Route Supervisor
Posts: 387
Joined: 24 Apr 2005 21:21
Location: Torquay, England
Contact:

Post by iNVERTED » 22 Aug 2006 16:24

Brianetta wrote:It lines up with the } a the other end.
Well, that's inconsistent. You never have things like:

if (blah)
{

}

while (blah)
{

}

User avatar
XeryusTC
Tycoon
Tycoon
Posts: 15415
Joined: 02 May 2005 11:05
Skype: XeryusTC
Location: localhost

Post by XeryusTC » 22 Aug 2006 18:10

iNVERTED wrote:
Brianetta wrote:It lines up with the } a the other end.
Well, that's inconsistent. You never have things like:
*snip*
First of all, it isn't inconsistent, it makes the code more consistent as lines have their own brackets.
Second, it exists, I don't know if the (old) OTTD source has this kind of things but TRoS does it that way.
Don't panic - My YouTube channel - Follow me on twitter (@XeryusTC) - Play Tribes: Ascend - Tired of Dropbox? Try SpiderOak (use this link and we both get 1GB extra space)
Image
OpenTTD: manual #openttdcoop: blog | wiki | public server | NewGRF pack | DevZone
Image Image Image Image Image Image Image

iNVERTED
Route Supervisor
Route Supervisor
Posts: 387
Joined: 24 Apr 2005 21:21
Location: Torquay, England
Contact:

Post by iNVERTED » 22 Aug 2006 19:34

XeryusTC wrote:
iNVERTED wrote:
Brianetta wrote:It lines up with the } a the other end.
Well, that's inconsistent. You never have things like:
*snip*
First of all, it isn't inconsistent, it makes the code more consistent as lines have their own brackets.
Second, it exists, I don't know if the (old) OTTD source has this kind of things but TRoS does it that way.
Huh?

In OTTD source, the only place where { goes on a line by itself is in functions. The if/while/whatever all had the { on the same line as the statement. Or maybe my memory just sucks. :P

User avatar
Darkvater
Tycoon
Tycoon
Posts: 3053
Joined: 24 Feb 2003 18:45
Location: Hong Kong

Post by Darkvater » 22 Aug 2006 20:17

iNVERTED wrote:In OTTD source, the only place where { goes on a line by itself is in functions. The if/while/whatever all had the { on the same line as the statement. Or maybe my memory just sucks. :P
Yes, that is consistent. For all functions the braces are on a new line, for all else it's on the same line. Consistent.
TrueLight: "Did you bother to read any of the replies, or you just pressed 'Reply' and started typing?"
<@[R-Dk]FoRbiDDeN> "HELP, this litte arrow thing keeps following my mouse, and I can't make it go away."

User avatar
Brianetta
Tycoon
Tycoon
Posts: 2566
Joined: 15 Oct 2003 22:00
Location: Jarrow, UK
Contact:

Post by Brianetta » 22 Aug 2006 21:21

TrueLight wrote:Brianetta: indent sucks ass :p Never makes indents like I want it :(
I shall have to knock up an indent config for you...
PGP fingerprint: E66A 9D58 AA10 E967 41A6 474E E41D 10AE 082C F3ED

User avatar
Korenn
Tycoon
Tycoon
Posts: 1628
Joined: 26 Mar 2004 01:27
Location: Netherlands, Enschede
Contact:

Post by Korenn » 23 Aug 2006 19:53

Darkvater wrote:
iNVERTED wrote:In OTTD source, the only place where { goes on a line by itself is in functions. The if/while/whatever all had the { on the same line as the statement. Or maybe my memory just sucks. :P
Yes, that is consistent. For all functions the braces are on a new line, for all else it's on the same line. Consistent.
it's indeed consistent, but still... yuck :P
Creator of the Openttd Challenge Spinoff, Town Demand patch
The path to riches, a report on playing on a daylength server on ultra hard mode.

iNVERTED
Route Supervisor
Route Supervisor
Posts: 387
Joined: 24 Apr 2005 21:21
Location: Torquay, England
Contact:

Post by iNVERTED » 23 Aug 2006 22:14

Darkvater wrote:
iNVERTED wrote:In OTTD source, the only place where { goes on a line by itself is in functions. The if/while/whatever all had the { on the same line as the statement. Or maybe my memory just sucks. :P
Yes, that is consistent. For all functions the braces are on a new line, for all else it's on the same line. Consistent.
*smacks forehead*

User avatar
Darkvater
Tycoon
Tycoon
Posts: 3053
Joined: 24 Feb 2003 18:45
Location: Hong Kong

Post by Darkvater » 17 Jan 2007 13:17

With the addition of C++ code to OpenTTD we will update the coding styles for C++ style constructs (overload, member variable, visibility, parameter, etc.).

Expect an updated opening post in the near future.
TrueLight: "Did you bother to read any of the replies, or you just pressed 'Reply' and started typing?"
<@[R-Dk]FoRbiDDeN> "HELP, this litte arrow thing keeps following my mouse, and I can't make it go away."

User avatar
Knuff
Engineer
Engineer
Posts: 13
Joined: 12 Mar 2004 18:14

Re: OpenTTD Coding Style and Guidelines

Post by Knuff » 22 Jan 2008 20:37

Hi!

Found a goto while reading openttds code, so yesterday i searched the code for goto statements.... found 147!!! I hope you all know that using goto produces really really messy and bad bad code. Now there's 3 possibilities:
1. You didn't notice.
2. You don't care about bad code.
3. You don't have time for fixing it.
So, could you tell me which of these applies? ;)

Remember: Knowing what goto does is fine, but do not actually use it!

knuff

Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Re: OpenTTD Coding Style and Guidelines

Post by Rubidium » 22 Jan 2008 20:50

It's #3 that applies and "if it ain't broke, don't fix it".

DaleStan
TTDPatch Developer
TTDPatch Developer
Posts: 10285
Joined: 18 Feb 2004 03:06
Contact:

Re: OpenTTD Coding Style and Guidelines

Post by DaleStan » 22 Jan 2008 21:32

Knuff wrote:So, could you tell me which of these applies? ;)
None of the above. Gotos are occasionally the best way to do things. Blind adherence to the rules causes code just as bad as blind flaunting of the rules. They even appear in the Linux kernel.
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser

User avatar
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Re: OpenTTD Coding Style and Guidelines

Post by Bilbo » 22 Jan 2008 23:58

DaleStan wrote:Gotos are occasionally the best way to do things.
For example if you have 3 nested for cycles and you want to break out of two inner levels. Without goto impossible ("break;" will only break one level), or at least impossible without setting some flags and issuing things like if (break_flag) break; at more levels. Which is ineffective. Though these situations hapen relatively rarely.
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)

DaleStan
TTDPatch Developer
TTDPatch Developer
Posts: 10285
Joined: 18 Feb 2004 03:06
Contact:

Re: OpenTTD Coding Style and Guidelines

Post by DaleStan » 23 Jan 2008 03:49

Another case, which actually can be done without any extra variables, is this:

Code: Select all

while (true) {
   // code
      // more code
         // with lots and lots of nesting
            if (rare_condition_requiring_restart) continue;  // This happens maybe 1% of the time.
      // still more code
   // and some more
   break;
}
Using a label instead of the while and a goto instead of the continue means I can leave off the whole "while (true) { /*do stuff*/ break; }" thing. Is that insanity really preferable to a goto, Knuff?
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser

User avatar
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Re: OpenTTD Coding Style and Guidelines

Post by Bilbo » 23 Jan 2008 13:00

DaleStan wrote:Using a label instead of the while and a goto instead of the continue means I can leave off the whole "while (true) { /*do stuff*/ break; }" thing. Is that insanity really preferable to a goto, Knuff?
Not to mention if the "rare_condition_requiring_restart" would be nested in some for or while cycles, then the continue won't work, as it will "restart" only the inner cycle. Then you'll need either goto or something really horrible to handle that without goto. I guess goto would be fine in that case :)
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)

Boah
Engineer
Engineer
Posts: 15
Joined: 29 Jul 2004 23:47
Location: London
Contact:

Re: OpenTTD Coding Style and Guidelines

Post by Boah » 25 Jan 2008 00:52

Knuff wrote:I hope you all know that using goto produces really really messy and bad bad code.
Really really messy and bad code is produced by programmers, not the goto. Generally people will try to avoid gotos because they can make code harder to read and maintain, but as DaleStan says, occasionally they are the best way to do something.

I have seen an example of throwing an exception as a means of avoiding the use of a goto. Now that is.. let's say "creative".
DaleStan wrote:Using a label instead of the while and a goto instead of the continue means I can leave off the whole "while (true) { /*do stuff*/ break; }" thing. Is that insanity really preferable to a goto, Knuff?
"continue" and "break" are effectively restricted goto statements anyway, so not really that much cleaner even if the example hadn't been this extreme.
When it is not necessary to make a decision, it is necessary not to make a decision - Lord Falkland (1610 - 1643)

audigex
Tycoon
Tycoon
Posts: 1919
Joined: 09 Dec 2007 21:28
Contact:

Re: OpenTTD Coding Style and Guidelines

Post by audigex » 13 Dec 2008 19:56

It's like anything else, use it badly and it screws things up. Use it badly enough and people avoid it when it should really be used.

As an example, many programmers shy away from PHP because there are so many bad, insecure systems made with it. It's not that PHP is bad, in fact you can build things as good or better than any comparable language, but it's a mental thing.

Gotos, breaks and continues are at times the best way to do something. In functions, if I have the choice of an if(condition) break; twice, or a double nested if-else, I'll take the break.

When working as a community, consistent is more important than right.
Jon

Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 4 guests