Skip to main content
GameDev.net gamedev.net
🔒 Locked

Coding-Style Poll

Started by L. Spiro Sep 27, 2016 at 12:40 PM 63 replies 16.6k views
Original Post
L. Spiro
L. Spiro
This is not an actual forum-supported poll because this isn’t about giving you specific options and then seeing which is most popular, but rather I would like to know how you feel about the following case.

Let’s say you joined a company and they have some coding standards to which you must adhere. Right now I mainly want to know how you feel about their style of braces conflicting with your own, but I generalized the topic so that I could ask my follow-up question as well.

Braces
How do you feel about policies that strictly control how you brace your code?
For example, would you prefer a policy that strictly requires all braces to be on their own line, or a policy that defines only where to put braces when declaring a class or defining a function, but lets you use your own style inside functions (which may well be to put them on their own lines)?

To segway into the next section, how irksome is it to have to conform to using a brace style other than your own?
Does it just bother you but you can get on with it, or does it leave a nasty taste in your mouth and make you disgusted at your own code?


Other
I am largely interested in what things you consider most annoying to change about your own style.
For example, maybe it would annoy you a little to have to prefix a class with “C” if you are not used to doing that, but how annoyed would you be if you could not use “m_” for members of a class?

List, in order from most grating to slightly tolerable, things you would hate to change about your own style if you had joined a company that had a coding standard largely in-conflict with your own.


L. Spiro
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
21st Century Moose
21st Century Moose

I prefer Allman brace style but to be honest, brace style, indentation, tabs-vs-spaces, etc - I can live with all of those. Worst case is I run the code through a source formatter after checking it out, then again before checking it back in. Takes a couple of seconds, no big deal and definitely not worth getting upset over.

"m_" and "g_", on the other hand, stop me in my tracks. I mean if I was forced to use them, or if I encountered code that used them. It's not grating and it's not annoying, it's that they genuinely stop me in my tracks and impact my productivity that much.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 
Rattrap
Rattrap

I don't work in an environment where anyone is telling me how my code should look, so I haven't had to play that game. But I can say it does irk me sometimes when reading other coding styles on websites (gamedev, stackoverflow, etc). I can get over it, but it sometimes feels like I have to stare at the code longer. Heck, I think I've been known to reformat other people's code when I post replies.

I like braces being on their own line and to line up vertically with their match. I just find this easier to look at. When it is at the end of the line, it feels like i have to hunt for it.

I used to subscribe to the prefix (C, I, m_), but over the years I just found them annoying. I think as soon as I had a class that started with a C or an I (making a double letter), it just looked weird to me (CClass or IInterface). I will occasionally use an underline suffix when using c# and properties when there is a similarly name variable backing up the property, but I try to avoid it when I can.

I also subscribe to the Pascal (or Upper Camel Case) for naming.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]
Kylotan
Kylotan
I use Allman style when writing my own code - and when I modify existing code, I use whatever style is already there. Personally it doesn't matter to me as long as it's consistent and not ridiculous (eg. 2 blank lines between 'if' and the brace).

I think the only annoying thing would be if a company code standard demanded rigorous Hungarian type prefixes - the more rigorous and varied, the more annoying.
alvaro
alvaro
The guiding principle is that if you are working on an existing project, you try to follow the style you encounter, so your code doesn't clash. Actually, that is the only principle we follow at work, and it has resulted in a consensus style that we are all familiar with. There are parts of the code that don't exactly conform to it, but we don't get our knickers in a twist about it.

For braces, I like this style:
bool is_prime(unsigned n) {
  if (n % 2 == 0)
    return n == 2;
  
  for (unsigned k = 3; k * k <= n; k += 2) {
    if (n % k == 0)
      return false;
  }
  
  return true;
}

So, opening brace on the same line after a space, closing brace on its own line, and no need for braces if the block is a single thing that fits in a single line. This is what I am used to, and I like how it's clear enough and doesn't waste too many lines. I examine code in a screen with limited vertical real estate, so line efficiency is somewhat important.

