I often see Erlang functions return ok, or {ok, <some value>}, or {error, <some problem>}.
Suppose my function returns an integer N. Should my function return just N, or {ok, N}?
Or suppose my function includes the call io:function("Yada yada"). Should it return ok or nothing at all?
Or suppose I’m making a record or fun. Should I return {ok, R} or (ok, F}?
Thanks,
LRP
It’s a matter of style, but making reasonable choices goes a long way toward making your code more readable and maintainable. Here are some thoughts based on my own preferences and what I see in the standard library:
{ok, _}orok. Good examples are:orddict:find/2andapplication:start/1. We do this so both cases can be easily pattern matched when making the call.foo(bar(N))may not work ifbar/1returns{ok, _}. Good examples are:lists:nth/2andmath:cos/1.okortrueas opposed to whatever the last returned value in the function happened to be. Good examples are:ets:delete/1andio:format/1.