Express What, not How.

Ken Shan ken at digitas.harvard.edu
Tue Oct 14 22:58:07 EDT 2003


Raffael Cavallaro <raffaelcavallaro at junk.mail.me.not.mac.com> wrote in article <raffaelcavallaro-7597B0.20420614102003 at netnews.attbi.com> in comp.lang.functional:
> In article <pan.2003.10.14.23.19.11.733879 at knm.org.pl>,
>  Marcin 'Qrczak' Kowalczyk <qrczak at knm.org.pl> wrote:
> > Why do you insist on naming *functions*? You could equally well say that
> > every list should be named, so you would see its purpose rather than its
> > contents.
> I think this concept is called variable bindings ;^)

Yes, but what Marcin is talking about outlawing anonymous variables, so
for example you can't write

    amount_due = sum(item_prices) * (1 + tax_rate)

in his hypothetical programming language down the slippery slope (or is
it up the slippery slope?), but must write

    subtotal = sum(item_prices)
    tax_multiplier = 1 + tax_rate
    amount_due = subtotal * tax_multiplier

just as programmers did before FORTRAN.

I would go one step further and point out that even in the code above,
there are still unnamed things whose functionality is possibly unclear.
These things are source code locations to which computations return,
aka continuations.  For instance, what is the purpose of executing this
code above?  The purpose is not to go through a bunch of arithmetic
operations for fun, but to compute the amount due.

    def compute_amount_due:
	subtotal = sum(item_prices)
	tax_multiplier = 1 + tax_rate
	amount_due = subtotal * tax_multiplier

There, clearer already.  But what is the purpose of having the subtotal?
It is to add the tax to get the amount due.

    def compute_amount_due:
	sum(item_prices, add_tax_for_amount_due)

    def add_tax_for_amount_due(subtotal):
	tax_multiplier = 1 + tax_rate
	amount_due = subtotal * tax_multiplier

What is the purpose of computing the tax_multiplier?  To multiply with
the subtotal in order to get the amount due.  (I assume a Python-ish
syntax with partial function application (currying) here.)

    def add_tax_for_amount_due(subtotal):
	add(1, tax_rate, subtotal_and_tax_multiplier_to_amount_due(subtotal))

    def subtotal_and_tax_multiplier_to_amount_due(subtotal, tax_multiplier):
	amount_due = subtotal * tax_multiplier

But what's the purpose of having the amount due at all?  Maybe it is to
print a bill:

    def subtotal_and_tax_multiplier_to_amount_due(subtotal, tax_multiplier):
	multiply(subtotal, tax_multiplier, print_bill_with_amount_due)

    def print_bill_with_amount_due(amount_due):
	...

> > Perhaps every number should be named, so you can see what it
> > represents rather than its value.
> Actually, they are _already_ named. The numerals we use _are_ names, not 
> numbers themselves. I'm surprised you aren't advocating the use of 
> Church Numerals for all numerical calculation.

Functions are _already_ named as well.  The lambda expressions we use
_are_ names, not functions themselves.

> > You could say that each statement of
> > a compound statement should be moved to a separate function, so you can
> > see what it does by its name, not how it does it by its contents. It's
> > all equally absurd.
> 
> In the Smalltalk community the rule of thumb is that if a method body 
> gets to be more than a few lines, you've failed to break it down into 
> smaller abstractions (i.e., methods).

So perhaps we should have the programming language outlaw anonymous
functions that are more than 4 lines long...

-- 
Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
"The first rule of politics: The ballots don't make the vote. The count
makes the vote." -- Boss Tweed in Gangs of New York




More information about the Python-list mailing list