Is constructor returning object never used wasteful?

I'm working with a code base that instantiates objects without assigning to a variable, like so:

new MyNonStaticClass();

Inside the constructor are calls to static classes/methods anyway...

Whilst this is valid, it's the only usage of the constructor and could be converted to a static method or static class.

This is littered through-out the code base, so the the question is:

Is there a performance benefit to changing these to static classes?

I'd like to if I had the time, but I need to prioritise the workload.

Jon Skeet
people
quotationmark

Is there a performance benefit to changing these to static classes?

Yes - you won't be constructing a load of pointless objects which the GC then has to collect. Is that significant in your application? Hard to tell.

More importantly, there's a significant readability benefit. Constructing an object has connotations about per object state being important. A void static method call can only have side-effects in terms of exceptions and global state, so it becomes a lot clearer what the purpose of the call is. Plus, you can give it a meaningful name.

I'd expect it to be pretty easy to refactor this with search and replace. (I don't know of tools to do this sort or refactoring easily, given that it's a weird situation. They may exist, but search and replace is probably quicker than finding them.)

people

See more on this question at Stackoverflow