Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does it make sense to use private static methods in instance class [duplicate]

Ive inherited some code that are a regular class with some private static methods in them. The code (pseudo code) looks like this

public class Animal
{
    private string typeOfAnimal;

    public Animal(string typeOfAnimal)
    {
        this.typeOfAnimal = typeOfAnimal;
    }

    public void MakeSound()
    {
        var sound = Animal.GetSound(typeOfAnimal);

        // Make use of sound here       
    }

    private static string GetSound(string typeOfAnimal)
    {
        if(typeOfAnimal  == "dog")
            return "bark";
        else if(typeOfAnimal == "cat")
            return "mjau";
    }
}

Is there any benefit in doing like this compared to making GetSound a regular instance method?

like image 428
Daniel Hansen Avatar asked Oct 19 '15 12:10

Daniel Hansen


People also ask

Should static methods be private or public?

private or public doesn't make a difference - static methods are OK, but if you find you're using them all the time (and of course instance methods that don't access any instance fields are basically static methods for this purpose), then you probably need to rethink the design.

What is the use of private static method in C#?

A private constructor prevents the class from being instantiated. The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.

When would you use a private static method?

One of the reasons you should use private static methods is to reduce the complexity of the code. For instance, if there are multiple public methods in your code, there are multiple ways the program can manipulate the objects. Hence, error correction and debugging become extremely difficult.

Is static method faster than instance method?

The static method can call without object while instance method can not be called without object. The execution time of instance method is faster than static method but in the PHP version 5.3 there was a bug which says that static methods are faster in which they have introduced late binding.


2 Answers

There is some very minor performance difference in static methods, I think that is actually something the SO guys take advantage of. Also, making the method static gets you a slight readability improvement because of what the keyword implies.

My take on this is usually readability. In this case there are two differences: instances vs static, public vs private. Neither is inherently more beneficial than the other, the benefits only appear depending on intended use. In your case, it has no value being a public method and isn't part of the public API of the type so you make it private, and doesn't want to mutate instance state so you make it static.

By default, ReSharper highlights methods that can be made static.

like image 163
Adam Houldsworth Avatar answered Sep 18 '22 05:09

Adam Houldsworth


It is advisable to mark your private methods as static if they are not using any of the instance object for slightly better performance and readability.

Infact the following warning in code analysis is shown if such methods are not marked as private.

CA1822: Mark members as static

Extract from the link -

Members that do not access instance data or call instance methods can be marked as static (Shared in Visual Basic). After you mark the methods as static, the compiler will emit nonvirtual call sites to these members. Emitting nonvirtual call sites will prevent a check at runtime for each call that makes sure that the current object pointer is non-null. This can achieve a measurable performance gain for performance-sensitive code. In some cases, the failure to access the current object instance represents a correctness issue.

like image 25
Yogi Avatar answered Sep 18 '22 05:09

Yogi