c# - Simplifing a nested IF statement -


i have function following

string foo(bool a, bool b) {     if(a)     {         if(b)         {             return "w";         }         else         {             return "x";         }     }     else     {         if(b)         {             return "y";         }         else         {             return "z";         }     } } 

that double nesting feels wrong me. there better way implement pattern?


thank helping, end going trinary route. helped turn this:

if (female) {     if (nutered)     {         destrow["targetsex"] = "fs";     }     else     {         destrow["targetsex"] = "f";     } } else {     if (nutered)     {         destrow["targetsex"] = "mn";     }     else     {         destrow["targetsex"] = "m";     } } 

in this

destrow["targetsex"] = female ? (nutered ? "fs" : "f")                               : (nutered ? "mn" : "m"); 

if (a) {     return b ? "w" : "x"; } return b ? "y" : "z"; 

or more terse:

return ? (b ? "w" : "x")            : (b ? "y" : "z"); 

if going exclusively unnested conditions:

if (a && b) return "w"; if (a && !b) return "x"; return b ? "y" : "z"; 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -