The Encapsulated Hack

As is pretty obvious, I'm a big fan of good OO design.  But I'm
a bigger fan of maintainable apps, and one of my favorite tricks when
evolving apps is what I call the "encapsulated hack."

Take, for
example, a CFC that needs access to an
application-scope variable.  What's the right solution? 
Package the variable up in such a way that it can be passed to the CFC,
so the CFC doesn't have to break encapsulation by calling the
application-scope variable.  Much of the time, that's easy to do,
but not always.  When it's not possible, I hack it. 

Here's what I'd do:

<cffunction name="getSomeVar" access="private" …>
  <!—TODO: encapsulated hack —>
  <cfreturn application.someVar />

you say, "that's breaking encapsulation!," and you'd be right. 
But notice that I'm breaking encapsulation in exactly one place,
and that place is itself encapsulated.  Now, when I do it the right
way, I can change the implementation of this one method, and nothing
else has to change.

Speaking from experience working with a single large
app over several years, I can tell you that this is very helpful in
allowing you to move forward with a good OO design, without having to
do everything
all at once.  Just make sure you clearly
indicate that you've got a hack, and that it needs to be fixed. 
If you're using CFEclipse, you can use a TODO comment as I have, open
the 'tasks' view, and you'll get a listing of all such todos in your

Comments are closed.