1

I've read about pro's/cons of static but I'm not sure about how to do it in my case from a performance point of view.

I have a classA with different variables and also functions with timers:

class ClassA
{
    // More vars...
    private System.Timers.Timer _values;
    public ClassA(IntPtr handle, String t)
    {
        _handle = handle;
        _title = t;
        CheckRoutineAndContinue();     
    }

Where CheckRoutineAndContinue is this:

    private void CheckRoutineAndContinue()
    {
        _values= new System.Timers.Timer();
        _values.Elapsed += delegate { Check(); };
        _values.Interval = 200;
        _values.AutoReset = false;
        _values.Start();
    }

    private void Check()
    {
        if (_max> 5) StopCheck();
        else
        {
            // Logic...
        _max++;
    }

    private void StopCheck()
    {
        if (_values!= null)
        {
            _values.AutoReset = false;
            _values.Enabled = false;
            _values.Stop();
        }
    }

My question is: I will have multiple objects of ClassA. If I create an static method for CheckRoutineAndContinue() it will only be created once and I can pass it the parameters I need to work with, whereas if I have it in my class, I don't need to send variables of ClassA and will be created only once. The code executed by all ClassA objects will be the same, but each one has different values in variables.

Is this a good occasion to create an static method and pass all the variables via parameters around somehow so these functions is only created one, or is it recommended to have these functions in the same class even though they are going to be created everytime I create a new ClassA object?

Assuming testing is no big deal in this case.

I would assume an static method is better as functions will be created again everytime, but I'm not 100% plus I'm not sure if an static method can handle the timer properly as I need to start-stop it depending on the logic of the function inside the timer.

сами J.D.
  • 483
  • 2
  • 5
  • 19
  • 3
    If you use a static function, it might be indeed (a tiny little bit) faster, but you'll lose acces to your class member variables. This will be a big OOP violation in most cases. – Stefan Aug 20 '15 at 20:52
  • If you have a "which is faster" performance question, the way to get the correct answer is to test both methods. – Blorgbeard Aug 20 '15 at 20:52
  • @Blorgbeard well, I also want to know what's the 'right way' to do it. But yes, your answer is correct as well. – сами J.D. Aug 20 '15 at 20:57
  • I've never looked at the IL code for this, but I always work on the idea that all code is static but with a syntax sugar of having a "this" variable passed to it invisibly, like you would write an extension method. I am probably technically wrong, and static might have a very marginal gain by not looking up a virtual function table or checking for overrides, but it would be a pretty small margin of benefit. Look forward to any clear answer this question brings. – PhillipH Aug 20 '15 at 20:58
  • 2
    @PhillipH: see http://stackoverflow.com/questions/169378/method-can-be-made-static-but-should-it – Stefan Aug 20 '15 at 20:59
  • @Stefan why am I losing access? I have getters and setters in ClassA, I still have access to them. However, calculations would be done in a different class so it's only loaded once. Am I right? Or you also talk about the timer variable, that's an OOP violation? – сами J.D. Aug 20 '15 at 20:59
  • 1
    Oops, my bad, yes you would have access to the private members as well. But still, this would lead to very awkward programming. – Stefan Aug 20 '15 at 21:02
  • Appreciate your time! Will carry on learning, probably will get a book to get concepts back :) – сами J.D. Aug 21 '15 at 13:34
  • @Stefan - thanks for the link. – PhillipH Aug 24 '15 at 15:55

1 Answers1

4

I don't see any real benefit to using static here. I don't agree that your performance would be improved; you would still be doing the same thing.

Generally, statics are used for two reasons: (1) you want something to be a singleton object (for example, the location of some data shared by the entire app; or (2) the method involved does not alter any state of the class and therefore marked as static because this somewhat optimizes the creation of the class instance in memory (in a very minor way).

Jeff Prince
  • 658
  • 6
  • 13
  • 3
    `I don't agree that your performance would be improved`: maybe true in this case where OP needs to do some extra effort. But in general static functions a (slightly[i.e. not valid for OP here]) faster. See http://stackoverflow.com/questions/169378/method-can-be-made-static-but-should-it – Stefan Aug 20 '15 at 20:57
  • I like your definition. For the singleton object it's quite clear. For (2), I assume then that the method which is static is the one who doesn't need to do any operations with the Class information (variables, etc), it only needs to be called (maybe with parameters, but that's it, from there on, it will continue on its own without interacting with the class. Did I get it right? Thanks a lot! – сами J.D. Aug 20 '15 at 21:02
  • 2
    J.D. Yes, a static method has no "this" pointer and cannot modify the state of the class instance. I think when you have such methods it is a good practice to mark them as static for documentation purposes. – Jeff Prince Aug 20 '15 at 21:07