Advantages to Using Private Static Methods

Advantages to Using Private Static Methods

From the FxCop rule page on this:

After you mark the methods as static, the compiler will emit non-virtual call sites to these members. Emitting non-virtual call sites will prevent a check at runtime for each call that ensures that the current object pointer is non-null. This can result in a measurable performance gain for performance-sensitive code. In some cases, the failure to access the current object instance represents a correctness issue.

What is the usage of private static method in c#?

As per MSDN

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.

https://msdn.microsoft.com/en-us/library/ms245046.aspx

One more benefit is calling sequence, when you call an instance method, code generated will push instance of this onto stack as first parameter and rest of the parameters for the method will be pushed onto the stack. So every instance method call requires one more additional stack push for this in along with other method parameters.

If you convert your method to static, static method calls do not require this so one less push operation for CPU. It doesn't seem big benefit for single call.

But if your method will be used very frequently and if you have couple of methods that do not require this then it can save significant CPU time, especially in graphics and scientific calculations.

This is the reason Resharper is suggesting you to change method to static when method does not reference anything that is part of this.

Here is the sample,

    public int Add(int a, int b) {
return a + b;
}

public static int StaticAdd(int a, int b) {
return a + b;
}

public void InstanceAdd() {
Console.WriteLine(this.Add(3,3));
}

public void InstanceAddStatic()
{
Console.WriteLine(StaticAdd(3, 3));
}

This is il generated for calling an instance method in "InstanceAdd"

.method public hidebysig 
instance void InstanceAdd () cil managed
{
// Method begins at RVA 0x2095
// Code size 16 (0x10)
.maxstack 8

IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.3
IL_0003: ldc.i4.3
IL_0004: call instance int32 Temp.MathTest::Add(int32, int32)
IL_0009: call void [System.Console]System.Console::WriteLine(int32)
IL_000e: nop
IL_000f: ret
} // end of method MathTest::InstanceAdd

and this is the il generated for instance method in "StaticAdd"

.method public hidebysig 
instance void InstanceAddStatic () cil managed
{
// Method begins at RVA 0x20a6
// Code size 15 (0xf)
.maxstack 8

IL_0000: nop
IL_0001: ldc.i4.3
IL_0002: ldc.i4.3
IL_0003: call int32 Temp.MathTest::StaticAdd(int32, int32)
IL_0008: call void [System.Console]System.Console::WriteLine(int32)
IL_000d: nop
IL_000e: ret
} // end of method MathTest::InstanceAddStatic

If you look at "StaticAdd", there is no ldarg.0, which is this. For every method call, there will always be ldarg.0 as first instruction and then rest of the parameters will follow.

Assuming no class variables are involved, what are the advantages of using a private static method over a private method in Java

In my opinion, the primary use cases for private static methods are reuse and readability of public static methods.

Readability
Suppose you have a large public static method with many branches, then it might be beneficial to readability to have each branch handled by a private static method. This is the approach advocated by books such as Clean Code.

Reuse
Suppose you have a number of public static methods with duplicated code, then it is usually a good idea to place the duplicated code in a private static method (unless of course the duplicated code is useful as a utility method in its own right).

PHP | Why should I use public static / private static function instead of public / private function?

"Normal" methods (usually called instance methods) are invoked on an instance of the class in which they're defined. The method will always have access to its object via $this, and so it can work with data carried by that object (and indeed modify it). This is a core aspect of object oriented programming, and it's what makes a class more than just a bunch of data.

Calls to static methods, on the other hand, aren't associated with a particular object. They behave just like regular functions in this respect; indeed the only difference is that they may be marked private and also have access to private methods and variables on instances of own their class. Static functions are really just an extension of procedural programming.

For example, an instance method is called on an object:

$object = new MyClass();
$result = $object->myInstanceMethod();

A static method is called on the class itself:

$result = MyClass::myStaticMethod();

java - What benefits can delegation to private static methods have over private methods in a springboot webflux (functional program style)

I don't think this question is related to WebFlux and to Spring in general, but rather to any code written in a functional style (you'll face the same with java 8 streams) and even to a code written in a "traditional" imperative style: what to call private static or private method?

In general I can see only one difference between private static methods and private methods:

private non-static methods can access the state/dependencies (non static data fields) of the class, whereas private static methods can't

This means that you might want to use private static methods if:

  • You only use them within the class
  • You want to emphasize that the method doesn't depend (read doesn't access) the internal state of the object its defined in.

On the opposite, if you already maintain the state/dependency, like WebClient in the example, then using the static makes you passing this parameter to the method. In My Opinion this is unnecessary: what if you have not 1, but say 3 parameters to pass and you call this method from many places in the source class.

Other than that I don't see much difference:

  • Mocks (testing) is just the same because you don't test private methods anyway and you can mock your dependencies in both cases in a same way by using constructor injection or some advanced techniques of mockito.

  • Performance penalty - roughly the same, I can't think about the case where the usage of one method over another will be a source of performance issues.

  • Maintainability - again, depends on your usecase, if the method is accessing the internal state you better use non static variant to reduce the clutter, if you want to emphasize that you use a "util" method that doesn't depend on the state - use private static method.

  • Multithreading - you should synchronize the state in both cases if its required, the fact that the method has static modifier by itself doesn't deal with multithreading at all.

I've also found this thread that can be useful for this question, again its applicable for general programming style (code organization) not only for webflux.

private static and private methods dilemma

If performance would be the entire purpose of static, then of course you could make anything static. But actually the keyword isn´t about performance, it´s about semantics - that is does the member belong to the class (and thus all instances), or to a specific instance of your class?

Imagine you´d create multiple instances of your class. If every instance had a _key, you surely need an instance-field, not a static one. If on the other hand all instances share the same key, you could make it static of course.

After all performance is just a side-effect of the keyword and you should never make design-decisions on pure performance-considerations.

Why would you mark static methods as private if used just internally?

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.

Private static methods. Generally good idea?

This seems burdensome.

When you consider that the entire chain of function calls becomes dependent upon an instance when you modify your DoSomethingMore method, the feeling of the changes being "burdensome" should become "necessary" instead.

What you should first consider are the semantics of the methods you're writing. When you write a method, you need to ask yourself whether the code should be an instance method (because it should semantically be an action associated with a single instance) or a static method (for anything else) and that decision should drive how you write the method.

Until you have measured performance shortcomings that are unacceptable in production code, you should make the method instance or static depending on what makes sense for your domain, not anything else.



Related Topics



Leave a reply



Submit