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

User avatar
ludde
OpenTTD Developer
OpenTTD Developer
Posts: 35
Joined: 07 Mar 2004 09:28

OpenTTD Coding Style and Guidelines

Post by ludde » 23 May 2004 10:00

Coding Style can be found at http://wiki.openttd.org/Coding_style.
http://sf.net/projects/openttd/
Post all your patches and feature requests here.

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

Post by Brianetta » 31 Oct 2005 15:04

What's the style for loops? I'm assuming it's like that of if in the example above. Am I correct?
PGP fingerprint: E66A 9D58 AA10 E967 41A6 474E E41D 10AE 082C F3ED

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

Post by Darkvater » 31 Oct 2005 22:59

Brianetta wrote:What's the style for loops? I'm assuming it's like that of if in the example above. Am I correct?
yes.

Code: Select all

for (i = 0; i != 10; i++) {
  bla;
  bla;
}
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
Graphite
Engineer
Engineer
Posts: 84
Joined: 17 Nov 2005 20:56
Location: Netherlands
Contact:

Post by Graphite » 18 Nov 2005 11:06

I think the style for switch-statements is still missing, which seems a rather important one to me because that's one that can be done in many different ways.

User avatar
Buggi
Engineer
Engineer
Posts: 124
Joined: 02 Feb 2005 19:03
Location: Now in North Dakota

Post by Buggi » 15 Feb 2006 03:14

I'm partial to blocking my code as it was intended...

Code: Select all

if (*v->Blahness() == Blah())
{
  *v->Jiggles++;
}

That way stuff really is "blocked" as it should. Plus the extra linebreak and carriage return doesn't bloat the filesize. :wink:
-Buggi
Programmer and TTD fan...

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

Post by DaleStan » 15 Feb 2006 05:58

I'm partial to blocking my code as it was intended...

Code: Select all

if(a)for(b=0;b<10;i++)while(c){doit();dootherthing();}
And the saved vertical space means that you can get so much more code on a single screen.

This is why we have a coding style. It helps forestall holy wars. And, unless you want to reformat something like 10 MB of source, the current style is the way it will stay.
Even if you do reformat all the code, I still don't think the style will change.
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 » 15 Feb 2006 18:33

Dalestan, I'm sure you could have saved still more space on the semicolon near the end.

Buggi, if the coding style really bugs you, and you can't work with such offensive style rules, you can always use indent with two sets of rules. One to format the code so that you like working on it, and one to change it back to the OpenTTD style before you commit changes/submit patches/whatever.
PGP fingerprint: E66A 9D58 AA10 E967 41A6 474E E41D 10AE 082C F3ED

hertogjan
Director
Director
Posts: 560
Joined: 03 Jan 2006 20:45
Location: Netherlands

Post by hertogjan » 15 Feb 2006 21:22

DaleStan wrote:(...)
And the saved vertical space means that you can get so much more code on a single screen.
(...)
But that is not necessarily a benefit. Books aren't printed over the full width of the paper because it would be hard to read otherwise (instead you will see a "wall of words"). Some people have shown that the ideal width of a line is about 70 characters. If it is longer or shorter, it is harder to read. For the same reason, newspapers use columns.
Making computer code more compact may cause the legibility to decrease. The use of line wrapping and indentation will make the code more readable. Overuse of those things will destroy legibility again. So you have to find some ideal line length.
Your example could be part of a "wall of computer code".

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

Post by Brianetta » 15 Feb 2006 23:54

* Brianetta hands hertogjan a replacement sarcasmometer

Yours seems to be failing.
PGP fingerprint: E66A 9D58 AA10 E967 41A6 474E E41D 10AE 082C F3ED

User avatar
Buggi
Engineer
Engineer
Posts: 124
Joined: 02 Feb 2005 19:03
Location: Now in North Dakota

Post by Buggi » 16 Feb 2006 00:38

I seem to be on the receiving end of most of Dale's anger these days. :(
-Buggi
Programmer and TTD fan...

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

Post by DaleStan » 16 Feb 2006 09:10

I'm afraid I tend to get angry when people suggest that OTTD should lose support for one of my two favourite programming languages.
Or even when I think that people want OTTD to quit supporting that language.

