Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'd just like to propose that developers of larger packages use bitmasks instead of lots of 0,1 (true or false) variables. This may be the standard for enlightened coders, or it might be a foolish notion by me, so I decided to ask you guys to assess the validity of my reasoning.

My reasoning for this is that Bitmasks can:
Save space, if you are shifting a lot. ( No more  my($is_alive,$is_cool,$is_a_smurf) = @_; ) However, this does not seem to be a valid purpose in and of itself, since most maintenance programmers may not come with knowledge of bitmasks, but will definitely understand true and false values.

Therefore, I reduce code, but the cons outnumber the pros, right? But no! What if I want to add a value to my subroutine checks? Maintenance, and code changes become a big hassle. I'll have to add that extra value in every subroutine that uses it. So how do we solve this? You could use a hash with all the values! A hash is especially a good fit if you are using an OO construct, where a HoH and perldsc types of things are right up your alley.

But I believe that bitmasks are still more helpful than hashes with 0,1 values, because they help you organize your packages, by allowing you to put constants somewhere else. I like to remind myself, and force myself to use the same constructs everywhere in my code. Code consistency means code clarity for a maintenance programmer, and it means simplicity for me. (My reasoning for using strict.) The same way I like to remind myself of the constructs I'm using, and be able to have one persistent variable that I can pass around to subroutines. Why us a hash, when you can use a scalar? And so... I use Bitmasks.

Does this sound valid to you, or am I just being an idiot? ( A why would be helpful) Is there somewhere else where Bitmasks should be used, instead? If you use Bitmasks for a different reason in this case, what is it?
In addition, here's a quick example of where I think Bitmasks are helpful: Pretend this is a random module encapsulated by a greater package. Instead of:
sub test_stuff { my($is_alive,$is_cool,$is_a_smurf,$is_a_moose) = @_; if ( $is_alive ) { print "It's alive!"; } elsif ( $is_cool || $is_a_smurf ) { print "It's either cool xor a smurf"; } elsif ( $is_a_moose ) { print "It's a moose"; } else { print "It's something strange :("; } } sub test_more_stuff { my($is_a_cow,$is_a_pow,$is_a_how,$is_a_now) = @_; if ( $is_a_cow ) { print "It's alive!"; } elsif { etc... etc...} }
package main; use Rev::Bitmasks qw(ALIVE COOL SMURF MOOSE COW POW HOW BOW); sub test_stuff { my $persistent = shift; if ( $persistent & ALIVE ) { print "It's alive!"; } elsif ( $persistent & (COOL | SMURF) ) { print "It's either cool xor a smurf"; } elsif ( $persistent & MOOSE ) { print "It's a moose"; } else { print "It's something strange :("; } } sub test_more_stuff { my $persistent = shift; if ( $persistent & COW ) { print "It's alive!"; } elsif { etc... etc...} } package Rev::Bitmasks; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(ALIVE COOL SMURF MOOSE COW POW HOW BOW); sub ALIVE () { 1; }; sub COOL () { 2; }; sub SMURF () { 4; }; sub MOOSE () { 8; }; sub COW () { 16; }; sub POW () { 32; }; sub HOW () { 64; }; sub BOW () { 128; };

In reply to What A Wonderful World: Bitmasks! by Revelation

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-23 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found