Bjorn,

>   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).

Indeed they do and indeed it is.  This is probably the most commonly seen
issue that crops up in coding standards (see below for a more detailed 
discussion).
But to your questions...

>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

Again this is a recommendation that crops up in coding guidelines.  Another 
one is
to enclose the expression in redundant brackets:

if ((variable_1 = variable_2))

The redundant brackets being used to signify, yes I really did mean to use an
assignment here.  A number of automated guideline enforcement tools are aware
of these 'rules'.

>The discussion was about whether this trick should be part of a coding 
>standard.

This particular case is a subproblem of a subproblem.  See below.

>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.

I am not sure I read from left to right all the time.  In an expression I 
always try
and find the top level operator (think of the expression represented as a 
parse tree).
Then I look at its operands.  But of course I do scan for that operator 
left to right.

The operand sequence: Variable  Constant
occurs a lot because of assignment (the most common operator).  So this 
sequence is
a well rehearsed one.

I would suspect that the reason that the sequence: Constant Variable
looks so unusual is because it is.  I practice this sequence is usually 
only ever seen
in the arguments to a function call.

>  For example, if our variable is called 'colour' and our constant
>'blue', then
>
>   if (colour == blue)
>
>is read as "if the colour is blue",

or "if blue is the colour"

>but the following
>
>   if (blue == colour)
>
>can be read as "if blue is a colour"

I don't see a C programmer thinking this.  A Pascal one perhaps (or those 
odd functional
programming people).

>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.

I would suspect that there is a strong learnt effect here.  One sequence 
occurs so much
more often than the other.

>Thirdly, it could be interesting to investigate whether left-to-right
>programming languages (probably the vast majority of existing programming
>languages)

APL is the only one, that springs to mind, that goes right to left.

Computer languages go top to bottom (like Japanese). There is certainly a 
strong
left to right influence.

>  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.

I have never met an Arabic programmer.  I know some Japanese ones and their
style does look unusual.

Now some technical rationale

Why do developers write "if (x = 1)" instead of "x=1; if (x)" you ask?

It's that old devil efficiency.  In the good old days compilers had limited
resources and few of them did any decent optimizations.  So extra code
was generated in the second case (usually an instruction to reload x, when it
was probably already in a register; a whole extra instruction!!!).  The 
refrain these
days (and for the last 10'ish years) is that a decent optimizing compiler 
will spot
that a does not need to be reloaded in the second case and that developers do
not need to worry about such issues.

The key phrase is 'decent optimizing'.  Many are not (the money gets spent on
advertising that they are, rather than making them so).  Also, for some 
chips, the
customer base is not large enough to warrant spending lots of money of creating
such a beast.

In practice, optimizing compiler or not, that single instruction rarely has any
measurable effect on the performance of a program (there are some special cases
where programs have to run in very constrained environments).

My biggest concern when I see the "if (x=1)" usage is that it indicates the 
developer
has too much interest in low level details and is looking at narrow 
optimization issues.

The general guideline is don't use an assignment operator in a conditional
expression.

It usually does lead to erroneous readings and it is often a pointless 
optimization.

derek

--
Derek M Jones                                                  tel: +44 (0) 
1252 520 667
Knowledge Software 
Ltd                                     mailto:[EMAIL PROTECTED]
Applications Standards Conformance Testing       http://www.knosof.co.uk



- 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]

Reply via email to