On Mon, Oct 19, 2009 at 7:30 PM, Steven D'Aprano <steve@pearwood.info> wrote:
On Tue, 20 Oct 2009 09:33:48 am Michael Foord wrote:
Having used languages that allow multiline anonymous functions (C#, Javascript) I have to say that they *can* improve the readability of code.
Could you provide some examples? I've seen plenty of usage in Javascript, but none that I would personally judge as improving readability -- and many that hurt badly. Even if this turns out to be a personal preference thing (some people think it is better, others just don't happen to agree), the advantage is likely small enough that it is outweighed by the advantages of standardization. (I've seen many people claim that the advantages are huge, but the people making such claims often turn out not to be familiar with nested functions.)
Given the difficulty in testing such anonymous functions, what do they do to the correctness of programs?
Anonymity of functions is generally not the biggest source of problems in Javascript. With C compilers, there is pressure to conform to the standard; with Javascript interpreters there is pressure to support existing broken content without any manual intervention (so no compiler switches). The major goal of HTML5 is to at least document and standardize the idiotic workarounds that are required to do that when parsing a web page labeled as HTML; there are similar, less advanced, plans for Javascript and the Browser Object Model, but ... I suspect "untested environment" will continue to be a much bigger issue for at least half a decade.
Being forced to define functions ahead of where they belong in the logical flow of the program is not a feature of Python but a restriction.
Agreed. But I have never felt the urge to read (as opposed to write) a full definition in the middle of call. (And if you aren't in the middle of a call, then the previous line isn't really that far ahead.) I have wished to just pass a name, and define the function later; the fact that I can't always do that is a quirk of python's execution model, but adding anonymous functions wouldn't help. And any syntax heavier than a single keyword would probably add so much noise that it would be worse than the out-of-order effect. In other words, I could understand the value of f(a, 5, delay g, "asdf") def g(x): return x But the cost is already high enough to make me unsure that it would be a worthwhile feature.
My point is that when it comes to data, we have anonymous data that is easy and straightforward to define, and yet we *still* prefer to give data names and define it ahead of where we use it. So why is code different? Why does defining code ahead of where you use it such a bad thing that the lambda restriction generates so much attention?
Data is static -- it just sits there. A name makes it shrink (in mental cost) but has no other effect -- it is like putting physical objects in a suitcase and zipping the thing shut. A function is part of the instructions; defining it somewhere else is like setting a bookmark to follow a footnote -- and then having to come back. That said, I suspect that much of the actual attention is from misunderstandings. -jJ