Sujet : Re: else ladders practice
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 02. Nov 2024, 14:11:47
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vg58ej$3q417$1@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 02/11/2024 09:37, fir wrote:
...
the fact is it is somewhat hard to say which is more obvious to readers
if(key=='A') Something();
else if(key=='B') Something();
else if(key=='C') Something();
else if(key=='D') Something();
or
if(key=='A') Something();
if(key=='B') Something();
if(key=='C') Something();
if(key=='D') Something();
imo the second is more for human but logically its a bit diferent
becouse else chain only goes forward on "false" and new statemant on
both "true and false"
If I saw code written the second way, I would ask myself why it wasn't
written the first way. The most obvious reason for not writing it the
first way is that the calls to Something() might indirectly change the
value of 'key', and that the later if() conditions are intentionally
dependent on those changes. I would waste time confirming that there's
no such possibility. You should write your code so that others reading
it won't have to worry about that possibility.