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!"); } } 
5
  • are you thinking that they don't need to be static, if they are only used internally? Commented Nov 4, 2017 at 12:57
  • On the other hand, is there any benefit in marking them non-static? Commented Nov 4, 2017 at 12:58
  • @Joe yes, that's what I would do it if no need of static attributes, just internal use and so on. Commented Nov 4, 2017 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. Commented Nov 4, 2017 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. Commented Nov 4, 2017 at 13:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I've just edited my question (code part) please have a look and tell me if that's what you mean regarding your last remark: "Also, if a method is never going to utilise any of the objects state then there is no reason to not make it static"
Bark() is using related objects so it is non-static. On the other hand, JustChilling() is not using related objects, so it can be static, am I right ?
correct that's what i mean. also as I side note in the future please don't remove code that you've already posted.
OK, I will put back the previous code and add the new one :)
I added the old code back where it was. Thank you !
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.