On 11/06/15 01:38, Stefan Ram wrote:
> Richard Heathfield <
r...@cpax.org.uk> writes:
>>I would agree with you that the code should be as easy to read as is
>>reasonably possible. I don't agree that adding redundant parentheses
>>necessarily improves readability.
>
> We are living in dumbed-down times!
>
> Use »i++« in JavaScript, and »jslint« by Crockford will tell
> you that this is »excessive trickiness«.
That's a curious way to describe something as bland as ++. I'd have
thought the word "idiomatic" would be more appropriate.
> A student in my Java course told me that he was not given a
> job because in the interview he used »?:«, and the company
> believed that this was unreadable (probably in their opinion
> because nobody knows such »obscure operators«). (And they
> also expected him to know this style rule of the company by
> mind reading.)
A narrow escape.
> They think »if« is much more readable for beginners than »?:«.
> But whenever I show the following to beginners:
>
> char const * english( int const a )
> { char const * result;
> if( a == 0 )result = "zero";
> if( a == 1 )result = "one";
> if( a == 2 )result = "two";
> if( a == 3 )result = "three";
> else result = "four";
> return result; }
You telegraphed it, so it was easy to spot. Whether it would have been
quite so obvious if we hadn't been primed, I don't know.
<snip>
> . When someone can't even read »?:« or »x++«, how do you expect
> him to write any piece of non-trivial code, say code for OCR,
> a compiler, or code to navigate a car through the streets?
Or indeed to tie his own shoelaces.
> Or people writing
>
> while(( *p++ = *q++ ));
>
> where the double parentheses are intended to avoid a warning message,
> or even people saying that each inner statement in a while needs braces.
> So they now even write:
>
> while(( *p++ = *q++ )){ ; }
>
> because they believe that this will help someone who might insert
> another statement into the loop not to forget the braces!
That one's not quite as ridiculous as you are trying to make it sound.
In that particular case, you have a mild point, but in general there is
actually a danger that this:
while(foo(bar) > baz)
quux = quuux(quuuux);
could be edited into this:
while(foo(bar) > baz)
initquuuux(&quuuux);
quux = quuux(quuuux);
The probability that this mistake will occur does of course depend on
the programmer, but the consequences if it does happen could be quite
troublesome. It is not difficult to contrive circumstances in which the
probability is increased:
* an editor that doesn't do line-wrap
* a while condition that stretches right across the visible window
* a programmer who is accustomed to a layout style that puts opening
braces (and perhaps even closing braces) at the ends of lines
* a single-statement loop body that spans multiple lines
None of these conditions is particularly unlikely, and the combination
could lead to an occurrence of this bug:
while((currdate = convert_date_to_julian(fund->pro| <- screen edge
fund->projection->projected_balance += interest_|
is_triversary(fund->projection_date) * fund->tri|
is_charge_date(currdate) * fund->projection->ann|
Contrived? Of course. Possible? Certainly.
I habitually use braces even for single-statement loop bodies, to the
point where I don't even think about it (except when hacking up a
ten-liner, maybe, in which case I might stretch a point, but a quick
review of a bunch of programs in my scratch directory would suggest that
I don't stretch that point very far or very often).
I'm not actually all that worried about stopping bugs in this way,
though. I got into the habit because I used to catch myself muttering
whenever I was maintaining old code and had to add the braces, so I
started putting them in 'up front' instead, which cured the muttering
problem (for no logical reason that I can think of - after all, it takes
just as long to put the braces in during initial development as it would
during maintenance, so it hasn't actually saved me any time... unless,
of course, it has stopped me from suffering instances of the bug
mentioned above (and there's no way to tell that, one way or the other).