DEV Community

Cesar Aguirre
Cesar Aguirre

Posted on • Updated on

Two CSharp idioms

You have heard about Pythonic code? Languages have an expressive or "native" way of doing things. But, what about C#? Is there C-Sharpic code?

In this series of posts, I wil attempt to present some idioms or "expressions" to write more expressive C# code. I collected these idioms after reviewing code and getting mine reviewed too.

In this first part, you have two useful C# idioms on conditionals and its alternative solutions.

Instead of lots of or’s, use an array of possible values

Use an array of known or correct values, instead of a bunch of comparison inside an if statement. You can find this code when checking preconditions or validating objects.


if (myVar == 2 || myVar == 5 || myVar == 10)
Enter fullscreen mode Exit fullscreen mode


var allowedValues = new int[] { 2, 5, 10 };
if (allowedValues.Any(t => myVar == t))
Enter fullscreen mode Exit fullscreen mode

If you need to check for a new value, you add it in the array instead of adding a new condition in the if statement.

Instead of lots of if's to find a value, use an array of Func

Replace consecutive if statements to find a value with an array of Func or small choice functions. Then, pick the first result different from a default value or null. You can find this code when finding a value among multiple choices.


var someKey = FindKey();
if (someKey == null)
    someKey = FindAlternateKey();
if (someKey == null)
    someKey = FindDefaultKey();
Enter fullscreen mode Exit fullscreen mode


var fallback = new List<Func<SomeObject>>
var someKey = fallback.FirstOrDefault(t => t != null);
Enter fullscreen mode Exit fullscreen mode

Also, you could take advantage of the Null Coleasing Operator (??) if these choice functions return null when a value isn't found.

var someKey = FindKey() ?? FindAlternateKey() ?? FindDefaultKey();
Enter fullscreen mode Exit fullscreen mode

Similarly, if you need to add a new alternative, either you add it in the array or nest it instead of adding the new alternative in the if statement.

Voilà! These are our first two idioms on conditionals. I have found these two idioms more readable in some scenarios. But, don't start to rewrite or refactor your code to follow any convention you find online. Make sure to follow the conventions in your own codebase, first.

Happy coding!

Top comments (3)

serinus1 profile image

Granted, they're just examples, but in the examples given the first solution is better. It's immediately obvious what you're doing to any programmer of any skill level from any language, and they take fewer lines.

The "idioms" as you call them can be useful tricks, but they require overhead knowledge. More developers are familiar with "or" and boolean logic than lambdas and linq. I would avoid using these tricks in the examples given. I would look to use these if they reduced, say 50 lines into 5 lines. In that case the overhead knowledge requirement is serving a purpose.

You always should have a reason to be clever and don't just try to be clever for the sake of it. Keep it simple and stupid whenever possible.

reidterror profile image

For the first one, would the performance take a hit?

canro91 profile image
Cesar Aguirre

I don't think so (unless you're working with lots of options, in that case you would need another abstraction). It strives for readability: make it work, clean and then fast