Expressions
Prefer expressions over statements.
Prefer logical operators to if statements
if (node.GetParenthesis(key))
{
item.Setting = false;
}
else
{
item.Setting = true;
}
Avoid if and ternary operator for logical expressions
Logical expressions have no branches, are more concise and always handle all the logical cases.
item.Setting = !GetParenthesis(key);
Use logical operators !
, &&
and ||
ternary operator
Prefer the ternary operator to simple if statements. The ternary operator is an expression in contrast to the if-statement and it forces you to handle all the cases.
We format the ternary operator like this, to see which branch we are dealing with easily.
return condition
? true-expression
: false-expression;
ternary operator
Switch Expression
Prefer switch expressions over switch statements.
return animalKind switch
{
AnimalKind.Dog => "dog",
AnimalKind.Cat => "cat",
_ => throw new InvalidOperationException($"Unsupported animal kind {animalKind}"),
};
switch expression
Expression-bodied Members
Use the expression body syntax when a member returns a single expression. Move the arrow to the next line when the expression gets too long.
public int Length => 0;
public string AbsolutePath()
=> Path.Combine(CalculateRootPath(), RelativePath);
expression-bodied members
Relational Patterns
We don't keep a space between the separator and the value in relational patterns.
string WaterState(int tempInFahrenheit)
=> tempInFahrenheit switch
{
>32 and <212 => "liquid",
<32 => "solid",
>212 => "gas",
32 => "solid/liquid transition",
212 => "liquid / gas transition",
};
switch expression with relational patterns