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:
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.
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.
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.
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.
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.
Reasons to make IsProper an instance member:
Reasons to make IsProper a static member:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With