0

Reviewing someone's code, in a non-static class, I noticed there were only private static methods. So those are used internally in its class.

interface IDogInterface
{
    void Bark(bool isBarking);
}

public class GoldenRevier : IDogInterface
{
    public void Bark(bool isBarking)
    {
        //Implementation...

        SomeMethod();
        SomeOtherMethod();
        AnotherMethod();
    }

    private static void SomeMethod();
    private static void SomeOtherMethod();
    private static void AnotherMethod();
}

Note: there are no static attributes, no getters or setters, only what you saw in the above code.

Why would you make those internal methods static ? I see no benefit doing this.

I see bad logic in the above code... Am I missing something ?

EDIT:

Code edit to highlight the accepted answer meaning.

public class GoldenRevier : IDogInterface
{
    private Bark _bark = new Bark();

    public void Bark(bool isBarking)
    {
        if(isBarking)
        {
            _bark.SetLoudness(5);
            _bark.Start();
        }
        else
        {
            JustChilling();
        }
    }

    private static void JustChilling() 
    { 
         Console.Write("No barking today, just chilling!");
    }
}
  • are you thinking that they don't need to be static, if they are only used internally? – Joe Nov 04 '17 at 12:57
  • On the other hand, is there any benefit in marking them non-static? – harold Nov 04 '17 at 12:58
  • @Joe yes, that's what I would do it if no need of static attributes, just internal use and so on. – Adrian Chiritescu Nov 04 '17 at 12:58
  • @harold It is about being object oriented as much as possible. Static methods breaks the rules in some way so that's why I would suggest making them non-static if there is no need to make them static. – Adrian Chiritescu Nov 04 '17 at 13:02
  • That would just be fake object orientation. These functions apparently don't use the instance, making it *seem* as if they do does not fundamentally change the structure of the program. – harold Nov 04 '17 at 13:03

2 Answers2

3

you make static methods private in order to restrict its visibility to the containing class. this is a common case when that's the only place you're using the method. Also, if a method is never going to utilise any of the objects state then there is no reason to not make it static.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
2

Its a coding style. If a method does not use any properties of the class you can make it static. If it is only used inside that class you can make it private. So you can make them private static if they do not use Properties of the class and are not reuseable elsewhere. You could leave them without the static as well.

So its a style questions - do you force methods to be static if they can be and see no reuseability you make em private static.

For readability / code overview: it tells a casual reader/reviewer: no dependencies (yet) on this method outside of this class and does not use any class properties.

It might help to keep your internal methods cleaner and shorter (clean code), fit DRY and with a speaking name simplifies readability.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69