On Tue, 2 Jan 2001, Derek M Jones wrote:
[snip, the constant == variable idiom for comparing a variable to a
constant in C, and what it implies: less prone to errors on the one hand,
more prone on the other]
> >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.
I know for a fact that I do not read from left to right all the time. When
reading ordinary text at high speeds my eyes make a sort of butterfly
movement across the page, reading whole paragraphs at once. With programs I
read top to bottom, reading whole statements in one go, as far as possible.
This makes a *lot* of sense in situations where you see something like
var = function(otherfunction(someothervar));
in which case you need to parse from inside out, and mostly from right to left.
> >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).
Actually I can't even see a functional programmer read that. To me the
statement "if blue is a colour" implies type-inspection of blue. The
statement if (blue == colour) only implies a comparison on the values of
blue and colour, without type inspection.
> >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.
What about PostScript? The postfix notation can be interpreted left to
right, but it's sometimes quite a lot easier to read it right to left.
Especially so for ifs...
> Computer languages go top to bottom (like Japanese). There is certainly a
> strong left to right influence.
Well said. Actually, my code in C is top to bottom within functions, but
with functions ordered (mostly) bottom to top. There's aspects of
boustrophedon as well, in some programming languages. See perl's
if (something_went_wrong) { dosomethingtofixit(); };
versus
dosomethingtofixthings() if (something_went_wrong);
for example.
> Now some technical rationale
>
> Why do developers write "if (x = 1)" instead of "x=1; if (x)" you ask?
And rightly so.
> 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.
Ah, but there's another reason. See below.
> 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.
These statements are very true. However, programming languages do not only
come with grammar, they also come with idiom. That idiom is not only made up
of the libraries available, but also of standard styles for doing things;
compare for (p=listpointer;p!=NULL;p=p->tl) { do_something_on_element(p); }
with p=listpointer; while (p!=NULL) { do_something_on_element(p); p=p->tl;}
An experienced C programmer will prefer the former, due to the fact that
he's used to it, and all loop control is in a single place. However, people
that are used to Pascal will usually prefer the latter.
In the case of assignments in if statements there's a powerful idiom,
and that is if((file=fopen(file))==NULL) { handle_open_error(); }
This makes for the least visual distraction from the main flow of the
code, and is therefore very accepted practice.
Doei, Arthur.
--
/\ / | [EMAIL PROTECTED] | Work like you don't need the money
/__\ / | A friend is someone with whom | Love like you have never been hurt
/ \/__ | you can dare to be yourself | Dance like there's nobody watching
- 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]