Live data from Hacker News

Clearer Conditionals using De Morgan's Laws

robots.thoughtbot.com

41–50 of 82 posts

Re: Clearer Conditionals using De Morgan's Laws

#41

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

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

Not to drag the thread off topic, but this seems like the kind of situation where a version of Haskell's Maybe or Either monads might come in handy, so you could write:

    do
      command1
      command2
      ...
and have the failures propagate automatically.

Re: Clearer Conditionals using De Morgan's Laws

#42
post #13

If you want to get good at clarifying conditionals, take an electronics class and revel in the Karnaugh maps.

I don't think this is necessary. I never wrote a conditional complicated enough to make me even think about getting down to K maps and I believe such a thing would be actually a smell and better be refactored as a whole. Knowing some boolean algebra is nice, though

Re: Clearer Conditionals using De Morgan's Laws

#43
post #40
post #33

Earlier quoted context omitted.

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

But what the sign says is either "no(food && drink)" or "no(food) && drink", depending on how you feel about precedence. The obvious implication in the first case is that either food or drink, or neither, but not both, is permissible; in the second, it's that drink is acceptable only when not also accompanied by food, but no other constraint is expressed with regard to either. If the intent was to express that neither food nor drink is permissible in any combination, the correct form, both logically and grammatically, would be "No food or drink".

Re: Clearer Conditionals using De Morgan's Laws

#45
post #39
post #37

Earlier quoted context omitted.

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.

Yes, and the other thing I find baffling is that a trivial post talking about something that would be covered in the first part of any baby logic course---before you even get to quantifiers!---is currently ranked as high as it is.

Re: Clearer Conditionals using De Morgan's Laws

#46
post #45
post #39

Earlier quoted context omitted.

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.

Yes, and the other thing I find baffling is that a trivial post talking about something that would be covered in the first part of any baby logic course---before you even get to quantifiers!---is currently ranked as high as it is.

Many coders out there haven't taken a course that teaches symbolic logic or such. It's a simple article that wasn't aimed at you is all.

Re: Clearer Conditionals using De Morgan's Laws

#47

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

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

What I've found myself doing in such cases is adding a function that processes multiple commands and indicates an error if any one of the commands failed (sdio_batch could be a varargs function):

  struct sdio_cmd {
      enum { SDIO_SELECT, SDIO_COMMAND } cmd;
      int reg; // Guesses at suitable names without
      int val; // knowing anything about SDIO
  }

  int sdio_batch(int count, ...);
  
  err = sdio_batch(3,
      &(struct sdio_cmd){ SDIO_SELECT, .val = rca },
      &(struct sdio_cmd){ SDIO_COMMAND, 55, rca 
I've been doing it in Ruby with network remote procedure calls, where it's not quite as ugly to use inline lists and variable argument counts as it is in C, and where branch count isn't quite as important.

The sample code I first looked at was doing Goto's to the exit code (deselect/return error) which was unacceptable :-).

What's so bad about a little goto between friends?

Re: Clearer Conditionals using De Morgan's Laws

#49
post #45
post #39

Earlier quoted context omitted.

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.

Yes, and the other thing I find baffling is that a trivial post talking about something that would be covered in the first part of any baby logic course---before you even get to quantifiers!---is currently ranked as high as it is.

Math is hard, let's discuss what new blah.js is there, or how that new company abandoned managers and uses github to order vegan lunches.

Re: Clearer Conditionals using De Morgan's Laws

#50
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…

[deleted]
Post reply on HN