Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better? private static vs private

Tags:

c#

.net

In this code sample:

public class SuperMan {
    private static bool IsProper(decimal x) {
        return x > 31.0m && x < 45.0m;
    }

    public bool CheckStuff(string a, string b, string c) {
        // lots of code, some of which introduces a variable x
        return IsProper(x) && /* other conditions */;
    }
}

Should IsProper(..) be a 'private static' or a 'private'. Assuming:

  1. IsProper(..) doesn't need to access any instance state (even in future.)
  2. We are not concerned about performance different between the two options (one of the things we should never do is guess about performance without actual measurement and optimize without the need.)
like image 904
kidoman Avatar asked Jan 15 '11 09:01

kidoman


People also ask

What is the difference between private and private static?

A private variable is only accessible inside the class. A static variable belongs to the class rather than to an instance of a class. Notice that the variable DEPARTMENT is also final , which means that it cannot be modified once it is set.

Are private static methods bad?

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.

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's the point of private static?

Private static variables are useful in the same way that private instance variables are useful: they store state which is accessed only by code within the same class. The accessibility (private/public/etc) and the instance/static nature of the variable are entirely orthogonal concepts.


2 Answers

It could be static, since it doesn't seem to have to do anything with the SuperMan class nor its members. But you should ask yourself if that function belongs in that class at all.

If you're checking if decimal is a proper decimal for SuperMan, then it belongs there. But I wouldn't make it static in that case. Chances are that you will later need to replace that constant values with SuperMan properties.

like image 164
GolezTrol Avatar answered Oct 02 '22 11:10

GolezTrol


Reasons to make IsProper an instance member:

  • IsProper needs another implementation in an inherited class
  • IsProper might need access to members in the future

Reasons to make IsProper a static member:

  • You have a small performance penalty for making it an instance member.
  • If you only need to create an instance of the class to call IsProper, you would make it a static
like image 20
GvS Avatar answered Oct 02 '22 13:10

GvS