Rule of Separation
The Rule of Separation is one of my favorites because it tells me how to modularize code. I’ve heard lots of advice over the years to “modularize” and that “it’s an art” (which is a fancy way of people saying they don’t know). With this rule, we do know, and by the end of this piece, you’ll understand how to modularize your code.
In this piece we will:
define the Rule of Separation
use the rule to improve real code
discuss how to use the principle when architecting
discuss how to use the principle to refactor code.
Rule of Separation
Seperate policy from mechanism; separate interfaces from engines
In this section, we’ll only focus on the first half of the rule as the second half is very similar. Here are a couple of quick definitions:
Policy: Restricts functionality (ex. fail if username > 20 chars)
Mechanism: Adds functionality (ex. the ability to create users)
Therefore, the rule tells us that code which adds functionality should be ‘seperate’ from code which restricts functionality. A quick …