1

I have an abstract class that is supposed to become the base of a huge hierarchy of classes. Among other things, it has a certain abstract method. During the execution, it will be called on all objects of this class constantly by a system that knows only about this abstract class and not it's children:

    foreach( AbstractClass object in allTheObjects )
        object.DoStuff();

However, a lot of it's children in fact don't override this method (which is empty in the base class); ones that use it and ones that don't a distributed among the class hierarchy. Will the C# take care of this empty method, or will I have to optimize it in some way?

P.S.: I know that premature optimization is evil, but it just got me really curious.

Max Yankov
  • 12,551
  • 12
  • 67
  • 135
  • What do you mean by **optimize**? – Mike Perrenoud Oct 22 '12 at 17:34
  • 3
    How do you optimize a method that does nothing? – JonH Oct 22 '12 at 17:34
  • 1
    *However, a lot of it's children in fact will have this method empty* Ignoring possible perf considerations, this doesn't indicate good things for your design. If you can you might want to favor composition over inheritance here instead. – Conrad Frix Oct 22 '12 at 17:34
  • @JonH, another good way of asking the question. My thoughts exactly. – Mike Perrenoud Oct 22 '12 at 17:35
  • By optimization I mean removing the unneeded method call. – Max Yankov Oct 22 '12 at 17:42
  • 1
    @golergka Calling a method that does nothing is *very* fast for the computer. Even doing it a few thousand times won't take long at all. You may want to improve the design to make it easier to understand and use, but it won't need to be to improve the speed. – Servy Oct 22 '12 at 17:45
  • 1
    It's faster to just call the empty method then check if it was implemented or not. There wouldn't be any difference even if you had a property on each object that you overrode and implemented called "CanCallDoStuff" and checked that before calling the method. If you can reduce the size of your enumeration by keeping a separate collection of items that DO implement the method and enumerate over that instead, that *might* be worth it if you enumerate very often and modify the collection rarely. But as you said yourself, most likely premature optimization. – Mike Marynowski Oct 22 '12 at 17:55

2 Answers2

3

I wouldn't worry about that. In C#, method calls are really cheap. And even if you would optizmizie this, I doubt that you will see any differnce. See this post for reference.

I'd worry more about if you could avoid looping through everything, or what data strucutre allTheObjects is. I'd say there's much more potential for optimization there.

Also you might think about if you really need a big inheritance structure, or if you can achieve your goals with composition or interfaces.

You will also find more information here (interface methods vs. delegates vs. normal method calls)

Community
  • 1
  • 1
LueTm
  • 2,366
  • 21
  • 31
  • What exactly do you mean by **In C#, method calls are really cheap.**? As opposed to being expensive in what other language? – Mike Perrenoud Oct 22 '12 at 17:40
  • It means they are not expensive, so you don't have to worry about calling them (or refactor a large one into two). – LueTm Oct 22 '12 at 17:43
  • @BigM It means that LueTm can't speak for every other language in existance, he can only speak for C# (or only choose to speak for C#), which is both what is relevant here and what he has experience in. – Servy Oct 22 '12 at 17:46
  • @Servy, I think my confusion is, when is calling an empty method ever expensive in any language? – Mike Perrenoud Oct 22 '12 at 17:47
  • @LueTm, see my comment to Servy please. – Mike Perrenoud Oct 22 '12 at 17:47
  • @BigM i.e. in javascript, a long prototype chain could mean it is very expensive to call lots of empty methods on objects high up in the prototype chain. – Mike Marynowski Oct 22 '12 at 17:48
  • @BigM With your statement the burden of proof lies on you to show that it is never expensive in any language. Without knowing much about every single language ever, I know I couldn't make such an assertion, what with all of the toy languages designed to do messed up stuff. I'm not saying you're wrong, I'm saying there's no need to prove it just to answer this question. The OP doesn't care about non-C# languages in this context. – Servy Oct 22 '12 at 17:49
  • Not quite; it's not a practical question, but rather a theoretical one (as I wouldn't optimize it at this stage anyway), I'm just curious about it, in other languages too. – Max Yankov Oct 22 '12 at 18:14
2

However, a lot of it's children in fact will have this method empty

Maybe you need to declare this method as virtual instead of abstract? Thus you can provide default empty implementation.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • That's what I meant, I don't know why I didn't clarify that. But the same empty method is called, so it doesn't make a difference where exactly is it declared, right? – Max Yankov Oct 22 '12 at 17:45
  • @golergka: Empty virtual methods at the base just removes the need for empty implementation of abstract methods. – jwaliszko Oct 22 '12 at 18:07
  • That's what I did, I just didn't clarify it at first. – Max Yankov Oct 22 '12 at 18:12