From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 765A7BC6E for ; Thu, 29 Mar 2007 03:52:48 +0200 (CEST) Received: from pih-relay05.plus.net (pih-relay05.plus.net [212.159.14.132]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l2T1ql5t018080 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Thu, 29 Mar 2007 03:52:48 +0200 Received: from [80.229.56.224] (helo=beast.local) by pih-relay05.plus.net with esmtp (Exim) id 1HWjol-00027Q-DJ for caml-list@yquem.inria.fr; Thu, 29 Mar 2007 02:52:47 +0100 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Style and organization of code Date: Thu, 29 Mar 2007 01:49:33 +0100 User-Agent: KMail/1.9.5 References: <45F87661.4020504@softhome.net> In-Reply-To: <45F87661.4020504@softhome.net> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200703290149.33206.jon@ffconsultancy.com> X-Miltered: at discorde with ID 460B1BEF.002 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 chunks:01 recursion:01 ocaml:01 compiler:01 mli:01 sig:01 struct:01 compilation:01 inlined:01 stdlib:01 instinct:98 frog:98 wrote:01 caml-list:01 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