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

ColdIce
Transport Coordinator
Transport Coordinator
Posts: 261
Joined: 25 Apr 2006 10:22
Location: Bucharest

Re: OpenTTD Coding Style and Guidelines

Post by ColdIce » 15 May 2009 07:31

it`s time for me to learn some c++. right now I don`t know nothing about coding. I don`t even know what program I must use to create a program/patch etc, so can you tell me what program I must use to begin? thank you

I have a book, something like C++ for dummies :P
ImageImage

Roujin
Tycoon
Tycoon
Posts: 1884
Joined: 08 Apr 2007 04:07

Re: OpenTTD Coding Style and Guidelines

Post by Roujin » 15 May 2009 12:33

What OS are you running?
* @Belugas wonders what is worst... a mom or a wife...
<Lakie> Well, they do the same thing but the code is different.

______________
My patches
check my wiki page (sticky button) for a complete list

ImageImage
ImageImageImageImageImageImageImage

User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9295
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: OpenTTD Coding Style and Guidelines

Post by planetmaker » 15 May 2009 12:39

Whatever, you should definitely read that page which answers those questions http://wiki.openttd.org/Development

ColdIce
Transport Coordinator
Transport Coordinator
Posts: 261
Joined: 25 Apr 2006 10:22
Location: Bucharest

Re: OpenTTD Coding Style and Guidelines

Post by ColdIce » 15 May 2009 13:18

@ Roujin : Windows Xp SP3

@planetmaker: yes, I know that page. Right now i don`t want to know from the start how to make a patch, right now I need to know the name of the program(s) where I will put those lines. You can call me idiot, stupid or whatever, I just don`t know even that simple thing for you, but I want to learn, ok? All I need right is just a straight answer and that`s the name of the program.. and maybe an informative link. If you can help me with that please leave an answer.

thank you.
ImageImage

Hirundo
Transport Coordinator
Transport Coordinator
Posts: 298
Joined: 27 Jan 2008 13:02

Re: OpenTTD Coding Style and Guidelines

Post by Hirundo » 15 May 2009 13:42

ColdIce wrote:... and maybe an informative link...
http://wiki.openttd.org/Microsoft_Visua ... s_Editions
Create your own NewGRF? Check out this tutorial!

Roujin
Tycoon
Tycoon
Posts: 1884
Joined: 08 Apr 2007 04:07

Re: OpenTTD Coding Style and Guidelines

Post by Roujin » 17 May 2009 11:03

Yup, that's what I would also recommend if you're on windows.

here's the corresponding wiki page: http://wiki.openttd.org/Microsoft_Visua ... s_Editions
* @Belugas wonders what is worst... a mom or a wife...
<Lakie> Well, they do the same thing but the code is different.

______________
My patches
check my wiki page (sticky button) for a complete list

ImageImage
ImageImageImageImageImageImageImage

User avatar
adf88
Chief Executive
Chief Executive
Posts: 644
Joined: 14 Jan 2008 15:51
Location: PL

Re: OpenTTD Coding Style and Guidelines

Post by adf88 » 05 Sep 2009 10:51

I wrote some time ago a regular expression to find some common coding style mistakes in MSVC using search tool. Probably someone will find it useful (MS syntax):

