caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Style and organization of code
Date: Thu, 29 Mar 2007 01:49:33 +0100	[thread overview]
Message-ID: <200703290149.33206.jon@ffconsultancy.com> (raw)
In-Reply-To: <45F87661.4020504@softhome.net>

On Wednesday 14 March 2007 22:25, ian wrote:
> I'm looking for a guidebook or just some rules of thumb on how to organize
> my OCaml code.

An excellent question.

> Say I have a function called "solveHardProblem".   solveHardProblem relies
> on several helper functions, which are not going to be useful to any other
> functions in the program.  So, my first instinct would be to define all the
> helpers using let blocks within the definition of solveHardProblem.
>
> But that would make the definition of solveHardProblem really long --
> several screens of text -- which I've been taught to avoid.  Is it wrong to
> use a module to hide those functions if the module signature will contain
> only that of solveHardProblem?

I would recommend splitting large functions into many smaller functions. This 
has several advantages:

1. Easier to test small, self-contained units, e.g. in a top-level.

2. Environment is explicit.

3. Often more efficient.

4. Easier to assimilate bite-sized chunks of code.

5. Easier to describe/document.

6. Explicit environment makes errors in recursion easier to avoid.

> And say you DO choose to use a module...  The OCaml documentation says that
> the compiler can automatically infer the signature without the need to
> create a .mli file for it.  Does anyone actually use that feature in
> practice, or is creating a sig hard-wired to the act of creating a struct?

I often use inferred .mlis, especially during development when I need more 
flexibility. Note that you can nest modules inside compilation units as well.

In this case, if your nested module solveHardProblem would only expose a 
single value then I'd leave that function and all of its helpers inlined into 
the outer module and restrict its signature to hide the helpers.

My style has evolved significantly since I started programming in OCaml and I 
would now recommend following the style of the stdlib.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


  parent reply	other threads:[~2007-03-29  1:52 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-14 22:25 ian
2007-03-15  3:21 ` [Caml-list] " Tom
2007-03-15  1:03   ` ian
2007-03-15  8:02     ` Gabriel Kerneis
2007-03-15  8:09       ` Maxence Guesdon
2007-03-15  8:20         ` Gabriel Kerneis
2007-03-15 10:11           ` ian
2007-03-15 20:04             ` Martin Jambon
2007-03-15  3:44 ` Chris King
2007-03-15  3:50   ` Chris King
2007-03-15  4:01 ` skaller
2007-03-15  9:03   ` micha
2007-03-15 13:38 ` dynamically finding libraries Vu Ngoc San
2007-03-15 17:10   ` [Caml-list] " Eric Cooper
2007-03-15 17:29     ` slightly OT: anyone reading this list with Mutt? Eric Cooper
2007-03-15 18:44       ` [Caml-list] " Stefano Zacchiroli
2007-03-15 19:00       ` caml-list
2007-03-15 19:44         ` ls-ocaml-developer-2006
2007-03-15 22:39 ` [Caml-list] Style and organization of code Richard Jones
2007-03-29  0:49 ` Jon Harrop [this message]
     [not found] <20070315224000.674E3BC82@yquem.inria.fr>
2007-03-15 23:08 ` David Allsopp
2007-03-15 23:48   ` Richard Jones
2007-03-16  3:51     ` Quôc Peyrot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200703290149.33206.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).