Below we have an EntityType enum which can identify both Natural- and Legal entities.
Now let’s see how we got here.
Creating the EntityType enum
The Flags attribute
The Flags attribute marks the enum so that it can be treated as a bit field. To get our intended behaviour (distinguish between Natural- and Legal entities) for flags, the integer values need to be factors of 2. This ensures that there is no overlap between any combinations of flags, for example: There is no way for 0, 1 and 2 to combine and make 4. Read more about the Flags attribute on MSDN.
Shift left (“«”)
This operation “shifts” the one’s in a binary representation to the left, by adding 0s to the end. Therefore the integer value 1 (00000001) shifted 5 bits to the left, becomes 32 (00100000). Read more about the Shift left operation on MSDN.
Bitwise OR (“|”)
|The return type of this operation is determined by its arguments. The operator calculates the binary representation of each argument and then does a bitwise OR operation on them. ‘(Natural||1)’ would compute as follows:|
|Don’t make the mistake of assuming that this operator acts as an integer + operation (although 32||1 = 33 and 32 + 1 = 33), using 32||33 as an example:|
Read more about the Bitwise OR operation on MSDN.
Putting it to use
Based on what we’ve learnt above, we know that the enum class could have just used explicit integer values. See this Gist. We also know that 2n & ( 2n + x ) = 2n.
Looking at our enum we can see that there are two “Macro” types of entities (Natural and Legal), which are then further divided into more specific entity types (Individuals and Body Corporates for example). If we have an Entity class with a property to identify its “Entity Type” and we store the integer value in the database (33 for Individual and 67 for Body Corporate) how would we be able to identify Natural entities which are Legal entities?
We could just check if an integer is within the range between 32 and 64, like this. Or we could apply what we’ve learnt from the Bitwise AND operator, if ‘Natural’ is 25 (32) and ‘Individual’ is 25+1 (33) we know that ‘Individual’ & ‘Natural’ == ‘Natural’, so we can add this property to our Entity class:
Our tests will pass regardless of our implementation.
Robust and Maintainable: Adding another “Macro” type (like “Artificial”) and some sub types (“Toaster” and “Roomba”) is trivial. You just add another “Marco’ enum entry, shifted one more bit to the left than the previous one, then add another list of specific entries. Checking if an entity is ‘Artificial’ is just as simple:
What would happen if you run out of legal entity integers?* It’s not entirely inconceivable that there can be more than 16 types of Legal Entities (especially once the robots become self aware and are no longer happy being referred to as ‘Artificial’ entities). The only thing you would have to do is crank up the bit shifting for all the ‘Macro’ types one more bit to the left. You wouldn’t have to worry about any number range checking, or have to now update each entry in the enum’s integer value. Had we not been so diligent and placed the checks in the Entity class, we could have had to replace loads of hard-coded integers everywhere in the code. Using the flags, all of these checks would just keep working as expected. (Remember kids, Magic Numbers are evil.)
|Complex: The biggest con for using flags to solve this problem is complexity. It might not be immediately clear to someone inheriting the code what exactly it does and how it works. Additionally, you might not see that you’re “running out of legal entity integers”. “Natural||1” will evaluate to the same value as “Natural||33” when “Natural” is 32 (or 1 « 5). Good code comments and appropriately defined unit tests could solve, or at least mitigate, these issues.|
|Not an easy reference material: This somewhat ties in with the complexity con. If you’re looking at the database tables and see EntityType is equal to 34, it’s going to take you a while to determine that that refers to ‘JointAccount’(Natural||2).|
*Hopefully you’d pick this up early enough or else you might also need to write some scripts to keep your database values consistent. This would be an issue with either approach.