(:b$)|(^\t* [^*])|([^\t]\t)|(^:b*(([^/*:b]|(/[^/*]))([^/]|(/[^/*]))* )|()((if)|(while)|(for)|(switch))(([^{]\n:b*\{)|(\()))|(XXX)|(^\t*//)|(^\t*[^#\t].*/\*)

With this expression you can find:
- whitespace at the end of line
- tabs that are not at the beginning of line
- spaces before first non-whitespace character in the line, except one space before asterisk "*" (multiline comments)
- some mistakes in comments (I can't remember exactly)
- left brace "{" in wrong place after if/while/for/switch directive
- newline after if/while/for/switch directive without left brace "{" after
- no space after if/while/for/switch keyword
(inspired by Alberths expression):
- "XXX" string
- c comment "/*" after line with text, but not including preprocessor directives (hash "#" at the beginning)
- cpp comment "//" without text before

... and probably something more that I can't remember, I won't analyse this creepy expression till it works well.

If you have any concrete, real suggestions to improve expression - write them here.
Last edited by adf88 on 25 Sep 2009 07:38, edited 10 times in total.
:] don't worry, be happy and checkout my patches

Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4740
Joined: 09 Sep 2007 05:03
Location: home

Re: OpenTTD Coding Style and Guidelines

Post by Alberth » 05 Sep 2009 11:22

I have been using a python program for some time.
Usage:

Code: Select all

python chk_patch.py <name of patch-file>
Your checks are somewhat different, maybe they can be merged in some way. (Feel free to do so and post an update.)
Attachments
chk_patch.py.txt
Python file to check patch files for coding mistakes (drop the .txt extension).
(1.47 KiB) Downloaded 270 times

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

Re: OpenTTD Coding Style and Guidelines

Post by DaleStan » 05 Sep 2009 13:17

adf88 wrote:(:b$)|(^\t* [^*])|([^\t]\t)|(^:b*(([^/*:b]|(/[^/*]))([^/]|(/[^/*]))* )|()((if)|(while)|(for)|(switch))(([^{]\n:b*\{)|(\()))
Are you quite sure that's not line noise? :p

(Sorry. This isn't a suggestion. I'm just impressed.)
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
adf88
Chief Executive
Chief Executive
Posts: 644
Joined: 14 Jan 2008 15:51
Location: PL

Re: OpenTTD Coding Style and Guidelines

Post by adf88 » 05 Sep 2009 13:23

DaleStan wrote:Are you quite sure that's not line noise? :p
I know this looks like ASCII-spaghetti :p but works well and I don't have to touch it.

BTW, my expression can find a lot of code that brakes coding style rules in whole OTTD project. In the current trunk search tool found 757 lines. More then 50% of them is not properly formatted code. The rest is some exceptions to coding style rules.
:] don't worry, be happy and checkout my patches

User avatar
adf88
Chief Executive
Chief Executive
Posts: 644
Joined: 14 Jan 2008 15:51
Location: PL

Re: OpenTTD Coding Style and Guidelines

Post by adf88 » 26 Sep 2009 11:04

I updated coding style wiki, indentation has been inconsistent. One time it has been made with tabs, another with two spaces, another with three spaces... some snippets have had one space before each line ...

Now it's the same as in properly formatted source code.

I have a proposition - maybe replace all tabs with:

Code: Select all

(space, right arrow, space)

and spaces with:

Code: Select all

·
(middle dot)

so people could see exactly which, where and how many whitespace characters are used?
:] don't worry, be happy and checkout my patches

User avatar
adf88
Chief Executive
Chief Executive
Posts: 644
Joined: 14 Jan 2008 15:51
Location: PL

Re: OpenTTD Coding Style and Guidelines

Post by adf88 » 30 Mar 2011 07:01

What about reference template arguments? i.e.

Code: Select all

template <const int &Tvar> ... 
Are they considered as not supported by some compilers thus forbidden?
What are the guides for variable/function/member references as template arguments?
:] don't worry, be happy and checkout my patches

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

Re: OpenTTD Coding Style and Guidelines

Post by Korenn » 08 Oct 2011 20:09

The link is broken, it links to http://wiki.openttd.org/Index.php/Coding_style

where it should link to: http://wiki.openttd.org/Coding_style

url rewrite, I guess ;)
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.

xtifr
Engineer
Engineer
Posts: 4
Joined: 26 Jun 2013 06:07

Re: OpenTTD Coding Style and Guidelines

Post by xtifr » 21 Jul 2013 00:43

I wish to register a complaint. I wish to complain about this parrot, what I purchased not half an hour ago from this very boutique....

Oh, wait, sorry, wrong sketch. No, my complaint, which I freely admit may be too little too late, is about this portion of the style guideline:
Global variables are preceded by an underscore. ("_")
This is a violation of the relevant ISO standards, which say that identifiers which start with an underscore are reserved for the implementation (compiler and standard libraries) at the global scope, and that identifiers which start with an underscore followed by a capital letter are reserved everywhere. Application programs are not allowed to use these names for globals.

Now, I admit that you've gotten away with it so far, and that fixing it at this late date may be more trouble than it's worth, but I think you should at least know that you're essentially relying on undefined behavior here. The compiler is perfectly within it's rights to translate every reference to any such global into system("/usr/local/games/nethack").

(In fact, one early version of gcc actually did that last with any #pragma it saw, simply to emphasize that a 100%-compiler-dependent "standard" feature gives you no guarantees of anything. I admit that it's unlikely that they'd do the same with identifiers that happen to start with underscore, but they could do so and still claim to have a standards-compliant compiler.)

Anyway, I guess I don't really object to you violating the international standards for the language, but I think it's important that you know you're violating the international yada yada. So, consider yourself informed. What you do with that knowledge is up to you.

references:
* Bjarne Stroustrup, The C++ Programming Language, Third Edition, Addison Wesley, 1997, pg 81.
* Ray Lischner, C++ in a Nutshell, O'Reilly & Associates, 2003, pg 3.

cheers

User avatar
SciFurz
Engineer
Engineer
Posts: 78
Joined: 13 Oct 2018 16:33
Contact:

Re: OpenTTD Coding Style and Guidelines

Post by SciFurz » 09 May 2019 18:14

It would be nice to see an explanation of the purpose of each filename type (on the development pages), namely what goes in/what use are the _base, _func, _type, _cmd, _gui, etcetera files.
Some are pretty obvious like _cmd and _gui, and of course .h and .cpp, but the other ones are not so clear.
Tinkering in the code in between writing mostly naughty stuff.
See https://www.patreon.com/scifurz

User avatar
orudge
Administrator
Administrator
Posts: 24028
Joined: 26 Jan 2001 20:18
Skype: orudge
Location: Banchory, UK
Contact:

Re: OpenTTD Coding Style and Guidelines

Post by orudge » 16 May 2019 09:44

If you haven't seen this guide, it's very interesting and worth a read.

User avatar
SciFurz
Engineer
Engineer
Posts: 78
Joined: 13 Oct 2018 16:33
Contact:

Re: OpenTTD Coding Style and Guidelines

Post by SciFurz » 16 May 2019 20:46

orudge wrote:If you haven't seen this guide, it's very interesting and worth a read.
That looks very useful, will be digging through it in the coming days.
Tinkering in the code in between writing mostly naughty stuff.
See https://www.patreon.com/scifurz

Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 4 guests