[BLACKBOX] Seasons Greetings from Linuxland

From: [at]} <Les>
Date: Wed, 29 Dec 2010 10:39:30 +0000

Les May wrote



// z = x++ + ++y;

 ++y;

 z = x + y;

 x++;



But which forms best express what the program writer intended?

------------------------------------------------------------------------------------------------



Chris Burrows wrote



The original point made by Wirth was related to the problem of parsing:



  x+++++y



It was your interpretation that it must have been intended to be:



  z = x++ + ++y;

-----------------------------------------------------------------------------------------



I think that this rather reinforces the point I was trying to make. Wirth suggested that x+++++y is a riddle. More complex expressions make it difficult to understand what the program writer intended that the code should do. Parsing problems are not confined to mechanical parsers.



Even if we ignore the vagaries of the C compiler then neither the intention of the programmer nor the outcome of the expression



digdata[digNr] = (bitdata[bitNr++] * 8) + (bitdata[bitNr++] * 4) +

(bitdata[bitNr++] * 2) + (bitdata[bitNr++]);



is immediately obvious.



It could be made obvious by expanding it to



bitNr++;

digdata[digNr] = (bitdata[bitNr] * 8) + (bitdata[bitNr] * 4) +

(bitdata[bitNr] * 2) + (bitdata[bitNr]);



or to



digdata[digNr] = (bitdata[bitNr] * 8) + (bitdata[bitNr] * 4) +

(bitdata[bitNr] * 2) + (bitdata[bitNr]);

bitNr++;



depending on what the writer intended.



It also follows the convention that text is to be read from left to right and from top to bottom. This means that there are fewer intermediate results (e.g. bitNr++)

to be stored on the reader's 'mental stack' which I believe aids understanding. The line



RETURN (ch <= '9') & (ch >= '0');



does not because once the comparisons and boolean have been mentally evaluated what to do with the result is at the beginning of the line.



I don't think this is a problem confined to C. I believe it is a applicable to all languages. Programs give the wrong 'answer' when there is a difference between what the programmer intended the code the to do and what it actually does. This suggests that it is a good idea to make your intentions explicit when writing programs especially if you might have to maintain them.



Les May























---- To unsubscribe, send a message with body "SIGNOFF BLACKBOX" to LISTSERV{([at]})nowhere.xy
Received on Wed Dec 29 2010 - 11:39:30 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:30:20 UTC