Live data from Hacker News

Clearer Conditionals using De Morgan's Laws

robots.thoughtbot.com

21–30 of 82 posts

Re: Clearer Conditionals using De Morgan's Laws

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

Unless they actually are a VB programmer and And doesn't short circuit.

Re: Clearer Conditionals using De Morgan's Laws

#22

Sorry, but I just don't see what was unclear about the original conditional. Anyone with a basic grasp of logic could parse it instantly. As for the refactoring, that might be a good choice (I myself prefer positive boolean methods) but it's not a logic lesson.

I see the double negative in code the same as it is in English (and probably any verbal language). Even though I can understand the first version of this code, for me second version can be understood much faster. The lesson I gathered is one that's already taught in verbal languages: double negatives should not be used (or used sparingly).

Re: Clearer Conditionals using De Morgan's Laws

#23
post #13

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

No need for a full blown electronics class. This subject falls under Switching Theory and can be covered thoroughly in the first (and often a single) semester. Starting with elementary set theory and boolean algebra, you go into the combinatorial logic (that covers De Morgan and Karnaugh), and finish with finite state machines (automata).

There are very few requisites too. Some high schools and trade schools teach the material to 16-18 year-olds in a year or two. I suspect this is partially why it's so often omitted in CS curriculum. It's too easy.

Re: Clearer Conditionals using De Morgan's Laws

#25

Sorry, but I just don't see what was unclear about the original conditional. Anyone with a basic grasp of logic could parse it instantly. As for the refactoring, that might be a good choice (I myself prefer positive boolean methods) but it's not a logic lesson.

I don't not agree, it was perfectly not unclear at all.

Re: Clearer Conditionals using De Morgan's Laws

#27
I guess it depends on whether or not you want your code to read like natural language.

The refactored version reads like:

Allow access to the site if the user is signed in or has a trusted IP.

The original (DeMorgan's applied):

Allow access to the site if the user isn't signed out or doesn't have an untrusted IP.

It does help to have a good understanding of propositional logic and Boolean algebra, though.

Re: Clearer Conditionals using De Morgan's Laws

#30
post #2

Good naming conventions are pretty key here. My guess is the original writer of that code had used those in something else entirely, then reused those methods in a new method so he wouldn't have to rewrite. I always feel like it's better to positively name Boolean values, personally, but I know everyone is different.

What he hand-waves is that he's got something that looks like this:

  def signed_out?
    # Code
  end
  
  def signed_in?
    !signed_out?
  end
Which can easily start to become its own problem. On the other hand, with Ruby, it might be worthwhile to define something like Class#invert such that you have this:

  def signed_out?
    # Code
  end
  method_invert :signed_out?, :signed_in?
Dunno. I haven't ever found myself in a position where it mattered.
Post reply on HN