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
Post a Comment