Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason not to use `new object().foo()`?

When using extremely short-lived objects that I only need to call one method on, I'm inclined to chain the method call directly to new. A very common example of this is something like the following:

string noNewlines = new Regex("\\n+").Replace(" ", oldString);

The point here is that I have no need for the Regex object after I've done the one replacement, and I like to be able to express this as a one-liner. Is there any non-obvious problem with this idiom? Some of my coworkers have expressed discomfort with it, but without anything that seemed to be like a good reason.

(I've marked this as both C# and Java, since the above idiom is common and usable in both languages.)

like image 463
JSBձոգչ Avatar asked Dec 21 '10 18:12

JSBձոգչ


4 Answers

This particular pattern is fine -- I use it myself on occasion.

But I would not use this pattern as you have in your example. There are two alternate approaches that are better.

Better approach: Use the static method Regex.Replace(string,string,string). There is no reason to obfuscate your meaning with the new-style syntax when a static method is available that does the same thing.

Best approach: If you use the same static (not dynamically-generated) Regex from the same method, and you call this method a lot, you should store the Regex object as a private static field on the class containing the method, since this avoids parsing the expression on each call to the method.

like image 65
cdhowie Avatar answered Nov 02 '22 15:11

cdhowie


I don't see anything wrong with this; I do this quite frequently myself.

The only exception to the rule might be for debugging purposes, it's sometimes necessary to be able to see the state of the object in the debugger, which can be difficult in a one-liner like this.

like image 40
CodingGorilla Avatar answered Nov 02 '22 17:11

CodingGorilla


If you don't need the object afterwards, I don't see a problem - I do it myself from time to time as well. However, it can be quite hard to spot, so if your coworkers are expressing discomfort, you might need to put it into a variable so there are no hard feelings on the team. Doesn't really hurt you.

like image 2
Femaref Avatar answered Nov 02 '22 15:11

Femaref


You just have to be careful when you're chaining methods of objects that implement IDisposable. Doing a single-line chain doesn't really leave room for calling Dispose or the using {...} block.

For example:

DialogResult result = New SomeCfgDialog(some_data).ShowDialog();

There is no instance variable on which to call Dispose.

Then there is potential to obfuscate intent, hurt rather than improve readability and make it tougher to examine values while debugging. But those are all issues particular to the object and the situation and the number of methods chained. I don't think that there is a single reason to avoid it. Sometimes doing this will make the code more concise and readable and other times it might hurt for some of the reasons mentioned above.

like image 2
Paul Sasik Avatar answered Nov 02 '22 15:11

Paul Sasik