Here is a collection of best practices for successful programming in OCaml. You should be using ocp-indent (see below) for your code formatting.
Coding Style Tooling
Rather than spending precious time worrying about the correct format for syntax, it’s much more convenient to have
tooling that will automatically lay out the code for you in a canonical manner. While ocp-indent used to be the clear
choice for automatic coding style, ocamlformat is a recent arrival that, inspired by ReasonML’s
refmt tool, uses
a more comprehensive approach of end-to-end parsing and printing. Both tools can be integrated into editors to automatically lay out code as you write it.
- ocp-indent is a coding style formatting tool that relies on heuristics and partial parsing rather than a full end-to-end parsing and printing approach, like ocamlformat below. The advantage of ocp-indent’s approach is that even partially-compiling files can be indented, as can code fragments.
- ocamlformat is a comprehensive coding style formatting tool that parses the code
and prints it out again. This follows the example of the
refmttool for Reason. While new,
ocamlformatmay eventually overtake
Coding Style Guidelines
- OCaml programming guidelines at ocaml.org
- XEN – OCaml Best Practices for Developers
- Jane Street Style
- Ocaml Towards Clarity and Grace