What it is and what we can do about it.\n---------------------------------------\n\n[Legacy code](https://hackernoon.com/tagged/legacy-code) is the bane of most developers. But it is a reality we all have to deal with.\n\nIt’s code you’ve inherited from someone else, maybe from way older versions of your app. And often it comes without any proper [documentation](https://hackernoon.com/tagged/documentation), a lot of smells, and a nagging feeling that “we should probably rework this… but it works as is though”.\n\nI’m sure that some [developers](https://hackernoon.com/tagged/developers) will spend most of their time on relatively new codebases, ones they will build from the ground up — and that they’ll never deal with legacy code as such.\n\n> Even then, I’m of the opinion you can inherit legacy code from yourself — or more accurately from “5 years ago"-yourself.\n\nI remember getting to use code that was seemingly constructing a very long string, based on a lot of options, parameters and collections. And then, it _eval'_d it.\n\nWhen I went to see the colleague who built the library, turns out that at the time they just didn’t know how much [metaprogramming](https://hackernoon.com/tagged/metaprogramming) you could do in Ruby without ever needing an _eval_ call.\n\nSome other parts of the code were straight-up reimplementation of things the framework could do by itself. “We started 8 years ago with version 1 dot something" — we were now at version 4.2.\n\nTime makes foreign all things, I guess.\n\nI should clarify, legacy code is not bad code per se. It is only old, or a bit more complex than it should and the author is nowhere to be found.\n\nI hated it.\n\n!(https://hackernoon.com/hn-images/0*OBpLfN_1Qw9Mh9aw.)\n\nAnd I think that’s the approach we tend to have most often towards legacy code. We tend go nuclear, rewrite it all and damn those who came before us.\n\nTurns out that’s not so great.\n\nLack of pre-written tests, lacking comprehensive knowledge of the code being rewritten and poor structure in our refactoring operations often leave us with a mess of code that only seem better to us than the previous one because this time, it’s made by us.\n\nI’ve left behind me quite messy code. Which then turns into its own legacy code.\n\nWhat to do then?\n\nIt turns out, dealing with legacy code is not spring cleaning. It’s more like improving your environment. You upgrade parts of it, one by one, whenever you stumble upon something whose improvement is in your capabilities.\n\nIt’s writing a test for that modification you had to make on that one old library, it’s annotating bits of hackey code after spending a while figuring them out, it’s replacing that one while loop by a for or an each.\n\nLegacy code is never a business priority. Trying to make it one will often turn it into new, soon-to-be legacy code. Let’s just factor it in our task estimates and do whatever little we can whenever we can about it instead.