Rules

Declarations order

In a file, the declarations are ordered, from top to bottom. β†’ Declaration comes before the usages.

In a .fsproj, the files are ordered too. β†’ We can import only something previously declared.

Type inference works by proximity: the closest match will be used.

Balance:

  • Pros:

    • no cyclic dependencies

    • faster and more predictable compilation

    • code easier to reason about

  • Cons:

    • need more coding discipline

Example: we try to use the fn before its declaration:

let result = fn 2
//           ~~ πŸ’₯ Error FS0039: The value or constructor 'fn' is not defined.

let fn i = i + 1

Indentation

  • In general, indentation is very important for code readability: β†’ It creates visual structures that match the logical structure, the hierarchy.

  • The indentation is optional in Cβ™― because it's the combination of curly braces { } and semi-colons ; that defines the logical blocks. β†’ These characters don't help the readability by themselves. It's the indentation that matters, then the curly braces can help to improve the block visual separation. β†’ More importantly, a code not properly indented can be misinterpreted, that can lead to bugs!

  • Indentation in Fβ™― is required:

    • It's the only way to define code blocks

    • Compiler ensures that the indentation is correct so the reader can really trust the indentation to understand the code structure.

πŸ‘‰ Conclusion: Fβ™― forces us to do what matters the most for the code readability πŸ‘

Vertical line of indentation

It's a concept a little more tricky, related to how Fβ™― understands the indentation.

  • In general, a block starts in a new line, at a greater indentation level.

  • But sometimes a block can start in a middle of a line. In this case, this position defines the expected vertical indentation line.

There are some exceptions to this rule, for instance with operators. πŸ”— Fβ™― syntax: indentation and verbosity, by Scott Wlaschin | Fβ™― for fun and profit.

☝️ Indentation rules have been relaxed in Fβ™― 6.

Recommendations

  • In temporary fsx, speed to write code is usually more important than code readability. Fβ™― allows us some inconsistency and that's fine.

  • In fsproj, proper and consistent formatting is important for maintainability. β†’ Use consistently only spaces (no tabulations) β†’ Use the same number of spaces for all indentation level, 4 spaces being idiomatic. The number spaces can vary only to fit the vertical indentation line (see details below). β†’ Avoid naming-sensible indentation a.k.a Vanity Alignment (*) : β€’ They can break compilation after a renaming. β€’ Blocks too far at the right is less readable for a left-to-right language reader. β†’ Use a code formatter like Fantomas to ensure this consistency.

(*) Vanity alignment example:

Attributes

Attributes works like in C# to decorate a class, a method... with little character differences:

Lang
Separated attributes
Merged attributes

C#

[AttributeOne] [SecondAttribute]

[AttributeOne, SecondAttribute

Fβ™―

[<AttributeOne>] [<SecondAttribute>]

[<AttributeOne>; <SecondAttribute>]

Addendum

πŸ”— Fβ™― Cheatsheet

πŸ”— Troubleshooting Fβ™― - Why won't my code compile?

Last updated

Was this helpful?