How would you format / strip this piece of code?

How would you format / strip 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:

There are many parameters in my class, so this may affect your answer.

0


source to share


13 replies


I agree with Patrick McElhany; no need to insert it ....

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

      



There are a few small advantages here:

  • You can set a breakpoint on the second line and check "aBlah".
  • Your differences will be cleaner (changes become more obvious) without inserting statements, for example. creating a new Blah in an independent statement adds it to the list.
+12


source


I would go with one liner. If the real arguments are too long, I would split them into a variable.



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

      

+5


source


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        */ 
    )          
);

      

+4


source


All numbers are added to the result. There is no need to comment on each issue separately. The comment "these numbers are added together" will do this. I'll do it like this:

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

      

but if these numbers have some meaning in and of themselves, each number can mean something different, for example, if it Blah

denotes a type for an item of inventory. 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 */
));

      

+4


source


or

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

      

I have to admit that 76 times out of 77 I do what you did the first time.

+2


source


the first way, since you put it in anyway.

+2


source


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

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

      

Note that in this situation I will also not use an uppercase variable name, which often implies something special, such as a constant.

+2


source


Or split it into two lines:

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

      

Or the indentation of a new Blah (); call:

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

      

If the arguments weren't long, in which case I would 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 often do one of the following:

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

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

      

+2


source


One line if there is not a lot of data. I would draw a line at about ten points, or sixty, seventy columns in total, whatever happens first.

+1


source


Whatever Eclipse auto-formatting gives, so when the next developer works on that code and formats it before committing, there is no diff issue.

+1


source


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

0


source


A problem with

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

      

is that it gets confused with your namespace. If you don't want a link to Blah, you shouldn't create one.

0


source


I'll either do it as a one-liner or assign the variable to a new Blah

variable, depending on whether I need to reference it again Blah

.

Regarding the readability question that a couple of answers answered by putting each argument on a separate comment line, I would address this using named parameters. (But not all languages ​​support named parameters, unfortunately.)

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

      

0


source







All Articles