It may sound counterintuitive, but one of the tidbits I’ve gotten from Russ Olsen’s Design Patterns in Ruby is the idea of writing classes and methods in as general a form as possible. He discusses an example of different types of vehicles and writes:
By writing code that uses the most general type possible—for example, by treating all of our planes and trains and cars like vehicles whenever we can—we reduce the total amount of coupling in our code. Instead of having 42 classes that are all tied to cars and boats and airplanes, perhaps we end up with 40 classes that know only about vehicles. Chances are that the remaining two classes will still give us trouble if we have to add another kind of vehicle, but at least we have limited the damage.
In fact, if we have to change only a couple of classes, we have succeeded in separating out the parts that need to change (the two classes) from the parts that stay the same (the other 40 classes). The cumulative effect of turning down the coupling volume is that our code tends to be less likely to shatter in a horrendous chain reaction in the face of change.
This has tremendous value for testing. Instead of writing methods like:
load_preferences_page()
load_users_page()
load_security_page()
we can create a single, general method and simply pass it parameters thus:
load_page(preferences)
load_page(users)
load_page(security)
This way, if the method needs to be changed, you only have to change one method, making the code easier to maintain and much less error-prone.
Pretty elementary for you object-oriented veterans…but cool stuff nonetheless!