Skip to content

Commit 2f286d5

Browse files
author
Ruben Schmidmeister
committed
Merge remote-tracking branch 'origin/master' into add-links
2 parents a00bc45 + 7acb9de commit 2f286d5

File tree

3 files changed

+69
-66
lines changed

3 files changed

+69
-66
lines changed

documentation/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [Naming Conventions](./naming.md)
88
* [Variables](./variables.md)
99
* [Basic constructs](./basic-constructs.md)
10+
* [Expressions](./expressions.md)
1011
* [Types](./types.md)
1112
* [Functional Programming](./functional.md)
1213
* [Object oriented programming](./oop.md)

documentation/src/basic-constructs.md

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,6 @@
44

55
Multiple expressions or statements in one line increase the mental burden while reading them. Split them up to avoid mentally hiding statements.
66

7-
## Prefer logical operators to if statements
8-
9-
```csharp
10-
if (node.GetParenthesis(key))
11-
{
12-
item.Setting = false;
13-
}
14-
else
15-
{
16-
item.Setting = true;
17-
}
18-
```
19-
*Avoid if and ternary operator for logical expressions*
20-
21-
Logical expressions have no branches, are more concise and always handle all the logical cases.
22-
23-
```csharp
24-
item.Setting = !GetParenthesis(key);
25-
```
26-
*Use logical operators `!`, `&&` and `||`*
27-
28-
297
## If-statements
308

319
The condition of the if-statement should have no side effects.
@@ -59,50 +37,6 @@ if (!updateIsReady)
5937
```
6038
*Better alternative*
6139

62-
## ternary operator
63-
64-
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.
65-
66-
We format the ternary operator like this, to see which branch we are dealing with easily.
67-
68-
```csharp
69-
return condition
70-
? true-expression
71-
: false-expression;
72-
```
73-
*ternary operator*
74-
75-
## Switch Expression
76-
77-
Prefer [switch expressions] over switch statements.
78-
79-
```csharp
80-
return animalKind switch
81-
{
82-
AnimalKind.Dog => "dog",
83-
AnimalKind.Cat => "cat",
84-
_ => throw new InvalidOperationException($"Unsupported animal kind {animalKind}"),
85-
};
86-
```
87-
*switch expression*
88-
89-
[switch expressions]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression
90-
91-
## Expression-bodied Members
92-
93-
Use the [expression body syntax] when a member returns a single expression.
94-
Move the arrow to the next line when the expression gets too long.
95-
96-
```csharp
97-
public int Length => 0;
98-
99-
public string AbsolutePath()
100-
=> Path.Combine(CalculateRootPath(), RelativePath);
101-
```
102-
*expression-bodied members*
103-
104-
[expression body syntax]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members
105-
10640
## For-loops
10741

10842
Avoid for-loops and use LINQ and higher order functions as an alternative.

documentation/src/expressions.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Expressions
2+
3+
Prefer expressions over statements.
4+
5+
## Prefer logical operators to if statements
6+
7+
```csharp
8+
if (node.GetParenthesis(key))
9+
{
10+
item.Setting = false;
11+
}
12+
else
13+
{
14+
item.Setting = true;
15+
}
16+
```
17+
*Avoid if and ternary operator for logical expressions*
18+
19+
Logical expressions have no branches, are more concise and always handle all the logical cases.
20+
21+
```csharp
22+
item.Setting = !GetParenthesis(key);
23+
```
24+
*Use logical operators `!`, `&&` and `||`*
25+
26+
## ternary operator
27+
28+
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.
29+
30+
We format the ternary operator like this, to see which branch we are dealing with easily.
31+
32+
```csharp
33+
return condition
34+
? true-expression
35+
: false-expression;
36+
```
37+
*ternary operator*
38+
39+
## Switch Expression
40+
41+
Prefer [switch expressions] over switch statements.
42+
43+
```csharp
44+
return animalKind switch
45+
{
46+
AnimalKind.Dog => "dog",
47+
AnimalKind.Cat => "cat",
48+
_ => throw new InvalidOperationException($"Unsupported animal kind {animalKind}"),
49+
};
50+
```
51+
*switch expression*
52+
53+
[switch expressions]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression
54+
55+
## Expression-bodied Members
56+
57+
Use the [expression body syntax] when a member returns a single expression.
58+
Move the arrow to the next line when the expression gets too long.
59+
60+
```csharp
61+
public int Length => 0;
62+
63+
public string AbsolutePath()
64+
=> Path.Combine(CalculateRootPath(), RelativePath);
65+
```
66+
*expression-bodied members*
67+
68+
[expression body syntax]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members

0 commit comments

Comments
 (0)