Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

(jcwren) RE: Building a byte to test truth table

by jcwren (Prior)
on Nov 02, 2000 at 01:53 UTC ( [id://39515]=note: print w/replies, xml ) Need Help??


in reply to Building a byte to test truth table

There's always the TMTOWTDI principle, but allow me to point out a potential problem here:

Your checks are now position dependent. If you move a block of code around in the section that builds the byte string, causing the byte position to change, you'll be spending the next few days debugging.

A more managable way to do this is with a bit field. By having each check toggle a bit on or off, you can now compare against a full value:
# # $word is 0, the default setting for bits being 0FF # $word = 0; $word |= 0x01 if ($obj->{organization} !~ /^\s*$/); ... $word |= 0x40 if ($obj->{report} !~ /^\s*$/); print "All fields supplied" if ($word == 0x00); print "Missing Organization" if ($word & 0x01 == 0);
Now, as long as you don't duplicate bit positions, you run into far less chances of fubar'ing the code should you insert tests, etc.

But this really isn't the best way either, since the code that sets the bit position and the code that tests for it are separated, and use magic numbers. You should use constants, to make sure you mean the same thing in both places.

There are a couple of good books that talk about how to prevent problems like this from creeping up on you. There is 'The Pragmatic Programmer', 'Code Complete', another good one whose name escapes me at the moment (it's on my bookshelf at home, and I'm not awake).

--Chris

e-mail jcwren

Replies are listed 'Best First'.
RE: (jcwren) RE: Building a byte to test truth table
by dws (Chancellor) on Nov 02, 2000 at 08:25 UTC
    Since you're using a hash to hold the object, reusing the keys to map to bit positions might help. For example:
    @fields = qw(organization report foo bar baz);
    $bit = 1;
    foreach $field ( @fields ) {
        $bitmask{$field} = $bit;
        $bit <<= 1;
    }
    

    This reduces the code that build $mask to

    foreach $field ( @fields ) {
        $mask |= $bit{$field} if $obj{$field} !~ /^\s*$/;
    }
    

    The challenging part is converting the static regular expressions to something dynamic. Here's one (untested) thought:

    sub testMask {
        my($bits, $on, $off) = @_;
        my($onmask, $testmask) = 0;
    
        foreach $field ( @$on ) {
            $onmask |= $bit{$field};
            $testmask |= $bit{$field};
        }
        foreach $ field ( @$off ) {
            $testmask |= $bit{$field};
        }
        return ($bits & $testmask) == $onmask;
    }
    

    This lets you rewrite the tests as:

    moreDatabaseProcessing() unless testMask($mask, \qw(organization), \qw(foo bar));
    

    The up side of this approach is that it's completely position independent. The downside is that it's not resilient against field name typos, though that can be mitigated with some extra tests.

      After looking at the code samples above and below, dws is the big winner! Thanks to all who gave useful samples, and of course, I took all suggestions and modified to my own look and feel. But the end result is closest to the code above.

      I just wanted to give a final thank you, now that it's all working, and since it seems appropriate to give thanks on big posts like 1000 or 500 or in my case 50.

      ALL HAIL BRAK!!!

RE: (jcwren) RE: Building a byte to test truth table
by PsychoSpunk (Hermit) on Nov 02, 2000 at 02:03 UTC
    Thanks, jcwren. In fact, your mention of "The Pragmatic Programmer" in a previous post, was the impetus for my recent visit to fatbrain.com. I have only digested a small portion of it, so I'd have eventually seen that sort of hint.

    And also thanks to Tye for his response too, but I figured that I'd cover it in one reply.

    /me buries his nose in the Camel to digest meaning. :) ALL HAIL BRAK!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://39515]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-26 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found