OdeToCode IC Logo

A Refactoring Experiment

Monday, April 9, 2012

obfuscated codeSoon I'll be giving a group of developers some code and asking them to do some refactoring.

The bad news is the code is hard to read. Some might say it's intentionally obfuscated, but let's not assume malice right away.

The good news is there are six working unit tests for the code.

There are two goals to the exercise. One goal is to give developers an opportunity to practice refactoring (a Kata, if you will). The way I'd attack the code is to start with some renaming operations, then extract some methods, then perhaps break down the one big class or eliminate a switch statement using patterns. There is no perfect answer.

The larger goal is to convince anyone not entirely sold on the benefit of automated tests how tests can empower them to work with, experiment with, and change existing code - even code they don't completely understand. Although this code was intentionally obfuscated, it's not unlike walking into the code for a complex domain the first time and not understanding why a wurgled customer can blargsmack on Tuesdays if they hold a vorkenhosen status. All domains are nonsense at the start.

If you want to try it too, the code is on github.

Jim Bolla Monday, April 9, 2012
Interesting. What are your thoughts on giving this exercise to an interviewee during a job interview?
Gravatar Scott Monday, April 9, 2012
@jim: I think it might be worth a try if you give them lots of time or do some pairing - definitely not for entry level though. FizzBuzz seems tough enough under the pressure of an interview! (www.codinghorror.com/...)
Jim Bolla Monday, April 9, 2012
Sadly I've interviewed quite a few asp.net developers with multiple years of experience that can't FizzBuzz in an hour. So sad. I'd also expect a disappointing number wouldn't be able to convert this code into something better in an hour.
Gravatar Jason Monday, April 9, 2012
For some reason when I saw the screenshot on my iPhone google reader I assumed this was done in JavaScript.

Once I realized it wasn't I though WTH...

Gravatar Pete Marshall Monday, April 9, 2012
Are you going to post solution at some point?
Gravatar scott Monday, April 9, 2012
@Jason: sweet!
Gravatar scott Monday, April 9, 2012
@Pete: I'm not decided if I want to post a solution in a branch, or cover it in the blog. Even if I cover it in the blog it will make it into the github repo at some point, so just watch there (or ping me in a week).
Gravatar Alex Tuesday, April 10, 2012
Really interesting ! Made me excited about it and i cant wait to give it a try :)
JP Tuesday, April 10, 2012
Well, I made some quick cleanup: http://pastebin.com/ujvki4zk 33 lines and in my opinion it's pretty easy on the eyes.
Gravatar Kevin Gregory Friday, April 13, 2012
Here's a stab at an extensible solution, although it does go a little beyond the original spec.


Thanks for the post, it was educational to see the power of unit tests so clearly while working on this refactoring. Also drives home the point about writing legible code...
Comments are closed.