Live data from Hacker News

Clearer Conditionals using De Morgan's Laws

robots.thoughtbot.com

31–40 of 82 posts

Re: Clearer Conditionals using De Morgan's Laws

#33
post #32
post #24

Old 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?

Dunno, maybe spoken language does not have such strict operator priority. Or maybe it has one different than maths and programming languages.

Re: Clearer Conditionals using De Morgan's Laws

#34
post #19

Earlier 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. }

Yes, the that was a very simple example.

Re: Clearer Conditionals using De Morgan's Laws

#35
post #12

The 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.

Re: Clearer Conditionals using De Morgan's Laws

#36
post #7

Earlier 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. }

Interestingly I've been going back and forth on this lately. I have been playing around with SD cards on the STM32F4 and a typical SD Card transaction consists of 3 to 10 commands which, if any one fails, the transaction fails. I'm currently using negative conditionals of the form "Not Error" (the Error test is an affirmative, and so that seems ok to me)

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

#37
post #12

The 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.

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 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

#39
post #37

Earlier 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…

The article title was clearer conditionals using DeMorgan's Law - to that end, the article illustrated how to move to that step for cleaner code through first applying the law.

Re: Clearer Conditionals using De Morgan's Laws

#40
post #33
post #32

Earlier 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.

What the sign clearly means is "no [members of the set] food and drink."
Post reply on HN