views:

143

answers:

9

How important is it for readability that code be in this form:

public void DoStuff()
{
    var v = new Object();
    v.PropertyID = "abc";
    v.Type = "abc";
    v.Style = "abc";
    v.SetMode(Mode.Abc);
    v.Draw();
}

vs.

public void DoStuff()
  {
    var v = new Object();
        v.PropertyID = "abc";
        v.Type = "abc";
        v.Style = "abc";
        v.SetMode(Mode.Abc);
        v.Draw();
    }

I tend to like the first style best, it makes things easy to read, how would you gently guide people towards the former and away from the latter? Or would you not?

+1  A: 

Format is very important although not essential. I tend to get slightly annoyed if I see code like the latter. If you're taking the time to write the code, make sure you take the time to format it correctly.

Jamie Lewis
The problem is defining what it means to be correct.
Foredecker
I realize that format is not essential for the compiler, but I'm thinking for the next person who has to deal with the code..
Nate Bross
A: 

The second way doesn't really tab nicely. Avoid it.

I also think people tend to get carried away with formatting like that. In a month another guy will come along and want this format

public void DoStuff()
  {
    var v                = new Object();
        v.PropertyID     = "abc";
        v.Type           = "abc";
        v.Style          = "abc";
        v.SetMode        (Mode.Abc);
        v.Draw           ();
  }

This gets to be pretty silly and rather difficult to work with.

If people are coding like that, question their reasoning and programming ability.

Bob
A: 

In the second example, your curly braces are not equally indented.

Spacing is important for me to read code. If you write code at my company - I'll probably have to read it at some point. If you don't format your code - I will use an autoformatter to get what I need.

David B
+2  A: 

Do people actually write code that looks like the latter? That's a maintainability nightmare.

I would argue that it's not so important what your code formatting conventions are -- more that you follow them consistently. The former example is not consistent and therefore unreadable and unmaintainable.

If you're having troubles guiding people toward consistency, have them imagine going back to maintain highly inconsistent code in a year.

Johnny G
A: 

Style is critically important when working in a team. So it doesn't matter what style you choose, just make sure everyone agrees upon it.. and then enforce the agreement. Set your IDE to auto-format your code.. and make sure everyone's IDE is set the same.

Boo
A: 

If you want to be kind, give them Code Complete to read. If you want to be mean, introduce sublte bugs like this one in their code:

if (x==y);
   DoSomething(); else
DoSomethingElse();
while(Whatever)
SomeFunction();

(If they find the bug in less than a day, you're not being sublte enough.)

Niki
A: 

I prefer your spacing, though I would do it a bit differently. I believe your most important question is how to convince someone your approach is best: Code formatting can be very subjective. Some people object because it takes too much time to get right. Others object because the team doesn't have any coding standards. Some object because the feel it is crammed down their necks.

The best method is to work with your team to build consensus that your particular approach is the best practice. This is true if you are the lead, or if you are a individual contributor.

Once a team consensus is generally accepted (it may not be universal), then I find that code reviews are the best place to ensure that team practices are followed. I suggest that you will find peer pressure is the most effective way to encourage other people to follow an accepted best practice. The corralling is often true; it is hard for one person to drive this kind of thing on team without consensus.

Here are some of my related StackOverflow answers

Foredecker
+1  A: 

If it were my code, I'd do it like this:

public void DoStuff()
{
    var v = new Object();

    v.PropertyID = "abc";
    v.Type       = "abc";
    v.Style      = "abc";

    v.SetMode(Mode.Abc);
    v.Draw();
}

This way it's clear which lines are property assignments and which ones are method calls.

I also agree with Jamie's answer, which said that "format is very important although not essential." What matters is that the formatting is not so bad that it impairs the ability of others to read it. I don't believe that a handful of extra tabs or newlines is going to make a huge difference to a competent programmer most of the time.

Parappa
A: 

As others have said, the first example is the norm; the second differs from it.

Also, make sure everyone working on the same set of files has the same convention for what a 'tab' is. It is best to define this as a number of spaces and to make sure everyone's text editors and IDE's agree.

It gets annoying when three or four people are working in the same SVN repository and editing each files with different spacing conventions.

tehblanx