About your second question, I don't think I could live with Hungarian notation or similar conventions to pollute variable names with auxiliary information. A lot of the code at work does the m_ thing for members, and I have learned to live with it, although it seems to me that classes should be small and understandable enough that it should be obvious what's a member and what's not.

I can't think of anything else that would bother me. But I am probably just not thinking about it hard enough. :)
DvDmanDT
DvDmanDT

I think every project/codebase should have a defined style all the way through.

I'm just sooo happy I work in C# where there's basically one universally accepted naming convention and one almost universally accepted formatting convention. Sure, there are differences between projects sometimes, but the differences are typically negligible compared to many other languages.

Norman Barrows
Norman Barrows
List, in order from most grating to slightly tolerable, things you would hate to change about your own style if you had joined a company that had a coding standard largely in-conflict with your own.

that's easy. its all about keystrokes (data entry speed) and readability. the more unnecessary keystrokes, the more annoying it is. the less readable it is the more annoying it is. typing takes time. deciphering code takes time. time is money. but unlike money, you only have a finite amount of time - don't waste it.

that being said, it sounds like you've been hired to produce code that conforms to certain formats and coding conventions. it would probably be a good idea if you just swallowed the bitter pill and wrote their ugly code for them. and be thankful you can define your own saner conventions for your own projects.

Norm Barrows Rockland Software Productions "Building PC games since 1989"</
mychii
mychii

I'm flexible to what is agreed on and what's already there. Different code style doesn't bother me at all, as long as it is consistent. What's annoying is when it is not consistent. What annoys me is like this:


void foo()
{
    if (true) { // <-- inconsistent braces.
        ...
        if (false)
            do something // <-- no braces gives problem when more than 1 line is needed, and again it's inconsistent.
    }
}

Any prefix, as long as they're consistent, I'm on it, even if it's as silly as prefix "S" for struct. I'll be annoyed if suddenly I see struct without "S" when it is already agreed that we should use "S" for naming structs.

Technically, however, "m_" gave me a problem with IDE. If there's an agreement not to use "m_", of course, as I said, I'm okay with it. But technically, in my experience, "m_" for private properties makes things easier on code completion and realizing on private properties on Visual Studio. Same goes to "g_" or "kVarName". Visual Studio's code completion somehow doesn't make a good order on which is private and which is public, which is global and which is related to the class and order things by that.

But in JetBrains WebStorm with code as sick as JavaScript, it can easily and automatically tell me which is private (semantically) or not, which part of the class or not, even with just a "_" prefix.

L. Spiro
L. Spiro

that being said, it sounds like you've been hired to produce code that conforms to certain formats and coding conventions. it would probably be a good idea if you just swallowed the bitter pill and wrote their ugly code for them. and be thankful you can define your own saner conventions for your own projects.

Opposite.
We are defining new standards and I am on the committee.
As for me, one of my policies is that happy coders are more productive coders, and one way to make unhappy coders is to make them use a style they dislike a lot.
A coding-style guideline should not be overly restrictive, especially unnecessarily. But the boundary between consistent coding and “comfortable dissonance” is not an easy line to draw.

This poll is really as straightforward as it sounds—no hidden motives about getting pushed into a style I don’t like etc.
The more topics I know that people consider “personal” the better I can consider where we should give people a “personal comfort” pass weighed against the clarity/consistency of the resulting code base.


L. Spiro
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
conquestor3
conquestor3

It completely depends on the developers I think... I've worked with devs who I can't understand what's going on until I place in some white space, and I've worked with devs who document everything fine.

The best standard I've used is one where an example of test data for parameters is included in a header for each method that has at least a few returns, so it just "made sense" when jumping into any method.

Paragon123
Paragon123

I can't stand it when braces are not on their own line, it is too easy to miss one when the line is wider than the screen and matching braces by column is incredibly easy.

Class names, and public properties are Capitalized

Parameters camelCase

local variables don't matter as long as they are not capitalized. starting with an _ is also acceptable (to me)

