MAthematical formula terms make for terrible names in a program (was: What's good for mathematical formulas can be bad for program code (was: how to extract a variable as parameter which has index using by a for loop?))

Ben Finney ben+python at benfinney.id.au
Wed Jun 8 16:26:58 EDT 2016


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:

> On Wednesday 08 June 2016 17:31, meInvent bbird wrote:
>
> > H2 = [MM[mmm][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
>
> This is a mess. I don't understand what you are trying to do. You have
> these variable names that don't mean anything, like "b" and "H2", and
> others which aren't defined, like MM. I don't understand what you are
> trying to accomplish, or the purpose of your code.

The nice thing about mathematical formulas is that one has latitude, in
the accompanying academic paper, to define at length the
single-character names. This means the formulas are briefer to write
by hand on the blackboard, and the reader can refer to the author's
extensive explanation of what the character means.

Mathematicians (and other scientists), please don't attempt to cram
meaning into single-letter names in your program code. None of the above
advantages apply, and we get only the disadvantages Steven describes.

Instead, choose meaningful names that someone who knows that meaning has
at least a better-than-random chance of remembering.

-- 
 \       “The fundamental principle of science, the definition almost, |
  `\             is this: the sole test of the validity of any idea is |
_o__)                                 experiment.” —Richard P. Feynman |
Ben Finney




More information about the Python-list mailing list