|Syntactic Confectionery Delight|
What A Wonderful World: Bitmasks!by Revelation (Deacon)
|on Dec 08, 2002 at 07:07 UTC||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: