Juval Lowy's C# Coding Standards Questions

后端 未结 8 2182
无人共我
无人共我 2021-01-30 18:07

I enjoy and highly recommend Juval Lowy\'s - C# Coding Standard. Juval explicitly avoids rationale for each directive in order to keep the standard tight (see the preface). Howe

8条回答
  •  不要未来只要你来
    2021-01-30 18:25

    Obviously, I'm not Juval, but I can take a stab at these

    1.13 Avoid fully qualified type names. Use the "using" statement instead.

    Performance can't be the issue here. I'm sure the issue is readability.

    1.26 Use empty parenthesis on parameterless-anonymous methods. Omit the parenthesis only if the anonymous method could have been used on any delegate.

    public delegate void Foo1();
    public delegate void Foo2(int val);
    
    public void Foo()
    {
        Foo1 first = delegate { Console.WriteLine("Hello world"); };
        Foo2 second = delegate { Console.WriteLine("Hello world"); };
        Foo1 third = delegate() { Console.WriteLine("Hello world"); };
        Foo2 fourth = delegate() { Console.WriteLine("Hello world"); }; // does not compile
    }
    

    Without the parens, the anonymous delegate can be applied to any delegate. With the parens, you're being specific about the signature of the delegate. Prefer the second syntax unless you really need the flexibility.

    2.19 Avoid defining custom exception classes

    Again, readability is the issue here. The framework exception classes are rich and well-understood. Be wary when replacing them.

    2.29 Avoid using the ternary conditional operator

    It's a readability and expandability thing. I don't really agree, but it's a standard religious fight.

    2.31 Avoid function calls in Boolean conditional statements. Assign into local variables and check on them.

    Partially this is readability, and partially it's for ease of debugging. I've starting to assign almost everything to temporary variables just so that they're easily found in the debugger later on.

    2.47 Avoid interfaces with one member.

    "Avoid" is kinda like "prefer", he's just saying think twice before you do it. If you only have one member, is the interface really modeling something useful and complete in your design? It's pretty rare to have a class with just one member, think seriously about why your interface is any different.

    2.53 Prefer using explicit interface implementation

    This is similar to the idea of using the least-public accessor you can. If your class doesn't need to make the interface public, then it probably shouldn't. This is going to differ significantly based on your design, obviously, but given the fact that most people just make the interface implicit without really thinking about it, it's advice worth considering.

提交回复
热议问题