The Artima Developer Community
Sponsored Link

Weblogs Forum
What's the Most Effective Code Style Policy?

43 replies on 3 pages. Most recent reply: Jul 25, 2007 5:37 PM by Erick Reid

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 43 replies on 3 pages [ « | 1 2 3 ]
Bill Venners

Posts: 2242
Nickname: bv
Registered: Jan, 2002

Re: What's the Most Effective Code Style Policy? Posted: Jul 17, 2007 2:13 PM
Reply to this message Reply
Advertisement
> ASCII art code is a very bad idea for two reasons:
>
> 1) Some developers like to use proportional fonts, and to
> them, the code will be hard to read (which is exactly how
> the code appears in the forum, by the way).
>
I hadn't thought about that.

> 2) The slightest refactoring will mess everything up,
> forcing the user to do manual editing of the refactoring.
> Which is exactly what refactoring is supposed to avoid.
>
And I hadn't thought about that either. Thanks. I was thinking that it would be difficult to get other programmers to adhere to the style, but we do auto-refactoring a lot, and those tools also wouldn't adhere.

I have been watching the discussion and considering this issue. I think it is good to have a code standard for a project that has multiple people working on it. I liked the suggestion of several people here that the standard come from the group itself. I think that would help buy-in. I also really liked that Sun had published a code style guide for Java, because instead of fighting over what our code style standard would be, we could just point to that document. (But deciding to use that document was really a group decision.)

We found, however, that there were style differences that arose that were not covered by that document. In that case several here have suggested that we just not worry about it, as these things really are minutia. I agree they are minutia, but I've been thinking it would be good to have a policy on such things--just don't worry too much when people make minor infractions. If you find a file in which someone made a minor (or major) deviation from the style standard, just fix it, test it, and check that in before making any other changes to it. Several people pointed out here the problem with intermingling formatting changes with real changes--it is harder to find the real changes in diffs later. So a rule should be to check in any formatting changes before making real changes to the file.

I also have been coming to the conclusion that it is helpful if there's a tool that can reformat code to the standard rather than having people do it by hand. Once you get the bugs out of that reformatter tool, it will be more reliable than hand-reformatting. One problem I have heard about, which I don't think anyone mentioned here, is that when you have people just reformatting code that isn't heavily covered by unit tests, that act can introduce bugs. After the reformatting, the code may be prettier, but it also may be broken. Such reformatting tools exist for Java, but it implies that the standard we decide upon needs to be algorithmic. In other words, it isn't good enough that the standard be written down in a document, but that it be captured in a reformatter tool in code.

Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: What's the Most Effective Code Style Policy? Posted: Jul 17, 2007 2:55 PM
Reply to this message Reply
Anyone remember Knuth's literate programming? That provided a separate presentation for your code which was better designed to be read (by humans).

Cameron Purdy

Posts: 186
Nickname: cpurdy
Registered: Dec, 2004

Re: What's the Most Effective Code Style Policy? Posted: Jul 17, 2007 3:03 PM
Reply to this message Reply
> The best policy is (almost) No Policy

I disagree.

The best policy is to make sure that your team is made up of people who can agree to a similar set of rules. Sometimes that similar set of rules is "The best policy is No Policy", and sometimes that similar set of rules is a strict set of style guidelines.

So agree to agree, or agree to disagree.

But it sucks when you have style Nazis and style hippies on the same team. Then you end up arguing about nonsense instead of getting work done.

Peace,

Cameron Purdy
http://www.oracle.com/technology/products/coherence/index.html

Cedric Beust

Posts: 140
Nickname: cbeust
Registered: Feb, 2004

Re: What's the Most Effective Code Style Policy? Posted: Jul 17, 2007 4:31 PM
Reply to this message Reply
> I have been watching the discussion and considering this
> issue. I think it is good to have a code standard for a
> project that has multiple people working on it.

I think there is pretty much universal agreement here. Even a bad style guide is better than no guide at all, in my opinion.

The Sun style guide is a good start but a lot of companies and projects just use it as a foundation and then list exceptions to these rules, which can range from significant semantic differences (e.g. "all fields must begin with m_" or "all interfaces must start with 'I'") to fairly minor ones ("opening braces always go on their own line").

There is very little point in discussing which one is better, but it is important to capture "a" decision in a document that all developers need to refer to when they're coding.

Another rule that works well in my experience is: "In doubt, make your code look like the code that surrounds it".

--
Cedric

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: What's the Most Effective Code Style Policy? Posted: Jul 17, 2007 5:37 PM
Reply to this message Reply
> > I have been watching the discussion and considering
> this
> > issue. I think it is good to have a code standard for a
> > project that has multiple people working on it.
>
> I think there is pretty much universal agreement here.

How can you say that, when this whole topic is about this question? If there were "universal agreement", this topic wouldn't have 30+ responses.


> Even a bad style guide is better than no guide at all, in
> n my opinion.

