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 de reason about
Cons:
need more coding discipline
Example: we try to use the fn
before its declaration:
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 mis-interpreted, 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 the way 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.
Recommendations
In temporary
fsx
, speed to write code is usually more important than code readability. F⯠allows us some inconsistency and that's fine.
(*) Vanity alignment example:
Attributes
Attributes works like in C# to decorate a class, a method... with little character differences:
C#
[AttributeOne]
[SecondAttribute]
[AttributeOne, SecondAttribute
FāÆ
[<AttributeOne>]
[<SecondAttribute>]
[<AttributeOne>; <SecondAttribute>]
Addendum
Last updated
Was this helpful?