views:

190

answers:

6

For quick tasks where I only use an instantiated object once, I am aware that I can do the following:

int FooBarResult = (new Foo()).Bar();

I say this is perfectly acceptable with non-disposable objects and more readable than the alternative:

Foo MyOnceUsedFoo = new Foo();
int FooBarResult = MyOnceUsedFoo.Bar();

Which do you use, and why?
Would you ever use this type of anonymous instantiation in a production app?
Preference: with parenthesis "(new Foo()).Bar();" or without "new Foo().Bar();"?

(Edited to abstract question away from Random class)

+7  A: 

Side note regarding random numbers: In fact, no, your specific example (new Random().Next(0,100)) is completely unacceptable. The generated random numbers will be far from uniform.


Other than that, in general, there is not much difference between the two. The compiler will most probably generate the exact same code in either case. You should go with the most readable case (long statements might harm readability; more code will do it too, so you have to make the trade-off in your specific case).

By the way, if you chose to go with the single line case, omit the unnecessary parens (new MyObject().Method() will do).

Mehrdad Afshari
There is no distribution if you generate only one random number. And if Random() has a bad cold start behavior you get none uniform numbers either way.
Daniel Brückner
@danbruc: Technically, there is a distribution, but yes, I get your point. You are right. But I have seen devs use this method to generate random numbers in a loop several times. I thought it's pretty important to mention it here.
Mehrdad Afshari
Full ack on that - you should not even think of generating multiple numbers this way.
Daniel Brückner
I appreciate the feedback
Robert Venables
@Scott Dorman: Thanks for the correction.
Mehrdad Afshari
+2  A: 

If you are only using the object once, the first way is better all the time.

It is shorter and clearer, because it makes it explicit that you will not use the object later.

It will probably compile to the same CIL anyway, so there's no advantage to the second form.

RossFabricant
+1  A: 

First statement. It's more readable, has less code and doesn't leave temps around.

Kozyarchuk
+5  A: 

You might want to consider the implications of using the code in the debugger. The second case will allow you to inspect the object you've created, whereas the first won't. Granted you can always back out to the second case when you're attempting to debug the code.

I've done it both ways and don't really have a preference. I prefer whatever looks more readable, which is highly dependent on the complexity of the class and method being called.

BTW -- you might want to pick a different example. I fear that your point might get lost in discussions over the best way to generate random numbers.

tvanfosson
+1 Regarding debugging - excellent point
Robert Venables
A: 

In fact the first way, creating a temporary, is more readable for two reasons:

1) it's more concise

There's less code to read, there's no unnecessary local variable introduced, and no potential name clash with another local, or shadowing of any variable with the same name in an enclosing scope

2) it communicates something that the second form doesn't, that the object is being used temporarily.

Reading it, I know that that instance is never going to be used again, so in my "mental compiler" that I use to understand the code I'm reading, I don't have to keep a reference to it any more than the code keeps a reference to it.

As Mehrdad notes, though, doing it with a Random class isn't a good idea.

As he also notes, the redundant parentheses make it less concise; unless you're in a dusty corner of a language, assume that competent programmers know the language's operator precedence. In this case, even if I don't know the operator precedence, the alternative parse (calling new on a function's return) is nonsensical, so the "obvious" reading is the correct reading.

int RandomIndex = (new Random()).Next(0,100);
int RandomIndex = new Random().Next(0,100);
tpdi
If the method takes a fair number of parameters, the first option may be less readable. Readability is a lot more like grammar than mathematics. The rules are usually correct, but sometimes breaking a rule makes for better code.
tvanfosson
A: 

The second one is debugging friendly, while the first one isn't. The second wins only because of this.

Gorkem Pacaci