How to decide between C# static and non-static methods?

前端 未结 11 878
闹比i
闹比i 2020-12-24 03:35

[Edit]

My original-question was \"Why to decide between static and non-static? Both do the same...\"

Unfortunately it was edited to a C#-specific question wh

相关标签:
11条回答
  • 2020-12-24 03:59

    If you use any other objects then you shokld default to instance level methods so that you can configure those dependencies using Dependancy Injection.

    For example, if one of those images was an SVG image then you may have a dependency on an XML parser which (in Java at least) have many implementations, likewise for SVG renderers I imagine and many other constituent image types may require similar arrangements that evolve as the state of the object evolves or which must be changed in different usage scenarios (e.g. test, production, different projects re-using your code).

    The blinking amber warning light is that you may be using classes that are not part of your framework's default libraries, so you've made a choice of a third party component and if using statics you are not well placed to modify that decision.

    A useful "redline" is that if you touch another process (database server, web service etc) then I'd regard a static method as bad 100% of the time as this makes unit testing more dificult.

    0 讨论(0)
  • 2020-12-24 04:07

    You cannot use static methods to implement an interface, and you cannot override static methods. So using static methods means that you are simply not doing OOP.

    Think about how you would implement the following functionality using only static methods?

    interface IDocument 
    {
       void Print(IDevice targetDevice);
    }
    
    IDocument instance;
    
    instance = new PdfDocument();
    instance.Print(printer);
    
    instance = new WordDocument();
    instance.Print(printer);
    
    0 讨论(0)
  • 2020-12-24 04:09

    In general, when programming using an OO mindset, you're going to want to avoid using static methods. In OOP, the idea is to represent everything as objects, and to give each object a clear set of abilities that represents its core abstraction. Static methods "break" this abstraction.

    Your example talking about a Document class with a copy method is a prime example. I would argue that the correct OO implementation is the first way. That is, to have copy as an instance method like this:

    document1.copy(toPath)
    

    It makes sense that the ability to copy itself is part of a Documents core abstraction. In this way, the client code sending the copy message only has to specify where to copy to, because it is understood that a Document keeps track of where it is located internally. There is no need for that information to be replicated anywhere else, which is a major problem with the third option you present that looks like this:

    Document.copy(fromPath, toPath)
    
    0 讨论(0)
  • 2020-12-24 04:11

    Here we go.

    First off:

    So I tend to use static methods very often (to be independent from a concrete instance - independency is always good thing).

    Quite the contrary: when using static methods you're very dependent on the concrete instance.

    As far as your Document is concerned, I'd go neither way. You've listed all responsibilities of Document class, which includes aggregation of data, saving itself to the database plus operations on pages and copying.

    This is way to much. Per SRP, each "module" (here "module" used as a catch-all term) should have only one reason to change. Your Document has lots of responsibilities, hence it has a whole slew of reasons to change. This is no good.

    With that in mind, I'd move all logic to other classes with strictly defined responsibilities. A more or less accepted criterion of what to move was introduced, I believe, by either Herb Sutter or Andrei Alexandrescu, an is as follows: all operations (think methods) that can be performed with an object through its public contract should be moved outside the object in question.


    0 讨论(0)
  • 2020-12-24 04:12

    My "rule" is:

    • If I don't need to use properties from my class, make it static. (in other words, if the method is not really attached to the class, just there for logic association, use static )
    0 讨论(0)
  • 2020-12-24 04:13

    Same as altCongnito, and i will add that fileObject.Copy everybody will use, more than the object fileObject. Static for a function that have ideal relationship with the class and not functional dependency of it.

    0 讨论(0)
提交回复
热议问题