Skip to content

Commit ba1af57

Browse files
committed
Update guide for long if or match expressions.
1 parent d73c1e1 commit ba1af57

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

docs/fsharp/style-guide/formatting.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,18 @@ Write them on one line when:
597597
if cond then e1 else e2
598598
```
599599

600+
If the else expression is absent, it is recommended to never to write the entire expression in one line.
601+
This is to differentiate the imperative code from the functional.
602+
603+
```fsharp
604+
// ✔️ OK
605+
if a then
606+
()
607+
608+
// ❌ Not OK, code formatters will reformat to the above by default
609+
if a then ()
610+
```
611+
600612
If any of the expressions are multi-line or `if/then/else` expressions.
601613

602614
```fsharp
@@ -631,7 +643,8 @@ else
631643
e4
632644
```
633645

634-
If a condition is long, the `then` keyword is still placed at the end of the expression.
646+
If a condition is multiline or exceeds the default tolerance of the single-line, the condition expression should use one indentation and a new line.
647+
The `if` and `then` keyword should align when encapsulating the long condition expression.
635648

636649
```fsharp
637650
// ✔️ OK, but better to refactor, see below
@@ -640,10 +653,31 @@ if
640653
|| someFunctionToCall
641654
aVeryLongParameterNameOne
642655
aVeryLongParameterNameTwo
643-
aVeryLongParameterNameThree then
656+
aVeryLongParameterNameThree
657+
then
644658
e1
645659
else
646660
e2
661+
662+
// ✔️The same applies to nested `elif` or `else if` expressions
663+
if a then
664+
b
665+
elif
666+
someLongFunctionCall
667+
argumentOne
668+
argumentTwo
669+
argumentThree
670+
argumentFour
671+
then
672+
c
673+
else if
674+
someOtherLongFunctionCall
675+
argumentOne
676+
argumentTwo
677+
argumentThree
678+
argumentFour
679+
then
680+
d
647681
```
648682

649683
It is, however, better style to refactor long conditions to a let binding or separate function:
@@ -906,6 +940,38 @@ match lam with
906940
sizeLambda lam1 + sizeLambda lam2
907941
```
908942

943+
Similar to large if conditions, if a match expression is multiline or exceeds the default tolerance of the single-line, the match expression should use one indentation and a new line.
944+
The `match` and `with` keyword should align when encapsulating the long match expression.
945+
946+
```fsharp
947+
// ✔️ OK, but better to refactor, see below
948+
match
949+
complexExpression a b && env.IsDevelopment()
950+
|| someFunctionToCall
951+
aVeryLongParameterNameOne
952+
aVeryLongParameterNameTwo
953+
aVeryLongParameterNameThree
954+
with
955+
| X y -> y
956+
| _ -> 0
957+
```
958+
959+
It is, however, better style to refactor long match expressions to a let binding or separate function:
960+
961+
```fsharp
962+
// ✔️ OK
963+
let performAction =
964+
complexExpression a b && env.IsDevelopment()
965+
|| someFunctionToCall
966+
aVeryLongParameterNameOne
967+
aVeryLongParameterNameTwo
968+
aVeryLongParameterNameThree
969+
970+
match performAction with
971+
| X y -> y
972+
| _ -> 0
973+
```
974+
909975
Aligning the arrows of a pattern match should be avoided.
910976

911977
```fsharp

0 commit comments

Comments
 (0)