Live data from Hacker News

Clearer Conditionals using De Morgan's Laws

robots.thoughtbot.com

71–80 of 82 posts

Re: Clearer Conditionals using De Morgan's Laws

#71

Earlier quoted context omitted.

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

I actually prefer your second form: return as soon as you know you are done in the function/method -- if nothing more can be done, then don't pretend to do any more. In the case of cleanup that must be done in the end, perhaps 2 functions would be better: a top level func to acquire and dispose of resources, calling an inner func to do as much work as it can with the resources. (assuming something like C that doesn't…

   > I actually prefer your second form: return as soon as
   > you know you are done in the function/method -- if 
   > nothing more can be done, then don't pretend to do 
   > any more.
The first form actually does that. As soon as one if condition fails, the code falls through down to the bottom and returns. This is most impressive in the initialization/identification phase (see sdio_open() [1]) because that has like a dozen commands it has to get through successfully. By collapsing this way it allows for common cleanup, and if the cleanup requirements change you only have to change it in one place.

The effect of the first form (in C) is to collapse all of the if's "else" clauses into a single one at the bottom.

As for finding the other end of an off screen if clause, I agree with you, that it is a crutch to use the 'find matching' command in the editor with the open brace. The interesting about this problem is that it is driven by the SDIO spec, which was driven by a strict requirement of backwards compatibility, which results in very carefully crafted command / response / flow options.

This discussion has been helpful for me as at some point I am going to have to describe this to people new to, or possibly unfamiliar with, programming which should be interesting.

[1] https://github.com/ChuckM/stm32f4-sdio-driver/blob/master/sd...

Re: Clearer Conditionals using De Morgan's Laws

#73
While it's nice learn De Morgan's Law if you don't know it, it occurs to me that the problem of finding the simplest version of a given logical expression in n logical variables is a classic NP-complete problem (or NP-hard, the simpler question of whether the negation of an expression can be reduced to true is basically SAT).

There is no easy method to reduce every expression to a simple normal form - the conjunctive normal form of a given be exponentially larger than the original expression, etc.

Re: Clearer Conditionals using De Morgan's Laws

#74
post #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 th…

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

Do you happen to have a link to an online course, textbook or other resource that covers this in a combined way that flows to well?

Re: Clearer Conditionals using De Morgan's Laws

#75
post #72
post #70

How does this get so many upvotes? Besides being rather straightforward logic, it's taught in surely every comp sci 101 course.

Clearly you haven't been to many comp sci 101 courses.

actually we learned this in AP comp sci in high school.

Re: Clearer Conditionals using De Morgan's Laws

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

Actually, double negatives are the norm in natural languages. Prescriptive "standard" English is weird in that there's a notion that there's a sort of algebraic multiplication of negatives/negators. As in most non-standard English dialects, a construct shaped like "I haven't done nothing" usually means exactly the same as "I haven't done anything" in most languages (and the double-negative version is usually considered to be more proper or more elegant). Yes, there is a way of looking at things that says we have gained some precision of language by the artificial imposition of a lot of the "rules" of English imposed (primarily and almost exclusively) by self-declared experts in the late seventeenth and eighteenth centuries, but there is no evidence that many of the rules that are more commonly broken than followed in vernacular English ever exited before Lowth, Murray and a handful of their contemporaries told us that they knew what was best for us.

Re: Clearer Conditionals using De Morgan's Laws

#80
post #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 th…

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

My Discrete Math 101 course (UNC-Wilmington) covered Karnaugh Maps, along with a heavy dose of Boolean Algebra. For various historical reasons, I've actually taken Discrete Math twice, and have noticed that the content of a class titled such can very dramatically. The other Discrete Math course had much less emphasis on Boolean Algebra and logic, and a lot more on elements of probability and statistics.

Post reply on HN