Setup Cleanup

You often see cases where your code needs to do some setup before performing some operation and then some teardown afterward. If the setup and teardown are in different parts of your system, it's hard to see that they're matched up. I like to try to put the setup and teardown into the same method and ideally have the teardown as part of an ensure clause of the processing to make sure that the teardown always happens. If the setup and teardown are in different parts of the system, then it's hard to verify visually that the system actually works properly and that the teardown was actually run. For example, if you need to process data from a file, have one method that opens the file, calls processing, and then in an ensure block, has code to close the file. This way, you can visually see from that one method that the close will always be called properly.s

Download