Sujet : Re: else ladders practice
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 09. Nov 2024, 07:00:07
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vgmtp8$3kdnf$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 03.11.2024 21:00, Bart wrote:
This was the first part of your example:
const char * flag_to_text_A(bool b) {
if (b == true) {
return "It's true!";
} else if (b == false) {
return "It's false!";
/I/ would question why you'd want to make the second branch conditional
in the first place.
You might want to read about Dijkstra's Guards; it might provide
some answers, rationales, and insights for this question. (Don't
get repelled or confused by the "calculate all conditions" aspect
or the non-determinism; think more about, e.g., the safety of full
specification, automated optimization runs, and other [positive]
implications.)
(Though if you're only focused on programmer-optimized structures
Dijkstra's concept and ideas probably won't help you.)
Incidentally, Dijkstra's Guards cover also an aspect of the OP's
original question.
Janis
Write an 'else' there, and the issue doesn't arise.
Because I can't see the point of deliberately writing code that usually
takes two paths, when either:
(1) you know that one will never be taken, or
(2) you're not sure, but don't make any provision in case it is
Fix that first rather relying on compiler writers to take care of your
badly written code.
[...]