tags:

views:

1136

answers:

7

Years ago when I was starting a small development project, the other developers and I sat down and agreed on a compromise brace and indentation style. It wasn't anybody's favourite, but it was something that nobody really hated. I wrote a .indentrc configuration file to that style, and had a check-in trigger that would run indent on every file as it was being checked in. That made it so that it didn't matter what style you wrote your code in, it would end up being the group standard before anybody else saw it. This had the advantage of consistency. But I've never seen anybody else do it this way before or since.

So what say the rest of you? Great idea, or abomination?

+3  A: 

That sounds like a good idea. As long as the style you end up with is not something completely strange, that's a good way to make sure your developers use the style. And it has the added benefit that they don't have to code that way - it will get reformatted for them when they check in their changes. It would be nice to have a tool like that available that you can plug into your CVS (generic term).

Elie
"CVS (generic term)" -> VCS/SCM?
hangy
Code Version System, but not any particular one.
Elie
+12  A: 

I'd say good idea. I'd take it a step further and make everyone use the config file in their IDE so that they were writing in the agreed upon style by default. If they're going to have to look at everyone else's code in the neutral style, they might as well get used to it. Even their own code should be in the neutral style after one check-in check-out cycle, so why develop new code in their own personal style?

Bill the Lizard
+4  A: 

If you limited it to enforcing style on braces and indentation, then I think it's a good idea. However, if you were to try to enforce every single formatting standard, then it probably wouldn't be. In my opinion, there are times when it makes sense to break the standard. For example, I prefer

int x = y * z;

to

int x = y*z;

because it's easier to read. However, I vastly prefer

int a = b*c + d*e;

to

int a = b * c + d * e;

because the spacing represents the order of operations.

So your policy of enforcing indentation and braces sounds really good. But if someone ever tried to blindly enforce other spacing rules, I don't think it would work well.

Eli Courtwright
As long as everyone agreed to an acceptable standard, I don't see why this couldn't be included as well as brace style and indentation.
Bill the Lizard
I like OpenBSD's idea on that. If you have to sacrifice readability to keep with the general standard, then don't do it. Readability is why the standard really got made in the first place. However, this should be the exception -- not the norm.
Nazadus
@Nazadus - I like that insight
Draemon
+2  A: 

We use TFS with a check in policy that runs a set of Stylecop rules. If your code doesn't pass, you can't check it in. Works very well indeed. Aside from a consistant style and good commenting throughout, it also seems to have increased the general quality of the code - perhaps because the developer is forced to describe what every method, event, etc does they are forced to think about the code more before check in.

Only an MS solution, but worthwhile if it's available to you.

Daniel M
The same can be done with Java technologies for sure, and I'm reasonably confident there are Open Source tools available to perform the same in C/C++. Don't know about the newer scripting/dynamic languages.
Ken Gentle
Some tools like checkstyle allow you to do something similar in java
Mario Ortegón
+9  A: 

Adopting a neutral coding style is definitely a good idea. However, only enforcing the coding style when the source is checked in may or may not be a good idea (see also Bill's and Elie's answers below).

Using the check-in hook:

Pro: Allows coders to write however they wish, so they don't have to think about the standard or change the way they write code. This minimizes resistance to the policy, and won't negatively impact their productivity when writing code.

Con: Your coders may have only a passing familiarity with the neutral style, so you aren't getting the full benefit of everyone using "the same" style. If your programmers ever have to work together in a pair programming setup, they are still going to be subjected to one another's programming style on the screen, which is going to be different from their own style or the neutral style.

Going one step further, using the neutral style during development:

Pro: Encourages fluency in the neutral style, everyone can always read everyone else's code before and after it's checked in.

Con: You'll encounter more resistance from your developers doing it this way. Depending on your culture, it could be more trouble than it is worth.

Adam Bellaire
+1  A: 

The biggest problem with using automatic code formatters is when the code formatter can't handle every scenario.

For example, if you have lots of SQL in your code, you probably format the SQL automatically. But if your SQL is longer than one line (how long is a line, anyway?) then you have to format it. So far I've yet to see a good formatter than can deal with this properly.

Example:

String sql = "SELECT * FROM USERS WHERE ID = ? AND NAME = ? AND IS_DELETED = 'N'";

vs

String sql = 
  "SELECT * " +
  "FROM USERS " + 
  "WHERE ID = ? " + 
  "  AND NAME = ? " +
  "  AND IS_DELETED = 'N'";

The second format is more readable when you have really long queries. Most formatters would de-format that into one long line up to the line length.

However if all you're doing is turning

if(x=1) print("blah"); else print("eep!");

into

if (x = 1) {
  print("blah");
} else {
  print("eep!");
}

then the formatter is ok. We do something similar at work; it isn't enforced by the CVS tool but rather by the IDE. Works reasonably well.

Mr. Shiny and New
Your IDE should also warn about using = in a condition. :-) For example, GCC with warnings turned on would insist that you write "if ((x = 1))" to signal your intent to actually use = instead of ==.
Chris Jester-Young
enh, maybe it's not C or Java code but some other language that uses = as the comparison operator :)
Mr. Shiny and New
A: 

I believe you have decided on your development environment by now. If you use Eclipse you can enable the Format Source save action on the Java editor, which reformats at every save. The major benefit from this is that source chances are marked in your source repository at the time when they were done, not when the source was reformatted later.

Make it an automatic step. You will appreciate it later.

Thorbjørn Ravn Andersen