Coding-Style Poll

Started by
63 comments, last by BBeck 7 years, 6 months ago
I find braces not on their own line very hard to read. Believe this is K&R style?

for(int i = 0; i < 10; ++i) {
       doLoopyStuff(i);
}
My brain just cannot process that. I can only handle:

for(int i = 0; i < 10; ++i)
{
    doLoopyStuff(i);
}
Professionally, I fit in with what others do but I'm fortunate that my current colleagues are "own-line-brace" guys too, as I would be at a major disadvantage if I had to write and follow code like my first example. But I suppose I'd just adjust to it.

My colleague has a very weird habit that I've adjusted to now:

class Foo
{
public:
    virtual
    const int*
    method() const;
};

const int*
Foo::method() const
{
}
End of the day, I hated this when I first saw it, but within a week or so I'd adjusted and I now flip between "work code" and "home code" without any problem whatsoever.
Advertisement

> (this applies to everyone posting here, as many have explained what they like to do but not how they would be bothered going against that)

I think it's difficult to answer that as an abstract question.
Generally I'm not that bothered, there are things way more important than where braces and upper case letters go.
But it depends on the specifics. Mostly I don't like the coding standards where I work but I accept them and it doesn't really bother me.
But there are just a couple of things I find very hard to comply with :P
I do though, and don't get upset about it.

I think I'd be more productive with the style I'm most comfortable with, but as long as the IDE can support the style properly, it shouldn't be too much of a problem.

For example, I use ReSharper which automatically reformats code on paste, semicolon and closing braces. If I can configure it to match your style, then I can work with it. Same thing with naming, if I can set it up to detect incorrect naming etc, then it's probably fine. It needs to work well with intellisense though. For example, if there's a member named m_someProperty, intellisense should be able to list it when I type m_s.., som.., sp.. (acronym) etc. As long as you have VAX and/or ReShaper, that's typically fine.

Over the years I've become a lot more tolerant of different coding styles, I've changed my own style several times, and I've been involved in defining code styles a number of times.

Currently, the client I'm working for has automatic code formatting inside the IDE for every save (something based on CLang-format), which I find really nice because there are no more discussions on style, it just happens.

In the past, where there's been contention, the decision process has been quite simple:

  1. Is there a rational argument against the style being introduced? If I can't come up with a convincing one very quickly, then I dismiss my own reservations and just adhere to the new style.
  2. Is the change important enough to warrant me spending additional time building up a data-supported argument? Often when balancing the cost of me doing so against the aggravation of having to follow the new style, the new style still wins out.
  3. So there's a rational argument and I care about it enough to dig deeper: I write down the argument in technical terms, with supporting data where possible. I present this argument to the team responsible for the coding style. They make the decision, and I abide by it.

It is often a case of "pick your battles" - do you really want to waste time and energy arguing code style, or are there more important things to do?

Finally, in my 20-odd years as a developer (damn, I'm getting old) I don't recall any truly insane styles in any project or place of work. If a bunch of rational developers work out a decent coding style, I think everyone should be able to "get on with it".

It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.

An important thing in any coding style IMHO is naming. We've currently have a bunch of issues because of that. Let me give you an example:

There is a very "quite" fight between using "num" or "count" prefix/suffix for, describing a variable/functions that is counting something:


int numPoints = foo.getNumPoints()

vs


int pointsCount = foo.getPointsCount();

and:


enum Colors
{
   Orange,
   Blue,
   White,

  NumColors VS. ColorsCount
};

I was a "num" guy, but in order to simplify searching i now use "count" as it is a suffix and it easy to find realtions to that count (it is easier to find all code that is all abount points if everything is named like this getPoints*())

Additionally you should think about similar cases that happen in your company. In my case we internally have an issue with those pairs identifiers "cycle/step", "voxel/core", "node/object" ect.

I have my preferences, of course, and when I'm in control of the code those are what I use.

When I'm not in control, I have my pet peeves, but honestly what I care about is that whatever style is chosen is enforced by tooling (and in general, there should be an escape hatch when the situation arises that a particular bit of code is better formatted elsewise). I want for the chosen format to not be overbearing with details, I want for hard rules (e.g. breaks and spacing) to be enforced before or by check-in, and for soft-rules (e.g. naming conventions like numWidget vs. widgetCount) to be flagged as part of code-review.

Without tooling to help it be enforced, any convention is just entropy and impending technical debt. without tooling, you'll eventually reach a point where you decide to pay the debt down (and had better apply tooling as a first step), or you'll acquiesce to accepting that the code will never conform and that future conformance will be best-effort. In general, without enforcement, coding standards will be the first thing to suffer under a deadline.

throw table_exception("(? ???)? ? ???");

@L.Spiro I think most of us are interested in the results. So I just wanted to ping you about that. :)

