<p>I'm sure you have all been there, you take on a project where there is a creaky old code base which is barely fit for purpose and you have to make the decision to either re-write it from scratch or repair what already exists.  </p>

<p>Conventional wisdom tends to suggest that you should never attempt a re-write from scratch as the risk of failure is very high.  So what did you do when faced with this problem, how did you make the decision and how did it turn out?</p>

[{'answer_id': 35239, 'author': 'juan', 'author_id': 1782, 'author_profile': 'https://Stackoverflow.com/users/1782', 'pm_score': 1, 'selected': False, 'text': '<p>It\'s not so black and white... it really depends on a lot of factors (the more important being "what does the person paying you want you to do")</p>\n\n<p>Where I work we re-wrote a development framework, and on the other hand, we keep modifying some old systems that cannot be migrated (because of the client\'s technology and time restrictions). In this case, we try to mantain the coding style and sometimes you have to implement a lot of workarounds because of the way it was built</p>\n'}, {'answer_id': 35241, 'author': 'levand', 'author_id': 3044, 'author_profile': 'https://Stackoverflow.com/users/3044', 'pm_score': 2, 'selected': False, 'text': '<p>Refactor unless it is very bad indeed.</p>\n\n<p><a href="http://www.joelonsoftware.com/articles/fog0000000069.html" rel="nofollow noreferrer">Joel has a lot to say on this... </a></p>\n\n<p>At the very least, rewrite the code with the old code in front of you and don\'t just start over from scratch. The old code may be terrible, but it is the way it is for a reason and if you ignore it you\'ll end up seeing the same bugs that were probably fixed years ago in the old code.</p>\n'}, {'answer_id': 35242, 'author': 'Sam Hasler', 'author_id': 2541, 'author_profile': 'https://Stackoverflow.com/users/2541', 'pm_score': 3, 'selected': False, 'text': '<p>See Joel Spolsky\'s essay <a href="http://www.joelonsoftware.com/articles/fog0000000069.html" rel="noreferrer">Things You Should Never Do</a>. In summary, when you rewrite you lose all the lessons you learned to make your current code work the way it needs to work.</p>\n\n<p>See also: <a href="http://www.laputan.org/mud/" rel="noreferrer">Big Ball of Mud</a></p>\n'}, {'answer_id': 35243, 'author': 'nsanders', 'author_id': 1244, 'author_profile': 'https://Stackoverflow.com/users/1244', 'pm_score': 3, 'selected': False, 'text': '<p>Just clean up the code a little bit every time you work with it.  If there isn\'t one already, setup a unit testing framework.  All new code should get tests written.  Any old code you fix as a result of bugs, try to slide in tests too.</p>\n\n<p>As the cleanups progress, you\'ll be able to sweep more and more of the nasty code into encapsulated bins.  Then you can pick those off one by one in the future.</p>\n\n<p>A tool like javadoc or doxygen, if not already in use, can also help improve code documentation and comprehensibility.  </p>\n\n<p>The arguments against a complete rewrite a pretty strong.  Those tons of "little bugs" and behaviors that were coded in over the time frame of the original project will sneak right back in again.</p>\n'}, {'answer_id': 35249, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 2, 'selected': False, 'text': "<p>One reason for rewriting at one of my previous jobs was an inability to find developers with enough experience to work  on the original code base.</p>\n\n<p>The decision was made to first clean up the underlying database structure, then rewrite in something that would make it easier to find full-time employees and/or contractors.</p>\n\n<p>I haven't heard yet how it worked out :)</p>\n\n<p>I think people have a tendency to go for rewrites because it seems more fun on the surface.</p>\n\n<p>We get to rebuild from scratch!</p>\n\n<p>We'll do it right <strong>this time</strong>!</p>\n\n<p>etc.</p>\n"}, {'answer_id': 35251, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 2, 'selected': False, 'text': "<p>It is rare for a re-write of anything complex to succeed.  It's tempting, but a low percentage strategy.</p>\n\n<p>Get legacy code under unit tests and refactor it, and/or completely replace small portions of it incrementally when opportune.</p>\n"}, {'answer_id': 35262, 'author': 'JosephStyons', 'author_id': 672, 'author_profile': 'https://Stackoverflow.com/users/672', 'pm_score': 4, 'selected': True, 'text': "<p>It really depends on how bad it is.</p>\n\n<p>If it's a small system, and you fully understand it, then a rewrite is not crazy.</p>\n\n<p>On the other hand, if it's a giant legacy monster with ten million lines of undocumented mystery code, then you're really going to have a hard time with a full rewrite.</p>\n\n<p>Points to consider:</p>\n\n<ul>\n<li>If it looks good to the user, they\nwon't care what kind of spaghetti\nmess it is for you.  On the other\nhand, if it's bad for them too, then\nit's easier to get agreement (and\npatience).</li>\n<li>If you do rewrite, try to do it one\npart at a time.  A messy,\ndisorganized codebase may make this\ndifficult (i.e, replacing just one\npart requires a rewrite of large\nicebergs of dependency code), but if\npossible, this makes it a lot easier\nto gradually do the rewrite and get\nfeedback from users along the way.</li>\n</ul>\n\n<p>I would really hesitate to take on a giant rewrite project for a large system without being able to release the new edition one part at a time.</p>\n"}, {'answer_id': 35278, 'author': 'Forgotten Semicolon', 'author_id': 1960, 'author_profile': 'https://Stackoverflow.com/users/1960', 'pm_score': 2, 'selected': False, 'text': '<p>There is a new book coming out, <a href="http://www.manning.com/baley/" rel="nofollow noreferrer">Brownfield Application Development in .NET</a> by Baley and Belcham.  The first chapter is free, and talks about these issues from a mostly platform agnostic perspective.</p>\n'}, {'answer_id': 35430, 'author': 'Stewart', 'author_id': 3260, 'author_profile': 'https://Stackoverflow.com/users/3260', 'pm_score': 1, 'selected': False, 'text': '<p>Depending on your situation, you <em>might</em> have another option: in-license third-party code.</p>\n\n<p>I\'ve consulted at a couple of companies where that would be the sensible choice, although seemingly "throwing away IP" can be a big barrier for management. At my current company, we seriously considered the viable option of using third-party code to replace our core framework, but that idea was ultimately rejected more for business reasons than technical reasons.</p>\n\n<p>To directly answer your question, we finally chose to rewrite the legacy framework - a decision we didn\'t take lightly!  14 months on, we don\'t regret this choice at all.  Just considering the time spent fixing bugs, our new framework has nearly paid for itself.  On the negative side, it is not quite feature-complete yet so we are in the unenviable position of maintaining two separate frameworks in parallel until we can port the last of our "front-end" applications.</p>\n'}, {'answer_id': 151619, 'author': 'Matt Cruikshank', 'author_id': 8643, 'author_profile': 'https://Stackoverflow.com/users/8643', 'pm_score': 1, 'selected': False, 'text': '<p>I highly recommend reading "Working Effectively with Legacy Code" by Michael Feathers.  It\'s coaching advice on how to refactor your code so that it is unit testable.</p>\n'}, {'answer_id': 151679, 'author': 'Tom Kidd', 'author_id': 2577, 'author_profile': 'https://Stackoverflow.com/users/2577', 'pm_score': 2, 'selected': False, 'text': '<p>Repair, or more importantly, refactor. Both because <a href="http://www.joelonsoftware.com/articles/fog0000000069.html" rel="nofollow noreferrer">Joel said so</a> and also because, if it\'s your code, you\'ve probably learned a ton more stuff since you touched this code last. If you wrote it in .NET 1.1, you can upgrade it to 3.5 SP1. You get to go in and purge all the old commented out code. You\'re 100x better as a developer now than when you first wrote this code. </p>\n\n<p>The one exception I think is when the code uses really antiquated technologies - in which case you might be better served by writing a new version. If you\'re looking at some VB6 app with 10,000 lines of code with an Access database backend obviously set up by someone who didn\'t know much about how databases work (which could very well be you eight years ago) then you can probably pull off a quicker, C#/SQL-based solution in a fraction of the time and code.</p>\n'}]
['https://Stackoverflow.com/questions/35233', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3305/']