How would you format / strip this piece of code?
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.
source to share
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 */
));
source to share
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.
source to share
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
))
source to share
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 ));
source to share