Design Rules
Note |
This is only an attempt to provide a definition / vision on what we mean by "design rule". This is not intended to be the final word on it, but rather a working definition open for dicussion. |
A Design Rule is a (relatively) easily verifiable rule that constrains how code is to be structured.
By adhering to a design rule developers trade away some flexibility on how they write and structure code
in order to make it easier to manually or automatically verify important properties of their software.
A design rule is not the same as the actual property one is seaking to guarantee (let's call this the
interesting property) but can actually be a weaker or stronger property than the
interesting property. The property specified by the design rule is a structural property of their code which developer chose to adopt because it:
- Is easier to verify (manually or automatically) than the interesting property.
- The fact that the desing-rule property holds boosts the developer's confidence in that the interesting property holds.
- It makes the code easier to read and understand (because it makes the interesting property more obvious and easier to verify).
For example, let's consider the opening and closing of files. An interesting property we may wish to ensure ourselves of as developers is that:
- All files that are opened are always eventually closed.
This property is potentially very hard to verify. However, it becomes easier to verify
if the following design rule is followed:
- Files that are opened by a method should be closed before exiting the method.
By adopting this desing rule the developer agrees to always open and close file in manner similar
to the following idiom:
void operateOnFile(String filename) {
... open file ...
... operate on the file ...
... close the file ...
}
Note that this design rule may be overly restrictive for some situations. In such cases, the desing
rule may need to be extended to allow for exceptions. For example, we could ammend the rule to allow
open files to be stored in instance variables of certain objects. For these more complex cases we may then chose to adopt other, perhaps more application specific design rules to gain confidence that those files are also not left accidentally open.