Using Private Static Method to Boost Your Code Quality


C# deals with two types of methods, namely static and instance methods (non-static methods).

The static method does not require objects or instances. The methods get called directly from the class name (ClassName.StaticMethodName()).

But what if you want to make these methods private?

Private static methods are methods that can only be accessed within the same class. They are usually used to encapsulate code only used by the class itself, but they don’t use the instance members. Having private static methods can improve the maintainability of your code by making it more modular.

Developers often question the benefits of private static methods in code and are unsure of their usage. So, in this post, let’s look at why you should have private static methods in your code.

Why have Private Static Methods in Code?

Private static methods are methods that can only be accessed from within the same class. These methods are usually used to perform internal operations for the class. This can be initializing data structures or checking for valid input.

Private static methods can be called from other static or instance method in the same class, but they cannot be called from outside the class. The private static method can only access other static members, such as:

  • static property
  • static field
  • static method

In C#, the private static class can’t access other instance members, nor reference the this keyword.

The class itself doesn’t have to be a static class to have the private static method.

Here are a few reasons for using them.

They are a utility

One of the reasons you should use private static methods is to reduce the complexity of the code. For instance, if there are multiple public methods in your code, there are multiple ways the program can manipulate the objects. Hence, error correction and debugging become extremely difficult.

On the other hand, having a private static method only visible from inside a class means the interface of the class reduces. In addition, this leads to a much simpler and more convenient code structure. So, you should declare a method ‘private’ whenever possible and use this utility well.

They cannot be accessed by other classes

Since private static methods help you hide portions of the code internal to the class, they are a tool that should not go wasted. For instance, while working with a handful of developers on a project, you need to ensure they don’t accidentally access something meant to remain unedited. In this case, the methods you must keep untouched at all costs should be private instead of public.

They help organize the code into smaller pieces

In general, static methods are called using the class name. In this way, they cut down multiple lines of code that would, otherwise, create instances and objects of the methods.

Furthermore, a good practice is to call private static methods inside a public static method instead of writing a public method that goes on and on. By calling multiple private methods, you increase the readability of the code as it breaks down into smaller pieces.

Benefits of having private static methods in C#

In .NET, private static methods provide code optimization. If the method is not calling any instance methods or accessing any instance data of the class, you should declare it ‘static’. If it is internal to the class, it must be ‘private static’.

Once you have private static methods, the compiler will issue nonvirtual calls for this method.

In this way, the compiler will minus a check it does to see if the object pointer is null or not. Since for each private static method, it will skip this check, the code will perform much better. For a larger codebase, this can result in significant improvement in performance.

Private static method and collection of related private static methods provide opportunity for refactoring

If a method is public, any member can access it. So, to change or delete this method, you would need to do it at every place inside the code where the method has been called. However, that’s not the case with private static methods.

You cannot invoke private static methods from outside the class, which allows them to be refactored freely. Furthermore, as they are internal to the class, there is zero chance of breaking or affecting any outside code while refactoring. Hence, a private static method provides a good opportunity for hassle-free refactoring.

The thing is, if you see a couple or similar private static methods performing a related task, you can perform the Extract Class refactoring to move them to a separate class.

How to test the private static method?

If this is a question you’ve been asking – don’t do it!

You should not test private static methods. Private static methods are not part of the class contract, nor do they communicate information to the external environment. They merely carry implementation logic and are called by public methods to make the code reusable and readable.

Hence, running unit tests on these methods will only be a waste of time and might lead to you breaking the code every time you try to make a change.

A better approach would be testing public methods calling the private static methods. This will reduce the number of tests, and if the test is a success, the private methods are working correctly too!

Refactor private static method using Method Object refactoring

In general, when you refactor using the Extract Method, Visual Studio would generate a private static method. But, how should you refactor a private static method?

In this case, the Extract Method doesn’t work because taking out simple chunks of code is not easy. However, you can go for the Method Object refactoring technique. In this refactoring you:

  1. Create a new class
  2. Create a private field for each local variable in the private static method.
  3. Create a constructor and give it the parameters of your private static method.
  4. Create a main() method or an instance method containing the body of the private static method.
  5. Replace all the local variables with the private fields.
  6. Create a method object in the body of the private static method that calls the new method.

It makes the code more readable. However, its biggest advantage is that it allows you to test the private methods as you only need to know how the new class should work.

Conclusion

Private Static Methods can make your code reusable, readable, and convenient to debug. They lead to performance gains and also indicate where refactoring is possible. However, you should not test these methods directly. Instead, try testing the public method, which is called the private static method.

You will need to refactor the private static method using a method object and a new class to be able to test these methods indirectly. While many programmers are wary of them, these methods will facilitate you in many ways along your coding journey, from code optimization to duplication removal.

Recent Posts