views:

423

answers:

7

If it is possible to auto-format code before and after a source control commit, checkout, diff, etc. does a company really need a standard code style?

It feels like standard coding style debates that have been raging since programming began like "put the bracket on the following line" or "properly indent your (" are no longer essential.

I realize in languages where white space matters the diff will have to consider it but for languages where the style is a personal preference is there really a need to worry about it anymore?

A: 

It is always nice if the coding style that the shop uses is the same one that is also followed by the development tools.

Otherwise, if there is a large body of code that already follows a shop standard which is NOT the same as that of the tools you have two choices:

  1. Modify all of the code to follow the tool standard, or
  2. Maintain the existing shop standard.

Many shops do the latter. Regardless, there does need to be some kind of standard, and it does need to be followed.

Some development tools allow you to tweak their standard. In some cases you may be able to bring the tools in alignment with the shop standard.

Robert Harvey
Dev tools these days are typically configurable so that you can customize the coding style. Therefore you would make the tool use your shop standard.
Robin
+2  A: 

No, not really.

If you can actually get it to work consistently and not make it flag code has changed due to a different style of laying the code out.

However, this is just a small part of coding standards. It won't cover multiple return statements, the use or not of ternary operators, etc.

Garry Shutler
A: 

It probably doesn't matter that much anymore if you can ensure that everybody in the team sees the source code "correctly" formatted, whatever they think it is. However I've not seen a system that can do that - you can do parts of it (say, reformat before and after checkin/checkout) but these days you also have to consider web interfaces into the version control, external code review systems that interact directly with the version control system etc.

The main purpose of a standard code style is (IMHO) to ensure that you can read other team members' code easily without having to start reverse engineering it because all the code is written using the same sort of guiding principles. Indenting and parentheses placement seem to be a major hangup on this but they are only a very small and in my opinion, somewhat overblown and not very important part of the need to make code consistent.

Unfortunately I'm not aware of any tools that can automatically apply consistent coding principles to source code...

Timo Geusch
+4  A: 

Auto-format can really only address whitespace.

It wont address developers giving variables bizarre nonsensical names. It won't address some developers having functions return null on an error vs throwing an exception.

I'm sure others can think of more examples.

whatsisname
So would it be reasonable to say that we could stop including whitespace in our style guides, and only include issues which have real semantic meaning, like "Is it okay to throw an exception for flow control"?
dennisjtaylor
+2  A: 

This is what we do at my work:

We all use Eclipse. We don't have a policy for using Eclipse but somehow none of us is an IDEA/IntelliJ guy. We also think our code should be written with legacy in mind. This means our code has to be readable in a certain way even years after (#1) no matter who wrote it and if that person even is in the company anymore.

Eclipse has couple handy features, automatic format on save and a specific Formatter tool. As you can see from the linked screenshot, it can be configured with XML. Thus there's a bunch of premade XML:s available for every worker in our company so that when a new guy comes in, we walk him through of the whole process and configure their Eclipse for them (yes, it's slightly evil thing to do) so that it actually uses those formatting XML:s we have provided. We do not enforce automatic format on save, we don't want to be completely intrusive, we just want to push all our developers into the right directions. For even increased compatability, we mostly use rules defined in JCC.

Next comes the important part, the actual builds. We are those who embrace automatic builds and for that we use Hudson Continuous Integration Server. There's two important parts in our configurations beyond this:

The Checkstyle Plugin is the magician in our code style enforcement guide line:

  1. After commiting code to CVS, Hudson build is triggered
  2. After build has been completed succesfully (all unit tests pass etc.), Checkstyle inspects the actual source files
  3. Checkstyle ranks the code based rules we have defined for it
  4. Continuous Integration Game sees the result of Checkstyle and awards/takes away points for the person who has the ownership for the relevant part of the code
  5. Leaderboard shows total points for every commiter in system

Basically this means that when anyone commits ugly code into our CVS, our build server automatically reduces that person's points.

This means that eventually any one of us can be ranked on the Leaderboard based on the general code quality in both look and OO principles such as Law of Demeter, Cyclomatic complexity etc. etc. Naturally this isn't a completely serious statistic, but it's a good indication you're doing something wrong when causing a build to be initiated in our CI won't reduce your points - most of our commits are worth between 1 and 5 points.

And is it working? Sort of, I don't think anyone of us at my work writes ugly or unmaintainable code and personally I love to hunt all kinds of scores so it's definitely motivating me to make code that looks nice and follows all the OO paradigms I know of.

And do we as a company really need it? I think we do as you should see from reading this entire answer, it can be considered a good practice for the advancements it brings.


#1: in a related note, I refactored legacy code from 2002 today which used those standards, didn't look "bad" at all even in its original form and certainly not worse in its new form

Esko
A: 

Yes, coding styles are needed if there is a desire to have a homogeneous code base. Such a code base can be useful in preventing individual ownership of parts of the code base, which can cause problems when people leave the team. If you can't imagine having wildly different styles and problems understanding all of it, just look at all the different ways English text can be organized in various communications, all written but quite different such as tweets, e-mail, text messages, IM, message board posts, etc. and changes in fonts, capitalization, decorations, etc.

JB King
A: 

Autoformatting on save is supported for java code. Is there a similar support for c code. For c/C++ code it seems to me that it only supports removal of trailing whitespaces. Is there a way I can incorporate that feature in eclipse.

Please advise.

kumar