Hmm, how bad can I make one? :-)

Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: What's the Most Effective Code Style Policy? Posted: Jul 17, 2007 5:49 PM
Reply to this message Reply
> > > I have been watching the discussion and considering
> > this
> > > issue. I think it is good to have a code standard for
> a
> > > project that has multiple people working on it.
> >
> > I think there is pretty much universal agreement here.
>
> How can you say that, when this whole topic is about this
> question? If there were "universal agreement", this topic
> wouldn't have 30+ responses.
>
>
> > Even a bad style guide is better than no guide at all,
> in
> > n my opinion.
>
> Hmm, how bad can I make one? :-)

Oh boy, this can be fun! Imagine this is for C++ or Java.

1. All variable names must be no more than two letters long.
2. All variable names must contain at least two vowels.
3. Newlines are not allowed in code. Since it is a freeform language newlines just take up extra space that can be wasteful of disk space. Not to mention it slows down compilation.
4. ALWAYS ALWAYS ALWAYS use pre-increment unless post-increment is specifically called for.
5. function names should be all caps
6. variable names should be all caps
7. the letters 'Q' and 'Z' cannot be used in function names. Phones work just fine without them. So can we.

Eivind Eklund

Posts: 49
Nickname: eeklund2
Registered: Jan, 2006

Re: What's the Most Effective Code Style Policy? Posted: Jul 18, 2007 5:14 AM
Reply to this message Reply
> > The goal should be for any programmer to feel equally
> at
> > home in any part of the code base, which means that
> code
> > written by one programmer should be impossible to
> > distinguish from code written by another programmer.
>
> Several comments have said similar - that different code
> styles hinder collective code ownership. I just find this
> hard to believe.
>
> The fact that some unfamiliar 3rd party open source
> library is used often hinders, a different take on "what
> is OO" often hinders, a poor design choice (like using a
> Bubble Sort, like using every pattern in the book to show
> off that you just read it) often hinders, but relative to
> those the placement of spaces in a line is just, well,
> trivial. If your programmers are seriously confused by
> bracket placement, they aren't very good programmers.

Actually, the situation where I have had this experience most profoundly (FreeBSD) is mostly the opposite: The people are very, very good programmers working in cooperation, so the problems you mention do not occur (much).

People aren't seriously confused by bracket placement; it's just an annoyance, one thing to adapt to. Sort of like if I staartd 2 change hov ai rite inglish, u kan rid it it is just slouver and harder to pick up the high-level stuff as you're wasting mental energy on extra low-level cognitive processing.

Standardizing that, and having people learn that style so it sits deeply, frees up that cognitive capacity for working at a higher level. It is far from the only issue, yet it makes it easier to work on the other issues, and is low-hanging fruit.

Also, the thing that is most important is that code in the same "area" (usually, same file or subproject) follow the same style, so you can set up a cognitive context for it. It's just even more effective if you can do this for the entire project, and it is easier for people to do modifications in a way that keep the style consistent in one area if that area has the same style they are used to.

That's my experience, with my cognitive style and the different projects I've worked with. And it is also my experience that having a strict style is in some ways more important when you work with good programmers than bad, as the bad programmers will make other mistakes that are more significant - but fixing the low hanging fruit of the style will make it easier to fix the other problems.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: What's the Most Effective Code Style Policy? Posted: Jul 18, 2007 6:57 AM
Reply to this message Reply
From the above discussions, it appears that "Java Coding Standards" and "Bracket Placement" are wholly interchangeable terms.

Once you've eliminated (or preferably permitted) the bracket variations is there anything of importance left that is not covered by the Sun guidelines and conventions?

Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: What's the Most Effective Code Style Policy? Posted: Jul 18, 2007 10:19 AM
Reply to this message Reply
> Once you've eliminated (or preferably permitted) the
> bracket variations is there anything of importance left
> that is not covered by the Sun guidelines and conventions?

TAB characters versus spaces. While that is covered by the guidelines (as is bracket placement) there is considerable disagreement.

John Bayko

Posts: 13
Nickname: tau
Registered: Mar, 2005

Re: What's the Most Effective Code Style Policy? Posted: Jul 18, 2007 10:48 AM
Reply to this message Reply
The reason coding standards so often seem like micromanaging is because they are. The most effective way to get people to achieve a goal is to explain the problem and the goal, and unless they are incompetent they can figure out how to meet it (if they are that dense, some suggestions help).

For a coding standard, explaining what the problems are is probably much more important than mandating a single solution. For example, if the problem is different placement of braces in a file, say that the goal is style should be consistent within a file - match whatever's there. For the question of where to break lines, say that the goal is that the next line should present as much information at the beginning to indicate its relationship to the previous line as possible, so:

a = b + c + d + e;

...might best be split before an operator rather than before a variable:

a = b + c + d
+ e;

