Over a million developers have joined DZone.

How To Create a Legacy Code Kata Source Code

· Java Zone

Learn more about the advantages of moving from a monolithic to microservices architecture.  Brought to you in partnership with IBM.

From time to time I do a Clean Code Workshop for coworkers or customers. A large part of these workshops deals with legacy code. I always start with the Ugly Trivia Game by J. B. Rainsberger, which actually isn’t by him, but I use his version anyway. While it is a great resource it gets boring after you have reviewed, tested, refactored, tested it some more and reviewed the tests and refactorings. Also: does it really look like the average legacy code we find in our projects? I don’t think so. Real life legacy code has more dependencies and does more really stupid stuff. My favorite so far was a class that loaded stuff from the database in the constructor. Not fun.

Therefor I like to create a little code base for working with legacy code that is very realistic but still small enough so one can handle it in the setting of a workshop and it is actually really simple to do that:

First pick some class that is ugly. Big methods are great. Sometimes you come a cross classes that are super tangled with 2-5 other classes. Those are super awesome. Don’t pick anything to large. A class with 5000 lines just create more work, not more learning opportunities.

Second copy that class (or that small class cluster) in a new project. No dependencies, none of the other stuff it works with. Most likely it wont compile, we gonna fix that in the next step.

Third fix all the compile problems using one of the following approaches:

  • Just add simple libraries like logging or Apache commons to the project.
  • Create missing classes using your IDE, will all the right methods. If the methods look like getter and setter, actually make them getter and setter.
  • If the methods look more complicated just throw aSomethingTerribleIsHappeningHere exception.

This is really fast, works with 98% of all legacy code I came across so far and creates some mean challenges for working with legacy code.

From Idea to Application gives you the architecture to quickly build, manage and run a range of applications (web, mobile, big data, new smart devices, etc.) on an open-standard, cloud-based platform. See why developers are using IBM Bluemix. Brought to you in partnership with IBM.


Published at DZone with permission of Jens Schauder, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}