Comments and Style
Prerequisites: Functions
Comments
// A single-line comment
let taxRate = 0.2 // after code
(*
A block comment
spanning several lines
*)
/// Returns the price including tax.
/// <param name="price">The net price.</param>
let withTax price = price * (1.0 + taxRate)
printfn "%.2f" (withTax 100.0)
120.00
Comments starting with /// are XML documentation comments: editors
show them when you hover over the function, and they're included in
generated library documentation. A plain /// line with no tags works
too.
Style guidelines
The official F# style guide (on learn.microsoft.com) and the formatter Fantomas agree on these conventions:
- Four-space indentation, spaces only.
camelCasefor values, functions, and parameters;PascalCasefor types, modules, union cases, record fields, and namespaces.- Put each pipeline step on its own line, starting with
|>. - Prefer immutable values and pure functions; keep
mutablelocal. - Keep functions small and name them after what they return
(
totalPrice), or what they do if they have side effects (saveOrder). - Handle every case in a
match— never ignore the incomplete-match warning. - Use
optioninstead ofnull, anddecimalfor money. - Put the "data" parameter last so functions work with
|>.
Tools
- Fantomas — formats code automatically (
dotnet tool install fantomas, thendotnet fantomas .). - Ionide / Rider — inline type hints, errors, and refactorings.
- FSharpLint and F# analyzers — catch style issues and common mistakes.
What's next
You now know the core of F#. Try the Beginner Projects, then continue to Level 2 — Intermediate, which starts with records.