For columns, decide whether the priority for code appearance is easier reading or easier editing (I'd go for editing). And so on.

If a coding standard wants to define a solution to every variation of every item it covers, it's going to start looking like a language definition itself (BNF and all). But programmers are problem solvers - if you just want someone to type in a program without thinking you'd hire typists. If you're going to bother hiring people who solve problems for a living, let them solve problems. Following a list of conditions and actions are what computers are for, not people.

Bill Venners

Posts: 2242
Nickname: bv
Registered: Jan, 2002

Re: What's the Most Effective Code Style Policy? Posted: Jul 18, 2007 12:10 PM
Reply to this message Reply
> > Once you've eliminated (or preferably permitted) the
> > bracket variations is there anything of importance left
> > that is not covered by the Sun guidelines and
> conventions?
>
> TAB characters versus spaces. While that is covered by the
> guidelines (as is bracket placement) there is considerable
> disagreement.

One thing I liked about Python's use of indentation to set off blocks was that it completely eliminated the debate about where to put the open curly brace. My friend Matt Gerrans and I were talking about how this was a good thing many years back, and then spent the next half hour arguing about how many spaces to indent in Python programs. Seems how to indent could also have been dictated by the Python language, but interestingly Guido found a useful way to apply this variation. In his first blog post after joining Google, he wrote:

And did I mention that I get to spend 50% of my time on Python? No strings attached. Of course I get to spend the other 50% on Python too, but that's in a corporate setting. Fortunately it's easy to separate the two. If it uses two-space indents, it's corporate code; if it uses four-space indents, it's open source. (If it uses tabs, I didn't write it! :-)

http://www.artima.com/weblogs/viewpost.jsp?thread=143947

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: What's the Most Effective Code Style Policy? Posted: Jul 18, 2007 12:46 PM
Reply to this message Reply
I'm in a big minority, but I like to mix styles. A couple of examples: (Java, but applicable to other languages - I added this a lot of palces for clarity)

Long vs. shorter variable names. I tend to use short names. A File would be file, a Foo would be a foo, (or maybe inFoo, outFoo) etc. Unless there is something tricky going on. For example, if a class has a List of Orders that might be getting modified by another thread, and I want to look at it


List<Order> avoidConcurrentModificationException = new ArrayList<Order>(this.orders);
for (Order order : avoidConcurrentModificationException)...


Somebody else reading the program has their eyes drawn to the unusually long name, and hopefully realizes that this is the important or tricky part of the routine.

Similar with "trivial" getters and setters. I used to write them on one line. The idea is to emphasize that they (for the most part) aren't worth looking at. Especially if they are private and only there because Hibernate needs them.


// trivial accessors required by Hibernate
private String getFoo() { return this.foo; }
private void setFoo(String foo) { this.foo = foo; }


The reader's eye can skim over this boilerplate code, and will be drawn to the real code that has normal bracket placement and useful Javadocs.

IMO, having pages of normal styled trivial accessors, each with some meaningless Javadoc required by a style checker, so they add up to 10 lines or so each, detracts from real understanding of the code by distracting the reader from the "meat".

Wilfred Springer

Posts: 176
Nickname: springerw
Registered: Sep, 2006

Re: What's the Most Effective Code Style Policy? Posted: Jul 19, 2007 4:08 AM
Reply to this message Reply
In the past 10 years, I have seen several styles. My experience: you get used to all of them, eventually.

So our policy at TomTom:

==========
Change Eclipse settings from tabs to spaces, and then stick with the default.
==========

Pretty simple. And you get used to it pretty quickly. Making sure you stick to the coding conventions is as simple as hitting CTRL+Shift f.

Erick Reid

Posts: 7
Nickname: erickreid
Registered: May, 2003

Re: What's the Most Effective Code Style Policy? Posted: Jul 25, 2007 5:37 PM
Reply to this message Reply
i'm a great fan of coding standards but am nonetheless cautious of overimplementation. still, things that seem silly like extra spaces and blank lines really ought to be given proper definition and sun's standard is a great base on which to build.

have a strict standard permits the creation of effective value-adds like code generation utilities. if all code follows strict rules, codegen utils can be created quickly, knowing that certain preconditions will be in place. even one-off whizbang codegen utils can bring exponential efficiencies and one key to that pile of gold is not having to spend a lot of time on the generator. a code standard goes a long way to enabling the ability to create such utils.

i worked for an outstanding practitioner who had at one time been on the c++ standards committee and who was/is a big believer in a very strict code standard, enforced through code reviews. at times these reviews and resulting marks felt nit-picking, but the advantages were more than worth it.

developers will grouse at having to bend their style to match a code standard, but really, the true impact such (likely minor) adaptations will have on productivity really should be negligible at first and non-existent within a very short period of time.

>> it took me extra time to line up everything into the columns

bad stuff. columns are prettier but a pain to maintain in the face of refactorings, renaming and other house cleaning. if important, soup up a formatter to columnize things on checkin.

Flat View: This topic has 43 replies on 3 pages [ « | 1  2  3 ]
Topic: What's the Most Effective Code Style Policy? Previous Topic   Next Topic Topic: Pardon My French, But This Code Is C.R.A.P.


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us