Live data from Hacker News

Clearer Conditionals using De Morgan's Laws

robots.thoughtbot.com

11–20 of 82 posts

Re: Clearer Conditionals using De Morgan's Laws

#11
post #6

[deleted]

> In most programming languages, AND will short circuit. OR requires both operands to be evaluated.

Can you name any widely-used programming language(s) that has a short-circuit AND that returns on the first non-true result but not a short-circuit OR that returns on the first non-false result?

Re: Clearer Conditionals using De Morgan's Laws

#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).

Re: Clearer Conditionals using De Morgan's Laws

#14

Alternative title: Why a CS degree is worthwhile even if you're a web developer.

In my experience, electrical engineering curriculums instill logical thinking (e.g. Boolean logic) in ways that most computer science programs overlook. (This is fine and probably quite reasonable.) But when every logic gate counts, you must think differently about boolean expressions!

Re: Clearer Conditionals using De Morgan's Laws

#15
post #13

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

+1

I transferred midway through my undergraduate degree, and was surprised to discover that Karnaugh maps weren't taught at my destination school - they are such an intuitive and straightforward mechanism for whittling down complex logic into its simplest form.

Re: Clearer Conditionals using De Morgan's Laws

#18
post #7

Cool to see De Morgan's Laws used at a high level. But the real takeaway: rewrite your conditional until it makes sense.

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

Re: Clearer Conditionals using De Morgan's Laws

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

Or

    if (a && b && c) {
        // Do something.
    }

Re: Clearer Conditionals using De Morgan's Laws

#20
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.

Post reply on HN