Skip to content

Basic programming tips

Warnings

Warnings are always bad, unless you expect them (e.g. because code is only partially written). Never ignore warnings. Your finished code should never generate warnings.

Use let instead of match when there is only one variant

It's not uncommon to have types that only have one variant. We see this in the program types of our compilers, for instance:

type program = Program of exp

If you have a program value and you want to extract the exp component, you can use a pattern match as usual:

match prog with
  | Program e -> e  (* the only case *)

However, this is overkill for such a simple case. The idiomatic way to do this is with a let:

let (Program e) = prog in ...

You can think of a let used in this way as a one-variant pattern match. Never use a match when a let suffices; a match in this case is excessive and harder to read.