TulipeMoutarde.be

About

Limit hard dependencies in your code

Written on May 17 2012.

This blog post was written a long time ago and may not reflect my current opinion or might be technically out of date. Read with a grain of salt.

Igor Stasenko summarizes what he does to avoid coding with hard dependencies in his code:

Rule #1. never make more than 1 reference to any class external to your package in your code. an exception is kernel classes.

which means, that you are allowed to do `Set new` or `Array new`, in any place of your code but never `SomeFooClass new` more than once.

If your code having such dependency, localize it in single method:

fooClass
  ^ FooClass

and then use `self fooClass` everywhere.. Like that, if you will figure out one day, that sand moves under your feets, you’ll have to change only single method. :)

I never felt the need of doing that when the refactoring browser can rename all that for you but it’s still make sense if you want cleaner code.

Thanks Igor :)