You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fsharp/style-guide/formatting.md
+68-2Lines changed: 68 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -597,6 +597,18 @@ Write them on one line when:
597
597
if cond then e1 else e2
598
598
```
599
599
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
+
600
612
If any of the expressions are multi-line or `if/then/else` expressions.
601
613
602
614
```fsharp
@@ -631,7 +643,8 @@ else
631
643
e4
632
644
```
633
645
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.
635
648
636
649
```fsharp
637
650
// ✔️ OK, but better to refactor, see below
@@ -640,10 +653,31 @@ if
640
653
|| someFunctionToCall
641
654
aVeryLongParameterNameOne
642
655
aVeryLongParameterNameTwo
643
-
aVeryLongParameterNameThree then
656
+
aVeryLongParameterNameThree
657
+
then
644
658
e1
645
659
else
646
660
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
647
681
```
648
682
649
683
It is, however, better style to refactor long conditions to a let binding or separate function:
@@ -906,6 +940,38 @@ match lam with
906
940
sizeLambda lam1 + sizeLambda lam2
907
941
```
908
942
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
+
909
975
Aligning the arrows of a pattern match should be avoided.
0 commit comments