« (Re)learning to ice skate | Main | An interesting mix (of oil and water, kind of) »

December 29, 2006

Coding with negative logic - tips and tricks?

The last few days I've been working on a change that involves somewhat complex logic. I found that the statements I struggled with the most were dealing with negative logic:

$list->disable_columns('env_overlap') unless 
  ((!$st->get('enable_te_length_user') && $st->get('te_suppress_name')
    || $st->get('enable_te_length_user')
      && $st->get('te_length_user') ne 'N')
  && !$st->get('te_disable_overlapping_envelopes'));

I found myself translating this into a "positive" logic sequence, but kept getting confused whether I was thinking about the translated logic or the actual logic.

I built a couple of logic charts to help:

Effects of te_suppress_name and te_length switches

Envelope duration
E None non-None
n |-----------|-----------|
v E Allowed |User-def’d | User-def’d|
N d |-----------|-----------|
a i Not allowed | N / A | Auto-def’d|
m t |-----------|-----------|
e

Effects of enable_te_length_user and te_length_user switches

Per-user Envelope duration
E P None non-None
n e |-----------|-----------|
a r Enabled |User-def’d |^Auto-def’d|
b U |-----------|-----------|
l s Not enabled | Company | Company |
e e |-----------|-----------|
r

Would love to hear how others deal with this (yes, I know Damian Conway recommends avoiding it altogether).

I was suprised how difficult it was to "think negatively." Maybe there's a heart-warming story in here about how human nature is predisposed to be positive.

Posted by pete at December 29, 2006 9:21 AM

Comments

Good news! very intresting blog ;)

Posted by: viktoria332 at January 22, 2007 1:50 PM