In an ongoing discussion on the newsgroup comp.lang.c about a specific coding
standard an interesting point about how we read source code was raised. To
understand the point I must first describe how assignment and comparison works
in the C programming language (as well as C++ and Java).
In C a single equal-sign is used to assign a value, in this case a constant,
to a variable
variable = constant;
and two consecutive equal-signs are used to compare a variable to a constant
if (variable == constant)
However, the following statement is also legal in C. It first assigns the value
to the variable, and then uses the variable to determine the outcome of the if
statement.
if (variable = constant)
The above statement is equivalent to the following ('!=' means 'not equal to')
variable = constant;
if (variable != 0)
Clearly the "if (variable = value)" notation is error prone, and I would
postulate that many C programmers occasionally writes a single equal-sign
instead of two. This type of error can also be difficult to find (just like
finding your own spelling mistakes).
Fortunately, most modern compilers issue a warning when they meet this
construct,
but many C programmers still try to remedy this potential problem by reversing
the order in the statement
if (constant == variable)
In C the left hand side of an assignment must be a variable; it cannot be a
constant, so the following statement is illegal and the compiler will stop with
an error message
if (constant = variable)
The discussion was about whether this trick should be part of a coding standard.
The point that was being raised was that the reversal results in a conceptual
problem, because we tend to read the source code left-to-right, just like we
read English. For example, if our variable is called 'colour' and our constant
'blue', then
if (colour == blue)
is read as "if the colour is blue", but the following
if (blue == colour)
can be read as "if blue is a colour" (which would always be true, regardless
of the value of the colour variable), even though the compiler interprets it
as "if colour is blue".
I have three reasons to mention this issue here:
Firstly, it is an interesting example of how alleviating one dimension (error-
proneness) in Cognitive Dimensions can result in conceptual problems. I am
unsure what dimension the resulting problem should be characterized as, but I
am inclined to believe that it also belongs to the error-proneness dimension.
Secondly, should anybody have been inspired by the recent discussion here
about lack of psychological studies on coding standards and decide to conduct
experiments, then this problem would be an obvious candidate for inclusion.
Thirdly, it could be interesting to investigate whether left-to-right
programming languages (probably the vast majority of existing programming
languages) cause conceptual problems for programmers whose native languages
are right-to-left, e.g. Arabic, because they tend to read the source code in
a right-to-left manner.
- Automatic footer for [EMAIL PROTECTED] ----------------------------------
To unsubscribe from this list, mail [EMAIL PROTECTED] unsubscribe discuss
To join the announcements list, mail [EMAIL PROTECTED] subscribe announce
To receive a help file, mail [EMAIL PROTECTED] help
If you have any problems or questions, please mail [EMAIL PROTECTED]