Juval Lowy's C# Coding Standards Questions

后端 未结 8 2091
无人共我
无人共我 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:42

    2.29 Ternary operator

    To start with, if you start to use the ternary operator, there should be a reason for the use of the ternary operator over a regular if-then-else. Observe :

    if (x == 0) {...} else{...} //A set of statements demand a regular If-then-else
    
    //A simple assignment can be handled by the ternary operator
    y = (x == 0)? 1 : 0 //this is readable and how it should be used
    
    
    (x==0)? callSomething() : callSomethingElse() //this is NOT how it should be used
    

    The ternary statement is meant for returning one of two values depending upon the conditional it is evaluating. This is extremely handy when doing FP. For call statements that do not return a value, you should revert to if-then-else.

提交回复
热议问题