views:

507

answers:

13

How would you format/indent this piece of code?

int ID = Blahs.Add( new Blah( -1, -2, -3) );

or

int ID = Blahs.Add( new Blah(
1,2,3,55
)          
);


Edit:

My class has lots of parameters actually, so that might effect your response.

+2  A: 

or

int ID = Blahs.Add( 
            new Blah( 1, 2, 3, 55 )          
         );

I must confess, though, that 76 times out of 77 I do what you did the first time.

boost
+2  A: 

first way since you are inlining it anyway.

StingyJack
+5  A: 

I'd go with the one-liner. If the real arguments make one line too long, I would break it up with a variable.

Blah blah = new Blah(1,2,3,55);
int ID = Blahs.Add( blah );
Patrick McElhaney
+1  A: 

One line, unless there's a lot of data. I'd draw the line at about ten items or sixty, seventy columns in total, whatever comes first.

Kawa
+4  A: 
int ID = Blahs.Add
( 
    new Blah
    (
        1,    /* When the answer is within this percentage, accept it. */ 
        2,    /* Initial seed for algorithm                            */ 
        3,    /* Maximum threads for calculation                       */ 
        55    /* Limit on number of hours, a thread may iterate        */ 
    )          
);
EvilTeach
/* This is a one. *//* This is a two. *//* This is a three. *//* This is a fifty five. */
frou
so much wasted space, ugh!
Chris Ballance
My eyeballs were hurting until I got to this one. Thank you.
MusiGenesis
@Chris: I sell bulk whitespace off my website. I can cut you a good deal, and I take PayPal.
MusiGenesis
i need a gross of tabs, and 5000 rounds of blanks for my 380.
EvilTeach
+4  A: 

All numbers are being added to a result. No need to comment each number separately. A comment "these numbers are added together" will do it. I'm going to do it like this:

int result = Blahs.Add( new Blah(1, 2, 3, 55) );

but if those numbers carry some meaning on their own, each number could stand for something entirely different, for example if Blah denotes the type for an inventory item. I would go with

int ID = Blahs.Add( new Blah(
    1, /* wtf is this */ 
    2, /* wtf is this */
    3, /* wtf is this */
    55 /* and huh */
));
Johannes Schaub - litb
under what conditions would they not have a meaning?
EvilTeach
if they don't have a different meaning on their own. for example, all numbers are being added to a result. No need to comment each number separately. A comment "these numbers are added together" will do it. But if Blah is a inventory item, each number could stand for something entirely different.
Johannes Schaub - litb
+2  A: 

I would use similar formatting as your first example, but without the redundant space delimiters before and after the parenthesis delimiters:

int id = BLahs.Add(new Blah(-1, -2, -3));

Note that I also wouldn't use an all upper-case variable name in this situation, which often implies something special, like a constant.

J c
+10  A: 

I agree with Patrick McElhaney; there is no need to nest it....

Blah aBlah = new Blah( 1, 2, 3, 55 );
int ID = Blahas.Add( aBlah );

There are a couple of small advantage here:

  1. You can set a break point on the second line and inspect 'aBlah'.
  2. Your diffs will be cleaner (changes more obvious) without nesting the statements, e.g. creating the new Blah is in an independent statement from adding it to the list.
Foredecker
+1 for spacing out the comma-delimited elements
MusiGenesis
+1 for anticipating debuggery
mseery
Agreed! Also, the addition of the variable makes the code a little more self-documenting (assuming you name the variable thoughtfully).
Patrick McElhaney
+2  A: 

Either split it into two lines:

new_Blah = new Blah(-1, -2, -3)
int ID = BLahs.Add(new_Blah);

Or indent the new Blah(); call:

int ID = BLahs.Add(
    new Blah(-1, -2, -3)
);

Unless the arguments were long, in which case I'd probably do something like..

int ID = BLahs.Add(new Blah(
    (-1 * 24) + 9,
    -2,
    -3
));

As a slightly more practical example, in Python I quite commonly do the either of the following:

myArray.append(
    someFunction(-1, -2, -3)
)

myArray.append(someFunction(
    otherFunction("An Arg"),
    (x**2) + 4,
    something = True
))
dbr
A: 

int ID = Blahs.Add(new Blah(1,2,3,55)); // Numbers n such that the set of base 4 digits of n equals the set of base 6 digits of n.

A: 

The problem with

Blah aBlah = new Blah( 1, 2, 3, 55 );
int ID = Blahas.Add( aBlah );

is that it messes with your namespace. If you don't need a reference to the Blah you shouldn't create it.

It seems rather harsh to consider having a variable that is only used once within a scope as messing with a namespace, especially when the purpose of breaking it out is to improve code readability.
J c
Now, I don't know how much Java you've been programming, but it really isn't improving code readability just to break things like that in to several lines.Of course you could separate the ints 1, 2, 3 and 55 into new variables, using a std. emtpy constructing and then setting, but it's not good.
The fact that he added "My class has lots of parameters actually, so that might effect your response." just makes the point even better.
If the temp variable is defined within a narrow scope, it hardly contributes to namespace pollution.
Dave Sherohman
@pgd: I don't think anyone was suggesting defining each constructor argument as a separate variable. If his Blah class has lots of parameters, that would be a pro for having the aBlah reference, not a con.
J c
A: 

I'd either do it as a one-liner or assign the new Blah to a variable, depending on whether I'll need to reference that Blah directly again.

As far as the readability issue which a couple answers have addressed by putting each argument on a separate line with comments, I would address that by using named parameters. (But not all languages support named parameters, unfortunately.)

int ID = BLahs.Add(new Blah( foo => -1, bar => -2, baz => -3 ));
Dave Sherohman
+1  A: 

Whatever Eclipse's auto-formatter gives me, so when the next dev works on that code and formats before committing, there aren't weird issues with the diff.

Adam Jaskiewicz