Re: The Philosophy of Programming?

From: Ian Woods (newspub2_at_wuggyNOCAPS.org)
Date: 02/15/04


Date: Sun, 15 Feb 2004 03:35:28 +0000 (UTC)

spinoza1111@yahoo.com (Edward G. Nilges) wrote in
news:f5dda427.0402141731.477e41f9@posting.google.com:

<snip>

> In my strlen example, deriving the strlen for a string known to be
> bounded by the input console length in a function in the do condition
> lowers the psychological complexity.

Assigning the value of the strlen to a temporary variable reduces the
complexity of analysing the source code. It's trivial, even in non-
trivial code, to determine that such a temporary is not altered...
especially trivial if it's const! On the other hand, it is considerably
less trivial to ensure that the string length has not been altered.
Consider:

/* do something with a string s*/ {
  const size_t len=strlen(s);
  size_t i;
  
  for (i=0;i<len;i++) {
    DoSomeOperation(s);
  }
}

compared with...

/* do something with a string s */ {
  size_t i;

  for (i=0;i<strlen(s);i++) {
    DoSomeOperation(s);
  }
}

Even with less trivial code it's still a no-brainer to check how len is
used compared to having to dig out the code and documentation for each
and every function s is passed to in the loop.

> More generally, using expressions in place of temporary variables
> lowers psychological complexity for most intelligent readers.

For most intelligent programmers, the laziness principal applies. Why
make considerably more work later when a simple temporary takes much less
time to use?

As in the example above, the use of the temporary is a bloody big clue,
with really bold friendly letters etched out in neon saying "The length
of the string should not change during this operation. If it does,
consider it a big big bug. Thank you.".

> I agree that many programmers prefer to see temprary variables and I
> propose that this is because their use of the temporary variable
> paradigm, communicated to them by false authority, damaged them in
> Dijkstra's sense, where he claims that early exposure to Basic damages
> programming ability.
>
> To me, the length of a string is something I should just have if I
> have a string. However, in May 2002 when I downloaded Jacques Navia's
> free compiler and coded in C after an interval of ten years, I had a
> massive brain fart.

If you want strings where getting the length is O(1), there are plenty of
freely available libraries which implement such a sting model, or,
implement whatever strategy you like best yourself. I used google and
found 3 or 4 on the first page alone.

<snip>

> You can build initial versions of code in a readable fashion, test
> them, and then improve performance at the key places that need it.

Indeed you can, but you can also use temporary variables to show intent
very clearly and not only as a performance improvement. Needlessly, and
uselessly wasting cycles though is just asking to come back and make it
faster later, and so the laziness principal applies. Since it takes, ooh,
maybe 5 whole seconds of effort to use a temporary, that using the
temporary shows a clear loop invarient and hence reducing the effort to
understand the code in the loop... it sounds like a great thing to do to
me. More efficient, and more readable!
 
<snip>

> I conclude that real programmers love to waste the computer's time
> because they know that the wear and tear is a metaphor.

The time doesn't, and never has, belonged to the computer. The time spent
in wasted cycles is time lost by the users. I totalled up the amount of
time I spend waiting for my computer to get on with it over a few hours.
I found that I'd spent a little under 5% of my time waiting...

I for one would much prefer the programmers of the software I use to use
the 5 seconds of time (if that) it takes them to make their loops O(n)
instead of O(n*n). Perhaps I'd get back some of the time I waited for
windows to switch, for text to be searched for, for programs to open and
all the various other times I'm waiting during the day.
 
Ian Woods

-- 
"I'm a paranoid schizophrenic sado-masochist.
My other half's out to get me and I can't wait."
Richard Heathfield


Relevant Pages

  • Re: A C++ Whishlist
    ... > people from inclusion in a standard. ... > creating their own string class. ... >>don't want an ever increasing size of exception specification on each ...
    (comp.lang.cpp)
  • Re: Hash functions (was: Maximum String size in Java?)
    ... snip ... ... Inserted with sx31hsh and ssfh_ph in 0.385 secs ... your string library has problems with them speaks for itself. ... published your SFH code with the shifted and undersized ints. ...
    (comp.programming)
  • Re: Zero terminated strings
    ... including when you do things like string concatenation etc ... Simple "tricks" are all you need to implement a decent link level ... <snip basic link layer protocol> ...
    (comp.lang.c)
  • Performance with reading large numbers of files...
    ... I have a small test application that recurses a directory and adds all the file names to a string collection. ... RecurseDirs, sDiskFiles); ... private static void RecurseDirs ...
    (microsoft.public.dotnet.framework.performance)