Sujet : Re: else ladders practice
De : fir (at) *nospam* grunge.pl (fir)
Groupes : comp.lang.cDate : 02. Nov 2024, 09:37:35
Autres entêtes
Organisation : i2pn2 (i2pn.org)
Message-ID : <57d42d094a6141f145d1366a85abda2a6be0ff6e@i2pn2.org>
References : 1 2 3 4
User-Agent : Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
David Brown wrote:
>
It is best to write the code in the way that makes most sense - whatever
gives the best clarity and makes the programmer's intentions obvious to
readers, and with the least risk of errors.
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"
(there is also an option to go only on true (something like "then" keyword
if(key=='A') Something();
then if(key=='B') Something();
then if(key=='C') Something();
then if(key=='D') Something();
though c dont has it (eventually can be done by nestong ifs)