private variables holding the data to a property is the same name as the property camelCased with a proceding _


public class Foo
{
  int _count; 
  public int Count
  {
    get
    {
      return _count;
    } 
    set
    {
      _count=value;
    }
  }
}

The only time i use specific prefix or suffixes are with GUI elements, event handlers and delegates.

User controls are prefixed with an _ and have a suffix indicating the type of control.

Events are suffixed with EventHandler

And delegates are suffixed with Delegate

Tabs as spaces is always better (IMO) than tabs as tabs.

I like to ensure that all parenthesis are explicit (unless the expression becomes unreadable because of it)

I work in .net mostly, switching between VB.net and C#.net, I can not stand VB implicit parenthesis for subs without parameters... myObject.ToString just drives me up a wall compared to myObject.ToString()

When in doubt I try to find it here https://msdn.microsoft.com/en-us/library/ms229002(v=vs.110).aspx

LandonJerre
LandonJerre
Braces How do you feel about policies that strictly control how you brace your code? For example, would you prefer a policy that strictly requires all braces to be on their own line, or a policy that defines only where to put braces when declaring a class or defining a function, but lets you use your own style inside functions (which may well be to put them on their own lines)? To segway into the next section, how irksome is it to have to conform to using a brace style other than your own? Does it just bother you but you can get on with it, or does it leave a nasty taste in your mouth and make you disgusted at your own code?

If I find the policy to be sane, then I don't have a problem with it. For example: I prefer Allman style, and use it where it is possible, but at work we write Java and Javascript code with the Java variant of K&R style, and I don't really have a problem with it. On the other hand if the policy would be to use Whitesmiths style, or something I find equally unreadable, I probably would have found another job already.

Other I am largely interested in what things you consider most annoying to change about your own style. For example, maybe it would annoy you a little to have to prefix a class with “C” if you are not used to doing that, but how annoyed would you be if you could not use “m_” for members of a class? List, in order from most grating to slightly tolerable, things you would hate to change about your own style if you had joined a company that had a coding standard largely in-conflict with your own.