It really didn't help that the first post I specifically noticed was yours was this one. I now see it wasn't intended this way, but my luser-o-meter went off, and first impressions are hard to break. You're doing quite well at forcing me to adjust my opinion (in this case, that's a good thing); keep it up.
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
bobingabout
Tycoon
Tycoon
Posts: 1850
Joined: 21 May 2005 15:10
Location: Hull, England

Post by bobingabout » 16 Feb 2006 10:30

i don't think OTTD should stop support of newGRF, even if what i said makes it saound that way, but i do think the 32bpp stuff might need a bit more of a kick than newGRF supports.
JPG SUX!!! USE PNG!!!
There are times when JPG is useful, TTD screenshots is not one of them. Please use PNG instead.

[/url]

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

Post by Brianetta » 16 Feb 2006 13:20

How would the graphical bit depth affect the "kick" of newgrf?
PGP fingerprint: E66A 9D58 AA10 E967 41A6 474E E41D 10AE 082C F3ED

User avatar
bobingabout
Tycoon
Tycoon
Posts: 1850
Joined: 21 May 2005 15:10
Location: Hull, England

Post by bobingabout » 16 Feb 2006 13:25

because its actually 34bits, because you require a 32bpp image and a 2bpp CC overlay, which means each sprite requires 2 seperate images.
JPG SUX!!! USE PNG!!!
There are times when JPG is useful, TTD screenshots is not one of them. Please use PNG instead.

[/url]

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

Post by Brianetta » 16 Feb 2006 13:29

Still not sure why you need to handle it any differently from the 8 bit graphics. I was under the impression that newgrf was fairly agnostic toward bit depth and graphics format. Anyway, this is dragging a fairly important sticky off topic, and there are already some threads covering this subject.
PGP fingerprint: E66A 9D58 AA10 E967 41A6 474E E41D 10AE 082C F3ED

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

Post by Darkvater » 08 Apr 2006 08:01

*updated starting post with 'coding guidelines'
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."

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

Post by DaleStan » 14 Jul 2006 13:35

I don't see anything mentioned here about using

Code: Select all

enum {
  FOO = 0,
  BAR = 1,
  BAZ = 2,
  END = 3
};
instead of

Code: Select all

enum {
  FOO,
  BAR,
  BAZ,
  END
};
Most of the enums I've seen in the OpenTTD source are in the former format, not the latter. I can see lots of disadvantages to the former, but no advantages, and the use of the former format has caused bugs that would be complete non-issues were the explicit values not present.

Is it just a matter of someone sitting down with grep, removing all the explicit values, and posting a patch on Flyspray, or is there some reason that the enums must have explicit values?
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

richk67
Tycoon
Tycoon
Posts: 2363
Joined: 05 Jun 2003 16:21
Location: Up North
Contact:

Post by richk67 » 14 Jul 2006 13:43

DaleStan wrote:Is it just a matter of someone sitting down with grep, removing all the explicit values, and posting a patch on Flyspray, or is there some reason that the enums must have explicit values?
I think there are occasions where explicit enums are better; eg. in any list where the value is saved (airport heading states being one). However, yes, in general non-explicit enums are better.

The bug you link to is an awkward one for enumeration; not every item in the graphic list has an enumerated value. So the enumerated values jump with fairly large gaps between them. (These correspond to values in station_land.h). It was a typo, nothing more.
OTTD NewGRF_ports. Add an airport design via newgrf.Superceded by Yexo's NewGrf Airports 2
Want to organise your trains? Try Routemarkers.
--- ==== --- === --- === ---
Firework Photography

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

Post by DaleStan » 14 Jul 2006 14:16

So just use the explicit values where they are needed, and leave all the others out. If typos can be eliminated by removing code, then I'm of the opinion that said code should be removed posthaste.

Even if only END loses its explicit value, that acheives most of the desired result, while still making it difficult to break the savegames by inserting a value in the middle of the list.
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

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

Post by TrueBrain » 22 Aug 2006 09:58

Coding Style have been slightly altered. Please do pay attention to them.

Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 3 guests