The Junk Drawer Mini-Pattern

by Charles Miller on May 28, 2003

This pattern originally had a different name, chosen in ignorance of its ugly racist connotations. Thanks to @irlnomi on Twitter for pointing this out, and @betsythemuffin for proposing a better name.

Name: The Junk Drawer

Context:

You are in some way subject to architectural, framework or language constraints that force you to write ugly code. For example, your UI framework requires one kind of object, your persistence framework requires another, and you keep having to convert between the two.

Forces:

  • You will not always be in a position to remove the root cause of the ugliness.
  • Ugly code is usually boiler-plate, and not very interesting.
  • It is better not to have ugly, boiler-plate code obscuring code that actually does something interesting.
  • Of course, [insert-your-favourite-language-or-framework-here] is immune to this problem. Obviously this pattern does not apply to you.

Therefore

Stash your ugly code inside a Junk Drawer. The junk drawer is a single file or class where issues of code cleanliness do not apply. It is entered by reputable developers with no small amount of trepidation, and left as quickly as possible. On the other hand, it does the job, and it keeps the bad elements away from more cultured code.

If the constraints that caused you to stash your code in the junk drawer are widespread, though, it may end up being the biggest single file in your application.

Examples of use:

None that this author is willing to admit to.

Previously: This one is Alan's fault

Next: Impossible Performance Art