Code Smells: TOO HARD

2012-05-22
2 min read

If you’re attacking a problem that just isn’t coming together, stop and take a step back. The feeling of “GAH WHY IS THIS SO HARD” is a preemptive code smell, alerting you that you’re about to write bad code.

My most recent TOO HARD experience was with table_print, my gem that prints objects as tables. I had Law of Demeter violations all over the place, spaghetti-coding myself into a ball I knew was not going to be maintainable. If it feels hard now, it’s definitely going to feel hard in six months.

I snapped out of it when I realized I didn’t have any objects to represent a row or groups of rows, just hashes upon hashes to try to keep everything straight. But that insight was a fluke - I need some concrete strategies for addressing this code smell.

Next time something is TOO HARD, I’ll try to see the problem from a different perspective. Some things to try:

  • Maybe write a paragraph or two about what I’m doing, and look at the words I use. Do I have objects representing my nouns? Do I have methods representing my verbs?
  • Zoom out, or zoom in - think about how this problem fits into the broader ecosystem, or what the most basic building blocks are.
  • Refresh myself on ruby design patterns - maybe there’s already a good way of doing what I’m trying to do.
  • Get another mind on it. Rubber ducking and pairing will at least make me collect my thoughts, even if my partner has nothing concrete to add.