Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've used to write code in C-like languages using the Allman style, regarding the position of braces.

void foo(int bar)
{
  if(bar)
  {
     //...
  }
  else
    return;
  //...
}

Now the last two years I have been working mostly in JavaScript and when we adopted jslint as part of our QA process, I had to adopt to the Crockford way of doing things. So I had to change the coding style into:

function foo(bar) {
  if (bar) {
     //...
  } else { 
    return;
  }
  //...
}

Now apart from comparing a C/C++ example with JavaScript, I must say that my JavaScript-Crockford-coding style now has spread into my C/C++/Java coding when I revise old projects and work on code in those languages that for example has no problem with single line statements or ambiguous newline insertion.

I used to consider the later format very awkward, I have never had any problems with adapting my coding style to the one chosen by my predecessors, except for when I was a Junior developer mostly being the solve developer on legacy projects and the first thing I did was to change the indenting style.

But now after a couple of months I consider the Allman style a little bit too spacious and feel more comfortable with the K&R-like style.

Have you changed your coding style during your career?

share|improve this question
7  
What's the problem with such a minor thing as just a coding style? Teach your IDE a variety of styles and let it take care of it. It's not a programmers business to pay attention to a coding style. I'm concurrently contributing to a number of open-source projects, each with a different coding standard, and I'm just switching Emacs settings when switching projects. –  SK-logic Feb 12 '11 at 13:22
    
I went through the same exact same transition when I went from C to Java. No going back from the K&R style now. –  Alb Feb 12 '11 at 13:59
    
@SK-logic: I use Yegge's js2-mode in Emacs, but not everyone uses Emacs. I did spend a lot of time setting up Eclipse indentation style but gave up (buggy), so I ended up patching einars jsbeautify to be used as a JS formating tool in our repository. Anyway, I know that there are solutions out there so I was more referring to my personal habits. –  Ernelli Feb 12 '11 at 14:36
    
Agreed, this is something that an automatic formatter needs to take care of every time the file is saved. –  user1249 Feb 12 '11 at 17:27
    
I still find overly-compact code hard to read. I don't know why people advocate K&R bracing, but not something like for(i=0;i<10;i++). It's more compact, right?!? –  Almo Jul 11 '12 at 19:27
show 2 more comments

closed as not constructive by maple_shaft Mar 17 '13 at 1:55

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

10 Answers

Yes, Early in my career I used to indent the braces as well, needless to say code got a little too wide sometimes and when I discovered Visual Studio formatting and it (wisely) didnt' support that I dropped the habit.

I really hate working with the Crockford style though since IMO it makes code less readable structurally and makes it easier to overlook braces.

share|improve this answer
add comment

I've changed a few times over the years.

Changing style is very natural when you're starting with a new language - you're learning lots of new idioms anyway. To a lesser degree, the same thing applies with different projects.

Also, I find my preferred style changes gradually over time anyway.

But some things I find very difficult to override. Like in bracey languages, with a multi-line block, I always have the open brace on the next line. Whether the braces are indented or not isn't a big deal, but if the open brace is on the end of the "while" or whatever line, it just looks so horribly wrong it distracts me from other issues.


EDIT - Looking at this again, I realise that actually over the last couple of years I've got used to Egyptian braces. I still don't use them (I've not been in a position where I had to), but they don't bug me in other peoples code any more.

I don't know what changed. It's not as simple as familiarity - I had plenty of time for that before.

In the long term, I guess almost anything can change.

share|improve this answer
add comment

When I started programming in C, 25 years ago, K&R was the C resource, and I adopted its coding style. When I first saw the "ANSI C"/Allman style, it just looked wrong to me -- way too much wasted space.

For several years, while maintaining code, I adopted whichever style the original code was written in, but continued to write original code in K&R style. As time went by, I found the ANSI C style more readable and easier to maintain, and started writing original code in the newer style. I even adopted the practice of using braces for single line if and else clauses.

Now the K&R style looks wrong -- just too busy -- and when I have to maintain code written in that style I cringe a little -- but leave it alone.

So yes I changed -- and it only took 20 some years.

share|improve this answer
add comment

It's almost impossible to change. You need to get this stuff nailed down in stone right away in any new project/team. That's why the first chapter of "C++ Coding Standards", but Sutter and Alexandrescu, is called, "Braces on the next line, always!" rather than, "Don't sweat the small stuff."

Wait...

share|improve this answer
add comment

I find it hard to change. I've used your Allman style (with indented braces in compound statements as well - aka the GNU style) - and find everything else difficult, if not impossible to read.

The other styles make me work much harder to figure out whats going on. Then the irritation level starts to rise, and then I get really grumpy. Changing to I something consider inferior is not something I want to do!

share|improve this answer
add comment

I have no problem adapting my style depending on the context.

I work in c#, javascript, css & html everyday so my coding style changes depending on the context. When in c# i use the MSDN styles (enforced by tools as well) and then when i open a JS file i use the crockford style.

Being able to switch styles is something i think its important for a programmer to be able to do. When maintaining old software you need to be able to use the style that it was originally written in not the style you are currently using.

As others have said, make the tools work for you and use them to fix the styles where possible.

share|improve this answer
add comment

I've changed my personal style slowly over the years. I also have to adapt to the coding conventions established at work and in the various outside-of-work projects I contribute to -- all of these differ slightly from what I would prefer were I to have complete control over the stylistic choices.

It's usually not so bad, and even when I lose track of which context I'm in the IDE can get it mostly-corrected for me anyhow with a single keystroke.

share|improve this answer
add comment

I changed to the Eclipse formatter default configuration which turned out to be so beneficial as it can be automatically enabled as a save action that it was not hard to make that permanent.

Before that I played with very wide lines (using a proportional font) and other goodies, but the killer feature was that code formatting changes took action immediately instead of long time later. This allowed to see who did the actual change in the code repository.

share|improve this answer
    
Huh, change management tracking implemented by coding style analysis, wild! Anyway, the automatic formatting introduced by various IDE's really make your changeset's larger than necessary if the developers in a team has different preferences. Thats the reason why we began to autoformat all code using scripts prior check in. –  Ernelli Feb 12 '11 at 19:05
1  
@Ernelli, thats why agreeing on the default configuration is a good thing. We try to keep things simple. –  user1249 Feb 12 '11 at 19:44
add comment

My personal projects are riddled with coding-styles explorations. I find it quite stupid to pick a style at random and then stick with it no matter what, so I just explore my options. In fact I may have a single file with various styles, that accumulate over the editions, which has the bonus to allow me to quickly check them against each other.

Normally it doesn't leak into my daily work though, nor does it leak when I edit a file subject to a defined convention (open-source). I tend to maintain consistency when it's not for my sole review, because it's more agreeable to/less distracting for the reader.

As for the speed of change, I do have some inertia. So generally it's not a radical change, but more a gradient. Of course the more I'm writing and reading in the new style the more quickly it settles.

share|improve this answer
add comment

Yes, I've changed it some time ago.

I usually write in C#/Java with a bit of C however after I looked to functional programming through F# and Erland I realised that I don't write code like this (even in C#/Java):

void DoSomething(SomeParameter parameter) { ... }

It started to look very unsuall for me and instead of this I started to prefer immutuable types and wrote my code in the following way:

SomeParameter DoSomething(SomeParameter parameter) {...}

Now this functional fever is gone and I try to combine both approaches when appropriate.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.