Code formatting is something that - at least currently - in F# is somewhat of a subjective matter. There are certainly some patterns that we see most teams that we work with adopting, but in other cases there's a wide degree of variance. On the one hand there's nothing necessarily wrong with that, but on the other hand one can make the argument that it's something that shouldn't be an issue or need any thought expended on it. As such, code formatters have become more and more popular over the years to the extent that some languages have them built in as a code part of the language - Elm being one example of that.
F# doesn't have a built-in or pervasive code formatter, although this is starting to slowly change thanks to a few developments. Firstly, there's a set of official F# Style Guidelines that's been developed along with a set of issues around this. Having this in place is an important step towards bringing some level of consistency to the formatting. The second factor is the continued evolution and development of the Fantomas package, which is an F# code formatter tool.
Fantomas is not difficult to install from the NuGet package:
dotnet tool install -g fantomas --prerelease
This installs the latest pre-release version of fantomas v5 machine-wide. If you prefer to have repository-level configuration, use the following commands:
dotnet new tool-manifest # if you are setting up this repo dotnet tool install --local fantomas --prerelease
Fantomas v5 pre-release is what the Fantomas team recommend you to start with; v4 is the latest stable version. To use that, install the
fantomas-toolNuGet package instead of
There's also integrated tooling support for the main three IDEs (VS Code, Visual Studio and Rider). For example, in VS Code once you've installed the tool as above, your can use the standard Format Document command to execute Fantomas, or even turn on the "Format On Save" setting (which can be set at the repository level) so that every time you save your file, Fantomas will automatically reformat it. The screenshot below illustrates a single file that has been run against Fantomas in VS Code; the old version on the left and the reformatted version on the right.
Fantomas also includes support for working in a CI/CD setting by being able to fail if the code it checks does not conform to the standard - so you can enforce adoption of the tool without manually needing to check code, and you can retrospectively apply Fantomas formatting to an entire folder so it's a breeze to upgrade an entire project at once (or to try it out to see what things would look like).
Lastly, whilst Fantomas is opinionated it also contains a number of switches that you can use to tweak how it behaves if necessary, with the documentation on the GitHub repository providing full details on the available settings.
The documentation is currently in the process of being ported to a new website - watch this space!
F# has lacked a reliable, performant and flexible code formatter for many years. The continued development of Fantomas (which is also being run against the official F# compiler code base now!) has the potential to streamline and unify part of the F# development process that up until recently has been somewhat more inconsistent. I'd encourage you to run it on your projects today, even if just to try it out - you might be surprised by it!