Don't write a coding standard. Ever. People who write coding standards should be lined up against a wall and SHOT. Over and over and over and over again until everyone, everywhere forever stops writing them.

Why?

Because once you've put cursor to Word document[1], for the rest of time you have to boringly enforce it and have it boringly enforced on you.

Companies are very keen on coding standards and it wastes MASSIVE amounts of time. You spend ages messing about in code reviews with people going "oh, this needs a space here and two there and this line is one character too long (and fixing it will involve you hand re-formatting two subsequent lines..)" and everyone's smug about this because it feels like work and it's BLOODY WELL NOT WORK and then later on the review someone says "Oh hey, why don't you just use this API over here" and you throw away all that code and all the effort in fiddling the spaces into place.

Code layout standards are the sort of thing you need if you have either a lot of spare software engineer time you need soaking up or the kind of engineers better deployed on the task of counting spaces in code than in actually writing it. The first option is unlikely and the second is an environment to leave soon anyway.

Go and write an automated nitpicker / code autoformatter / automated nitfixer instead. Preferably by using an existing one wrapped up with some flags describing the selection of options made in the tedious meetings. And very preferably the latter two[2].

Now you a) don't have to enforce it and b) people don't spend their lives having to try and conform to it. A quick command and now you get nicely formatted code and you're having the machines doing the boring scut work. Which is supposed to be the business we're in but everyone seems to lose sight of that when it comes down to space-counting, bracket-lining-up OCD.

[1] Usually, ironically, a very poorly laid out one which sets off my typographical sensibilities but which for some reason can never, ever, ever be changed[3].

[2] An ex-colleague was horrified when my approach to getting frankly stupid quantities of silly, niggly complaints out of an automated Java style-checker was to write a python script which ingested its complaints, parsed them and ran regexes over the source to fix them. I considered this a set of problems which were hence solved for all time. He seemed to consider it cheating. I wasn't doing the penance properly and never would learn how to write compliant code. So I named it after him...

[3] I recall one such document for C++ which required that any condition must be compared to a boolean. So you had to write "if ((x == y) == true)". This was because once someone had had some code written without it which "didn't work". Given the skill background of some of the other "engineers", I suspect they were just a muppet. But despite no record of the incident, no repro, no witnesses or no sense for the rule I still could not get the committee to remove it. On the basis that "you just never know, do you?". Once you start down these routes, you're in a world of giving the Bs a mechanism to treat the As like Cs and soon you don't have any As.
Here endeth the lesson.
The reason why I'm flexible with any coding standard and has to be extremely consistent with it, is because it won't waste time at all once I get used to it. And since all the code looks just the same, it's as if it's me that writes that code. I don't know. Harmony?

What wastes my time is when nobody wants to setup a coding standard for a project and start creating inconsistent coding mess cause everyone wants their own style and start screaming about it in the middle of the project.

Try zero coding standard on JavaScript and you'll be surprised how many different implementations there when defining a class.

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)?

When I'm working for another company my mind-set changes - this means a lot of discipline (in my mind) comes with coding for a company

It thus means I have accept and do a lot of shit I don't like. And there are usually plenty of them. Coding style/standard (particularly braces) is really a minor one of all things to annoy me. If that's the only problem I've got working for a company I'ill consider myself really lucky! :D

Though Initially it could be really annoying and hard to get used to - but what isn't? So after a while I get used to it

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.

1.Being forced to write too much comments in a code is by far the most annoying thing for me. It really gets on my nerves as it disrupts the flow a lot of times. This was the toughest thing my so-called "disciplined mind-set" had to get used to

2. the next was overly long names, but I came to see the good in this as time went on and i adopted the style for my personal projects

3. I felt some error checking/test code every bit lines was slightly over-the-top and unnecessary

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

This topic is closed to new replies.

Advertisement