caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
To: ivan chollet <ivan.chollet@gmail.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Examples where let rec is undesirable
Date: Fri, 6 Jan 2012 11:34:08 +0100	[thread overview]
Message-ID: <CALgo1QL0VG-crmsPPUHnRuGL94XjvXDZgHPKRPPAAWKv-L=ZgA@mail.gmail.com> (raw)
In-Reply-To: <CACm_MF-hxbJEo=g4wNgN-YmHE+EeQzoUiDCMqEszOOgoOJfsdw@mail.gmail.com>

> Sorry Richard I should have elaborated a bit more.
> I guess there are a couple of examples in the literature, but one of them
> comes to my mind, consider the following code snippet:
>
> let fd = Unix.open "myfile1" ... in
> let fd = Unix.open "myfile2" ... in
> ... (some code)
> Unix.close fd
>
> This causes a file descriptor leak that is hard to detect statically in general.

If you mean detect statically by a _program_, then this makes
absolutely no difference. A static analyzer could alpha rename the
term so that each name is unique or work with De Bruijn indexes
without changing the complexity of the problem.

If you mean detect statically by a _programmer_ then yes, variable
shadowing can be a good tool to write obfuscated code.

> As a rule of thumb, I think it's better to give different conceptual objects
> different variable names, which also improves self-documentation.

Agreed. (I'm sure also got caught at least once by something like your
first example).

> Within nested scopes, all objects declared with a let-binding are usually distinct
> conceptually.

Not necessarily. I think what Łukasz mentions about incrementally
transformed values and cognitive burden is very true and in these
cases I wouldn't consider name recycling a bad style.

One example is working with a purely functional datastructure like
Set.t where you need to perform a few applications before getting to
the only value you are interested in. The alternative would be to
simply not name the intermediate results, it may be possible but
sometimes writing the sequence of application explicitly by a serie of
lets makes the presentation and the code clearer [1].

Best,

Daniel

[1]
Somewhat related is point-free style programming of which haskellers
are very fond of. A little bit of it is nice, but I think it sometimes
makes the cognitive burden too high. There's a balance between
conciseness and readability. Somehow programmers are obsessed by the
former at the detriment of the latter, but a good balance is needed.
This example (from this [2] wikipedia page) speaks for itself :

mf criteria operator list = filter criteria (map operator list)

vs

mf = (. map) . (.) . filter

I'm sure we all agree the former is easier to grasp.

[2]
http://en.wikipedia.org/wiki/Point-free_programming


  parent reply	other threads:[~2012-01-06 10:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-02 22:37 Diego Olivier Fernandez Pons
2012-01-02 22:49 ` Alexandre Pilkiewicz
2012-01-03  0:05 ` Lukasz Stafiniak
2012-01-03  5:47   ` Martin Jambon
2012-01-03  8:07     ` Gabriel Scherer
2012-01-05 20:04   ` Richard W.M. Jones
2012-01-05 20:27     ` ivan chollet
2012-01-05 20:46       ` Gabriel Scherer
2012-01-05 21:39         ` Richard W.M. Jones
2012-01-06  2:39           ` Cedric Cellier
2012-01-06 15:22         ` Damien Doligez
2012-01-05 21:36       ` Richard W.M. Jones
2012-01-05 23:16         ` ivan chollet
2012-01-06  8:34           ` David Allsopp
2012-01-06 10:34           ` Daniel Bünzli [this message]
2012-01-03 13:05 ` Yaron Minsky

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='CALgo1QL0VG-crmsPPUHnRuGL94XjvXDZgHPKRPPAAWKv-L=ZgA@mail.gmail.com' \
    --to=daniel.buenzli@erratique.ch \
    --cc=caml-list@inria.fr \
    --cc=ivan.chollet@gmail.com \
    /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).