Hi-programs! :) I have a question: when is it preferable to create static functions, and when not? For example, in the case when a function uses non-static class fields, it is better to make it static and all the necessary parameters to pass, or leave it non-static, and then nothing should be passed to it, but simply refer to the class fields?
- one"Non-static class fields" is a sort of class instance field. If you work with them, it is better not to declare a static method. And if functions are needed in the traditional sense of the word (for example, math: sine, cosine), then static. For the concept of function itself has already been replaced by a static class method (which at first seems strange) - alexlz
- one> which at first seems strange, consider it just as a namespace. - karmadro4
2 answers
Primitive explanation
Static functions should be used when you do not want to create an instance of a class. This is, for example, an auxiliary class. If the object is created, that is, the new operator is used:
SomeClass element = new SomeClass();
It seems to me that in this example it makes no sense to create a static method.
For advanced
Read this article: Seven Tips for Using Static Fields, Methods, and Classes
And this is the conclusion of the above article:
When adding static members to a type, you need to evaluate what it will lead to during the development period and at runtime. When developing it is necessary to carefully choose which members will be static, and strive to preserve the object-oriented architecture. Try not to mix heterogeneous functionality in one class: a class should not be a dump of static methods that are not related to each other. If a class contains only static members, remember to mark the class as static or sealed (sealed / NotInheritable) and define a private class constructor to make it impossible to create an instance.
Also, remember that static classes that maintain state between method calls should be safe in a multi-threaded environment by default. When you develop a class that is safe in a multi-threaded environment, you need special care when implementing and testing the class. Before you start creating such a class, ask yourself if these additional costs really are absolutely necessary.
Security in a multi-threaded environment affects application performance. In this article, you saw how performance might decrease due to the fact that the class has a static constructor. Assessing the impact on performance is especially important if you are writing a reusable library. It is never known beforehand whether someone will apply your class in the worst case, similar to that described at the beginning of the article.
Finally, try to carefully document your static classes. It is usually understandable when static methods are given the names of typical operations, as in the case of the static Open and Copy methods of the System.IO.FileClass class. If you implement a non-trivial class that maintains state between method calls, class users will be interested in details related to performance and security in a multi-threaded environment. The more class information you provide in advance, the less time you will spend on troubleshooting later. *
- 2Very primitive explanation. - Olter
- And if my function does not use any non-static internal fields, and receives all data in parameters, then it would be more logical for mine to make it static. Or am I wrong? - Donil
- oneAt least for reasons "to create an unnecessary instance of the class to call this function"? - alexlz
- @Danil, static functions in C # are some analogue of pure functions, i.e. functions without side effects that depend only on the parameters passed (with the same parameters always get the same result), therefore you are right - Specter
- 2By making functions as static as possible, you are slipping into procedural programming and you cannot use OOP in OO language — a common mistake for newbies. - wind
Static functions are used for convenience, in fact they replace the design:
MyClass MyVar = new MyClass ();
MyVar.DoSmth ();
on
MyClass.DoSmth ();
which in some cases is shorter.
For C # with the garbage collector, this is not so important, since the construction is not very cumbersome, but Delphi, for example, has a lot more gain in terms of code size.
In terms of performance, static methods are also usually preferable, but you should not chase performance to the detriment of clarity and readability of the code.