Variable name prefixes basicly make the code unreadable for me. I usually don't read code properly like text, I just run through it, so for variable names usually I only look at the first 3-4 characters, and the length. (I heavily lean on autocompletion during actual coding for this exact reason, I usually don't know the full names of variables, just how they start.) Because of this, if the first characters are consistently the same throughout multiple variable names, I'm forced to read the code like proper text, which is irritating to me, and much slower too.
Class name prefixes while irritating, I can live with them, but they make harder to find stuff for me (again I usually remember the beginning of the class names, and if all classes start with a C, that doesn't make my life easier). I'm fine with anything else that doesn't involve prefixes.

Zaoshi Kaba
Zaoshi Kaba

I don't mind whether brace is on same line or new line, however company I currently work at uses pure-cancer style:


Foo* find (int id)
	{
	for(int i = 0; i < global.size(); i++)
		{
		if(id == global[i].id)
			{
			return global[i];
			}
		}
	}

Notice reverse order inside if() statement. That might have been a problem 2 decades ago but now we have compiler warnings. It looks simply stupid and unintuitive.

There's also a space after function name/call if it contains parameters, but no space if there are no parameters. No IDE in mankind supports automatic formatting for this monstrosity.

In my opinion most important rule when deciding on style is automated formatting. If IDE cannot format your style - it's bad style. Although I do believe braces are better left on the same line. Putting it on a different line doesn't add any clarity - it's not a statement, just formatting, and needlessly wastes vertical space. Indending alone is sufficient to show that "this code belongs to that loop", having separate brace showing same thing is redundant.

Also class/function/method headers. Yet another cancer. I'll sooner die of radiation and chemotherapy at this point.

My company's current style requires to write headers with author name, parameter explanations, etc. I can agree with short function description on what it does and extra information on special cases, but author's name? In BOTH header and code file? It becomes obsolete next day when someone edits it. Then you have 2 different names but neither is the author because someone edited it and didn't change name.

Oberon_Command
Oberon_Command
I can work with most styles - my gripes with code tend to be related to how a feature is implemented not what the code looks like.

That being said, the basics:
- tabs instead of spaces if I'm working primarily in the IDE, spaces if I have to look at my code in other editors often; this should be consistent throughout the codebase.
- tabs equivalent to 4 spaces
- no snake_case - PascalCase, camelCase, and scope_camelCase are permissible.
- no class prefixes. IFoo is permissible, but discouraged.
- either put opening braces on their own line or don't, as long as it's consistent
- closing braces always go on their own line
- braces should be aligned with the scope that holds them, not the scope they define
- all control flow statements must include braces (except switch cases, which are obviously delimited by other control flow statements) to prevent mistakes

These ones are optional, but I like to enforce them on myself anyway:
- try to stick to a maximum line length of 100 characters
- if a line goes past 100 characters, it should be split up
- if a function signature or invocation would be split up to maintain the line length, all arguments to the function should go on their own line.
- do not declare multiple variables in the same declaration
- if a template type is long enough to take up significant space or is difficult to remember (eg. std::vector>>), create a type alias with a more readable and meaningful name and use that instead

Finally:
// don't do this
auto thing = foo();
if (thing) {
}

// do this instead to enforce that the variable 'thing' can only be used if it's valid
// C++'17 will add syntax to make this work for more than just pointer types
if (auto thing = foo()) {
}

// I saw some id Software code that did this and I'm quite pleased with it
// Yes, it's a pain in the ass, but it makes the code SO much more readable for me.
// I would only do this if I had to "pretty up" my code and didn't think it needed to change much.
float    x    = 0.0f;
ThingFoo foo  = ThingFoo::Null; 
l0calh05t
l0calh05t

For braces, Allman over K&R, but I can live with either. But there are variants that are just nasty... like all those except Allman and K&R in the table on I particularly detest the so-called GNU style.

Similar for tabs vs spaces where I definitely prefer tabs (because you can adjust them. for example two space indents are too small for readability IMO but quite common and then you're stuck with that...) but I can live with either as long as its consistent. And as long as the tabs aren't used for alignment (instead of for indentation only)... which is plain dumb.

Now variable and class name prefixes... no, NO, NO, NO! And it appears im not alone with that opinion. Especially the "C" for class. It's pointless and unnecessary. Especially when considering the fact that classes and structs are the same thing (even if MSVC would like to tell you otherwise).

But one thing I would flat out refuse to do: block alignment, e.g.


int             foo                 =       10;
char            barf                =      'c';
SomeStupidClass anEven_StupiderName = xyzabcde;

Waste of time, you can't cleanly add new values etc. Just no.

MagForceSeven
MagForceSeven



How do you feel about policies that strictly control how you brace your code?

I think it's a good thing to promote a reasonable consistency in the codebase. While good programmers can bounce between any particular coding style, it reduces the cognitive load when looking at unfamiliar code and makes it easier to do searches in the code (although this is more true when trying to remember function names or doing searches like "variable = "). If there's going to be a style guide policy for braces it should be for the all the code written within the company no matter where it is (maybe separations for game vs tools code, like no stl in game but in tools it's okay)



To segway into the next section, how irksome is it to have to conform to using a brace style other than your own? Does it just bother you but you can get on with it, or does it leave a nasty taste in your mouth and make you disgusted at your own code?
.

Personally I'm fine with it. Honestly I've probably spent more time coding it styles at work that I had a problem with one way or the other than I have in the style I use for personal projects.



I am largely interested in what things you consider most annoying to change about your own style.

Braces are kind of annoying, but only when first getting used to a new codebase. Indentation policies I've ignored a lot of the time when the policy makes the code less readable (the policy for switch statements at my old job had the switch & cases at the same level which I found an inane way of writing those).



List, in order from most grating to slightly tolerable, things you would hate to change about your own style if you had joined a company that had a coding standard largely in-conflict with your own.

For me it usually has to mostly to do with age of the policies. When I started my last job it had a policy written when C with Classes was how you needed to write reasonable portable code, especially on consoles. It hadn't been updated in quite a while for improvements to compilers or getting the most from C++ or even what had been recognized as C++ best practices. I had a lot of suggestions and input in the updated policies to address these sorts of things when we finally got around to updating the coding standards we used.

--Russell Aasland
--Principal Engineer
--Midsummer Studios
l0calh05t
l0calh05t

I can work with most styles - my gripes with code tend to be related to how a feature is implemented not what the code looks like.

That being said, the basics:
- tabs instead of spaces if I'm working primarily in the IDE, spaces if I have to look at my code in other editors often; this should be consistent throughout the codebase.
- tabs equivalent to 4 spaces
- no snake_case - PascalCase, camelCase, and scope_camelCase are permissible
- either put opening braces on their own line or don't, as long as it's consistent
- closing braces always go on their own line
- braces should be aligned with the scope that holds them, not the scope they define
- all control flow statements must include braces (except switch cases, which are obviously delimited by other control flow statements) to prevent mistakes

Finally:


// don't do this
auto thing = foo();
if (thing) {
}

// do this instead to enforce that the variable 'thing' can only be used if it's valid
if (auto thing = foo()) {
}

Heh. If I'd make a set of coding rules it would be snake_case for most things, PascalCase only for concepts (template parameters) and UPPER_SNAKE_CASE for defines/macros. Because I think it's just not ok to simply ignore how the standard library looks like. (Or avoiding the standard library because you don't understand it which seems to be very popular in C++).

But a big +1 on if(auto thing = foo())! (Although I dislike the space before the if's opening parenthesis.

Another small detail that would make me happy: foo const * x instead of const foo * x. Because I'd prefer consistent right-to-left order over reading in spirals (think foo const * const x vs. const foo * const x).

Oberon_Command
Oberon_Command


I can work with most styles - my gripes with code tend to be related to how a feature is implemented not what the code looks like.

That being said, the basics:
- tabs instead of spaces if I'm working primarily in the IDE, spaces if I have to look at my code in other editors often; this should be consistent throughout the codebase.
- tabs equivalent to 4 spaces
- no snake_case - PascalCase, camelCase, and scope_camelCase are permissible
- either put opening braces on their own line or don't, as long as it's consistent
- closing braces always go on their own line
- braces should be aligned with the scope that holds them, not the scope they define
- all control flow statements must include braces (except switch cases, which are obviously delimited by other control flow statements) to prevent mistakes

Finally:

// don't do this
auto thing = foo();
if (thing) {
}

// do this instead to enforce that the variable 'thing' can only be used if it's valid
if (auto thing = foo()) {
}



Heh. If I'd make a set of coding rules it would be snake_case for most things, PascalCase only for concepts (template parameters) and UPPER_SNAKE_CASE for defines/macros. Because I think it's just not ok to simply ignore how the standard library looks like. (Or avoiding the standard library because you don't understand it which seems to be very popular in C++).

But a big +1 on if(auto thing = foo())! (Although I dislike the space before the if's opening parenthesis.



In my world, UPPER_SNAKE_CASE is ONLY used for macros. Constants are spelled "k_constantName".

I've tried doing Erlang-style in C++ (PascalCase for "variable" bindings, snake_case for everything else) and I think that style belongs in Erlang...
l0calh05t
l0calh05t



In my world, UPPER_SNAKE_CASE is ONLY used for macros. Constants are spelled "k_constantName".

Note that I said defines, not constants! Also constants are regular snake case to me and a loud hell no to k_ ;)

Alpha_ProgDes
Alpha_ProgDes
Depends.

If it's Javascript, I have my opening brace on the first line.
If it's C, C++, C#, Java, I have my opening brace on the next line.

Other than that, I'm pretty flexible on styles. Though tabs/indents less than 3 spaces annoy me.
Beginner in Game Development?  Read here. And read here.  

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.