Einstein summation notation (was: question of style)

Paul Rubin http
Fri Jul 17 12:15:17 EDT 2009


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> Since I haven't specified an implementation for assemble_page, it could 
> be doing *anything*. Perhaps it has to talk to a remote database over a 
> slow link, perhaps it generates 300 lines of really inefficient HTML code 
> with no content, perhaps it sends a print job to a printer which then 
> warms up, cleans the print heads, and ejects a blank piece of paper.

That sounds like a code smell to me.  If the function is supposed to
print a piece of paper when any of those strings are nonempty, it
should also print one when they are all empty.  Of the different
values a string can have, 'foo', 'bar', and '' are all perfectly good
values and should all be treated the same way.  

If you want an absent string to work differently than a present one,
that's separate from empty vs nonempty, and overloading the empty
string to mean "absent" is another antipattern.  Maybe what you really
want is a list of page constituents that are present:

   page_parts = [header, body, footer]

If all the constituents are absent, then page_parts is the empty list,
so you would just test for page_parts being empty.

> Why does it matter? The example is about the `if` test, not about the 
> function assemble_page().

Because of the notion that the code should "do nothing" gracefully.
The presence of that 'if' test is ungraceful, so its better to look
for ways to eliminate it than slickify it.



More information about the Python-list mailing list