38268

How would you format/indent this piece of code?

Question:

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 ) ); <hr /><h3>Edit:</h3>

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

Answer1:

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:

<ol><li>You can set a break point on the second line and inspect 'aBlah'. </li> <li>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.</li> </ol>

Answer2:

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

Answer3:

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

Answer4:

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

Answer5:

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.

Answer6:

first way since you are inlining it anyway.

Answer7:

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.

Answer8:

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 ))

Answer9:

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.

Answer10:

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.

Answer11:

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.

Answer12:

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.

Answer13:

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

Recommend

  • Difference (if there is any) between ` and ' in javascript [duplicate]
  • Difference between heading inside section or before it in HTML5
  • Add a row to a matrix inside a function (and propagate the changes outside) in Julia?
  • Eclipse ECF Vs Apache CXF
  • ORA-01843: not a valid month
  • Can comments make any difference during the run-time?
  • Displaying objects which are found in C++
  • Replace everything except numbers in a string vb6
  • Relationship between orders, customers and products?
  • How do you access podcasts in WP7?
  • HTTPS + gzip: Is it a security vulnerability if I only gzip non-sensitive files?
  • Numpy: How to get rid of the minima along axis=1, given the indices - in an efficient way?
  • Touch events over two dom elements?
  • Measure heap used by each object in Java [closed]
  • Can't find cygwin setup.exe file?
  • CUDA NSight is not installed with CUDA 5.0 installation file on Windows 8? [closed]
  • is there a way to update filter with async data
  • IE6 changes DOCTYPE to a bad one
  • Cannot upload to OneDrive using the new SDK
  • How to assign byte[] as a pointer in C#
  • App restarts from wrong activity
  • zope_i18n_compile_mo_files doesn't work on a Zeo configuration
  • Custom Tabgroup Appcelerator
  • $wpdb not working in file of WordPress plugin
  • Is there a javascript serializer for JSON.Net?
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Where to put my custom functions in Wordpress?
  • Apache 2.4 - remove | delete | uninstall
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • Arrays break string types in Julia
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Buffer size for converting unsigned long to string
  • AngularJs get employee from factory
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Binding checkboxes to object values in AngularJs