If you want to get good at clarifying conditionals, take an electronics class and revel in the Karnaugh maps.
Clearer Conditionals using De Morgan's Laws
31–40 of 82 posts
Re: Clearer Conditionals using De Morgan's Laws
#32Old CS students' prank: improving the "no food and drink" sign unsurprisingly often found in labs by scribbling "no (food && drink) == (no food || no drink)" on it.
Re: Clearer Conditionals using De Morgan's Laws
#33Old CS students' prank: improving the "no food and drink" sign unsurprisingly often found in labs by scribbling "no (food && drink) == (no food || no drink)" on it.
Wouldn't that be (no food) and (drink) anyway?
Re: Clearer Conditionals using De Morgan's Laws
#34Earlier quoted context omitted.
Except where writing negative conditionals are clearer. For example turning: if(a) { if(b) { if (c) { // Do something. } } } Into: if(!a) { } elseif(!b) { } elseif(!c) { } else { // Do something. }
Or if (a && b && c) { // Do something. }
Re: Clearer Conditionals using De Morgan's Laws
#35The transition from !signed_out? to signed_in? is entirely non-logical, so it's hard to see what it's doing in this post, which is supposedly illustrating logical laws (two of the most elementary logical laws, at that, that I'm surprised it would ever have occurred to someone to think needed introduction to an audience of professional programmers).
"Not signed out" can be rephrased as "not not signed in" and thus simplified to "signed in". Same for "not untrusted ip" equaling "not not trusted ip" and, after DN, simply "trusted ip".
It's completely logical, so I'm not sure what your point is. Perhaps the article should have explained this better.
Re: Clearer Conditionals using De Morgan's Laws
#36Earlier quoted context omitted.
Or conversely that it is better to write affirmative conditionals rather than negative conditionals. I always find reading the affirmative ones much easier, as I have always found working in positive logic easier than work with negative logic circuits.
Except where writing negative conditionals are clearer. For example turning: if(a) { if(b) { if (c) { // Do something. } } } Into: if(!a) { } elseif(!b) { } elseif(!c) { } else { // Do something. }
A typical sequence is like the one to set the bus width.
err = sdio_select(rca);
if (! err) {
err = sdio_command(55, rca
I had originally done it the other way err = sdio_select(rca);
if (err) {
return err;
}
err = sdio_command(55, rca
I find the first form more readable. It also generates
fewer branches in the generated code. The sample code I first looked at was doing Goto's to the exit code (deselect/return error) which was unacceptable :-).In the original article the confusion arose around negative test cases and then testing for them negatively (double negatives) which I think are always bad from a readability point of view.
Re: Clearer Conditionals using De Morgan's Laws
#37The transition from !signed_out? to signed_in? is entirely non-logical, so it's hard to see what it's doing in this post, which is supposedly illustrating logical laws (two of the most elementary logical laws, at that, that I'm surprised it would ever have occurred to someone to think needed introduction to an audience of professional programmers).
Not not x is equivalent to x using the double negation rule (DN). "Not signed out" can be rephrased as "not not signed in" and thus simplified to "signed in". Same for "not untrusted ip" equaling "not not trusted ip" and, after DN, simply "trusted ip". It's completely logical, so I'm not sure what your point is. Perhaps the article should have explained this better.
If you had "outside" and went from !outside?" to "inside?", that would be erroneous (you could also be on the threshold).
ETA: this is especially obvious for trusted/untrusted; it doesn't have to be the case that every ip is either positively trusted or positively untrusted. If, in some application, it is binary in that way, then you can, in that case, go from not untrusted to trusted. But that isn't justified by purely logical considerations.
ETA again, in fact a better example is this, it's not a logical fact that if you flip a coin and it comes up not-heads, it has come up tails. (Even ignoring improbably things like its landing on its side.) That's a conclusion that is justified by knowledge of the substantive domain of coins.
Re: Clearer Conditionals using De Morgan's Laws
#38If you want to get good at clarifying conditionals, take an electronics class and revel in the Karnaugh maps.
Re: Clearer Conditionals using De Morgan's Laws
#39Earlier quoted context omitted.
Not not x is equivalent to x using the double negation rule (DN). "Not signed out" can be rephrased as "not not signed in" and thus simplified to "signed in". Same for "not untrusted ip" equaling "not not trusted ip" and, after DN, simply "trusted ip". It's completely logical, so I'm not sure what your point is. Perhaps the article should have explained this better.
The fact that "signed out" and "signed in" are opposites is not a logical fact; there's no general inference from "not p_out" to "p_in". If you had "outside" and went from !outside?" to "inside?", that would be erroneous (you could also be on the threshold). ETA: this is especially obvious for trusted/untrusted; it doesn't have to be the case that every ip is either positively trusted or positively untrusted. If, in…
Re: Clearer Conditionals using De Morgan's Laws
#40Earlier quoted context omitted.
Wouldn't that be (no food) and (drink) anyway?
Dunno, maybe spoken language does not have such strict operator priority. Or maybe it has one different than maths and programming languages.