According to anti-if campaign it is a best practice not to use ifs in our code. Can anyone tell me if it possible to get rid of the if in this piece of code ? (switch is also
Abuse the ternary operator, at least in C#:
Action result = s == "bar" ? (Action)(() => { Console.WriteLine("bar"); }): s == "foo" ? (Action)(() => { Console.WriteLine("foo"); }) : (Action)(() => { Console.WriteLine(); });
Actually, I take that back... never EVER do this. Use a switch.