Acknowledgement: Most of this style guide is taken from CS 312 at Cornell University.
Mandatory Rules:
Suggestions:
let sum = List.fold_left (+) 0 (* Sums a list of integers. *) (* Sums a list of integers. *) let sum = List.fold_left (+) 0
(* This is one of those rare but long comments
* that need to span multiple lines because
* the code is unusually complex and requires
* extra explanation. *)
let complicatedFunction () = ...
let d = Unix.localtime (Unix.time ()) in
let m = d.Unix.tm_min in
let s = d.Unix.tm_min in
let f n = (n mod 3) = 0 in
List.filter f [m;s]
let foo x = x+1 let foo(x:int):int = x+1
module H = Hashtbl module L = List module A = Array ...
module L = List type foo = unit exception InternalError let first list = L.nth list 0
Token | Convention | Example | ||
Variables and functions | Symbolic or initial lower case. Use underscores for multiword names: | get_item | ||
Constructors | Initial upper case. Use embedded caps for multiword names. Historic exceptions are true, and false. Rarely are symbolic names like :: used. | Node EmptyQueue |
||
Types | All lower case. Use underscores for multiword names. | priority_queue | ||
Module Types | Initial upper case. Use embedded caps for multiword names. | PriorityQueue | ||
Modules | Same as module type convention. | PriorityQueue | ||
Functors | Same as module type convention. | PriorityQueue |
let x = "Long line..."^ "Another long line." let x = ("Long line..."^ "Another long line.")
let x = function1 (arg1) (arg2) (function2 (arg3)) (arg4) let x = function1 arg1 arg2 (function2 arg3) arg4
match expr with
pat1 -> ...
| pat2 -> ...
if exp1 then exp2 if exp1 then else if exp3 then exp4 exp2 else if exp5 then exp6 else exp3 else exp8 if exp1 then exp2 else exp3 if exp1 then exp2 else exp3
Bad | Good | |
let f arg1 arg2 = let x = fst arg1 in let y = snd arg1 in let z = fst arg2 in ... |
let f (x,y) (z,_) = ... |
|
let f arg1 = let x = arg1.foo in let y = arg1.bar in let baz = arg1.baz in ... |
let f {foo=x, bar=y, baz} = ... |
let fact 0 = 1 | fact n = n * fact(n-1) let fact n = if n=0 then 1 else n * fact(n-1)
Bad | Good | |
let v = someFunction() in let x = fst v in let y = snd v in x+y |
let x,y = someFunction() in x+y |
let d = Date.fromTimeLocal(Unix.time()) in match Date.month d with Date.Jan -> (match Date.day d with 1 -> print "Happy New Year" | _ -> ()) | Date.Jul -> (match Date.day d with 4 -> print "Happy Independence Day" | _ -> ()) | Date.Oct -> (match Date.day d with 10 -> print "Happy Metric Day" | _ -> ())Good
let d = Date.fromTimeLocal(Unix.time()) in match (Date.month d, Date.day d) of (Date.Jan, 1) -> print "Happy New Year" | (Date.Jul, 4) -> print "Happy Independence Day" | (Date.Oct, 10) -> print "Happy Metric Day" | _ -> ()
fun euclid (m:int,n:int) : (int * int * int) = if n=0 then (b 1, b 0, m) else (#2 (euclid (n, m mod n)), u - (m div n) * (euclid (n, m mod n)), #3 (euclid (n, m mod n)))Good
fun euclid (m:int,n:int) : (int * int * int) = if n=0 then (b 1, b 0, m) else let val q = m div n val r = n mod n val (u,v,g) = euclid (n,r) in (v, u-(q*v), g) end
let x = TextIO.inputLine TextIO.stdIn in match x with ...Good
match TextIO.inputLine TextIO.stdIn with
...
let x = y*y in x+z y*y + z
Bad | Good | |
if e then true else false | e | |
if e then false else true | not e | |
if beta then beta else false | beta | |
if not e then x else y | if e then y else x | |
if x then true else y | x || y | |
if x then y else false | x && y | |
if x then false else y | not x && y | |
if x then y else true | not x || y |
match e with true -> x | false -> y if e then x else y
match e with c -> x (* c is a constant value *) | _ -> y if e=c then x else y
letl x = match expr with (y,z) -> y let x,_ = expr
Bad | Good | |
l::[] | [l] | |
length + 0 | length | |
length * 1 | length | |
big exp * same big exp | let x = big exp in x*x | |
if
x then f a b c1 else f a b c2 |
f a b (if x then c1 else c2) | |
String.compare x y = 0 | x=y | |
String.compare x y < 0 | x<y | |
String.compare x y > 0 | x>y |
List.map (fun x -> sqrt x) [1.0; 4.0; 9.0; 16.0]
List.map sqrt [1.0; 4.0; 9.0; 16.0]
fold_left (fun x y -> x + y) 0
fold_left (+) 0