OCaml Style guide?

This might be a FAQ: is there a definitive style guide out there for OCaml?

(If one googles for it, the first hits are from some classes at Cornell and U. Penn, and I don’t think those are “definitive”.)

I quite like https://ocaml.org/learn/tutorials/guidelines.html

Just started reading it. It has a lot of good stuff, though it seems a bit silent on a few things I’d like answers on. (For example, what’s preferred, begin ... end vs ( ... ), and when?) But I’ll should try to read the whole thing before making such comments. :slight_smile:

I’ve found https://github.com/hammerlab/style-guides/blob/master/ocaml.md to be a useful supplement to @techate’s link above.

There is the ocamlformat ( https://github.com/ocaml-ppx/ocamlformat ) style; the advantage with this one is that it can plug in to your editor and auto-format your code for you, like gofmt, etc.

I think (but I haven’t tried it) Emacs’ tuareg-mode also autoformats your code while you type.

And finally of course, there is refmt which autoformats ReasonML code if you write in that syntax.

Hardly definitive, but we just posted our style guide here:

I think there is a typo here, a missing |:

match/with expressions

Put ‘ 	’ before the first pattern, e.g.:

image

Another one:

let position_change function

In general, this is a nice and sane guide. Will be a great benefit to incoming OCamlers. The Xen one is also fairly good https://wiki.xenproject.org/wiki/OCaml_Best_Practices_for_Developers

Under “Directory Names,” there’s a sentence that looks like it was meant as an editor’s note:

Is there guidance for organizing projects around lib/ and bin/ directories?

Thanks all for pointing out the issues. Should be fixed now.

y

Just reporting another typo I noticed:

The order argument to List.iter in the second code snippet should be orders (to match the first code snippet).