-1

Example

(1)

var classVar = new class();

classVar.Method();

(2)

new class().Method();

I like the second way, less wordy.

Is there a performance difference?

Is it considered bad coding practice?

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
BenG
  • 19
  • 4
  • 1
    You can not name your class `class`. And you shouldn’t name your method `Method`, i.e. starting with an uppercase letter. Besides that, the answer is, it doesn’t matter all. Use whatever you prefer. – Holger Feb 11 '22 at 10:32
  • Is this Java? You should add a suitable tag. – Manngo Feb 12 '22 at 00:49
  • I know this is supposed to be a simple example, but it’s too simple. If you use `new`, it’s because you want a specific instance, presumably with specific data. In that case you would use `new class(data)`. If that’s not the case, then the method is presumably static, so you don’t need `new` at all. Which way is this going? – Manngo Feb 12 '22 at 00:54
  • the use of the name "class" was meant to make it as simple as possible. – BenG Feb 12 '22 at 01:10
  • Yes, but is the method supposed to be static? – Manngo Feb 12 '22 at 01:24
  • What language is this? – Manngo Feb 12 '22 at 01:34

2 Answers2

0

If your Method() doesn't use anything from that class, why is it defined there? You could use namespace instead.

The only difference between your (1) and (2) is that the class instance is destructed immediately in (2).

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
  • Well, _your_ reference to it is gone, but it could have stashed `this` somewhere. – Davis Herring Feb 05 '22 at 03:05
  • I didn't add a variable in the method call because I was just trying to make it as simple as possible. Just asking opinions on any performance difference (if any) and if considered bad practice. Thx – BenG Feb 07 '22 at 21:02
  • @BenG if the instance of your `class` did not exist before the call to its `Method()`, this method should be a `static` in that class or, instead, in a namespace. Yes, there is a cost in creating an instance of your class, and it will be wasted here. – Vlad Feinstein Feb 08 '22 at 02:59
  • 1
    There is no such thing as “immediate destruction” in Java. – Holger Feb 11 '22 at 10:30
  • @Holger I swear there was no Java tag when I answered. I wouldn’t even read that question – Vlad Feinstein Feb 11 '22 at 15:46
  • Working in C# environment. I did not select Java tag, not sure how that got there. I only selected performance and code-standards. – BenG Feb 12 '22 at 00:12
  • It's not Java, Java has no var keyword. Tag was added by an editor, removing it. – Gabe Sechan Feb 12 '22 at 00:18
0

in your case Method seems to be static (ie there is no useful data stored inside the instance of 'class' (note I am ignoring all the invalid / not recommended name issues). Assuming this is c# but all languages have the same ideas, you would go

public class class{ // ugh
   public void static Method(){
       Console.WriteLine("I am Method");
   }
}

now you can go

 class.Method();

so the code is neatly filed away inside 'class' but you dont have to pay any overhead.

If you really need to instantiate 'class' then presumably you need to do more than just call one method on it. In which case

new class().Method();

makes no sense, since you call create it, call a method and destroy it.

Really a more concrete example is needed

pm100
  • 48,078
  • 23
  • 82
  • 145