Re: How to name variables in a program?



Phlip wrote:
August Karlstrom wrote:


Ok, then I see your point (the dot or the space under the dot has a
tendency to "disappear", right?) It's unfortunate then that `i' is
probably one of the most commonly used names for local variables. Make
sure you use the best possible font. I like Proggy Clean, though it
might be a tad too small.


Oookay. Now suppose for a moment that I weren't mildly visually impaired. (I
honestly am, but I really wish that were the _worst_ of my impairments!)

Shouldn't the same logic apply? To amp up the font, _and_ to use variable
names with high visual impact, like k, j, q, etc?

Yes, when it comes to legibility I'm with you. In mathematics I think `k' (rather than e.g. `i') is often used as subscript for that very reason. I think I will use j and k more often from now on.


(Also, one should use techniques like Ruby's .each() method that encapsulate
dumb little variables away from their controlled code...)

Note that single letter variable names are generally preferred for (trivial) loop variables as long names obscures the code. Compare


   FOR k := 0 TO LEN(a) DO
      a[k] := 0
   END

with

   FOR arrayIndex := 0 TO LEN(a) DO
      a[arrayIndex] := 0
   END

It's even more evident in the C family of languages:

   for (k = 0; k < n; k++)
      a[k] = 0;

versus

   for (arrayIndex = 0; arrayIndex < n; arrayIndex++)
      a[arrayIndex] = 0;


-- August .



Relevant Pages