<blockquote>
  <p>Where a new system concept or new technology is used, one has to build a
  system to throw away, for even the best planning is not so omniscient as
  to get it right the first time. Hence <strong>plan to throw one away; you will, anyhow.</strong></p>
  
  <p>-- Fred Brooks, <a href="http://en.wikipedia.org/wiki/The_Mythical_Man-Month" rel="noreferrer"><em>The Mythical Man-Month</em></a> [Emphasis mine]</p>
</blockquote>

<p>Build one to throw away.  That's what they told me.  Then they told me that we're all <a href="http://agilemanifesto.org/" rel="noreferrer">agile</a> now, so we should <a href="http://c2.com/cgi/wiki?RefactorMercilessly" rel="noreferrer">Refactor Mercilessly</a>.  What gives?</p>

<p>Is it <strong>always</strong> better to refactor my way out of trouble?  If not, can anyone suggest a rule-of-thumb to help me decide when to stick with it, and when to give up and start over?</p>

[{'answer_id': 78827, 'author': 'Chris Upchurch', 'author_id': 2600, 'author_profile': 'https://Stackoverflow.com/users/2600', 'pm_score': 3, 'selected': False, 'text': "<p>If you're merciless enough, the end result of refactoring will be pretty close to what you'd have gotten if you rebuilt from scratch, but you won't have been stuck with a non-working system during the process.</p>\n"}, {'answer_id': 78843, 'author': 'scubabbl', 'author_id': 9450, 'author_profile': 'https://Stackoverflow.com/users/9450', 'pm_score': 1, 'selected': False, 'text': "<p>I'll prototype when I'm trying to work out a new problem or functionality. After than, I'll rebuild it based on what I learned. Actually, that sounds a lot like refactoring... what? Maybe it's the same thing? Hmmm... </p>\n"}, {'answer_id': 78844, 'author': 'emk', 'author_id': 12089, 'author_profile': 'https://Stackoverflow.com/users/12089', 'pm_score': 5, 'selected': True, 'text': "<p>If you're doing test-driven development, you can refactor your way out of almost any trouble. I've changed major design decisions without much trouble, and rescued decade-old codebases.</p>\n\n<p>The only exception is when you've discovered that your architecture is completely wrong from beginning to end. For example, if you wrote your app using threads, but you discovered that you wanted a bunch of asynchronous state machines. At that point, go ahead and throw away the first draft.</p>\n"}, {'answer_id': 78883, 'author': 'metao', 'author_id': 11484, 'author_profile': 'https://Stackoverflow.com/users/11484', 'pm_score': 2, 'selected': False, 'text': "<p>There comes a point where refactoring is a waste of time. You just have to start again from scratch. If you keep your design fairly flexible, and you recognise that you don't know everything just yet, you won't have to throw anything away. A class might become redundant, of course, but you wont throw away a whole system. </p>\n\n<p>Having a flexible design is necessary to be able to refactor properly. Having no design or a rigid design means you WILL end up throwing something away - either because you cannot refactor, or because the constant refactoring degrades the maintainability of your code base. Few humans are meticulous and disciplined enough to be able to complete a long sequence of minor refactorings to maintain integrity. Unless you have an all-star team, this degradation will happen!</p>\n\n<p>TL;DR: You can refactor your way out of most trouble. Occasionally, though, you won't be able to refactor past some design elements. When that happens, it is time to start again - although hopefully you can re-use some of the components you have in place.</p>\n"}, {'answer_id': 79154, 'author': 'Dan Harper', 'author_id': 14530, 'author_profile': 'https://Stackoverflow.com/users/14530', 'pm_score': 1, 'selected': False, 'text': '<p>I think that throwing one away is sometimes the best way to go, but it can hurt.  One thing that I\'ve found that works well is to throw one away, but choose your technology well.</p>\n\n<p>For example, I\'ve written a large codebase in Ruby on Rails, and over the past 2-3 years, RoR has advanced a lot.  I also made some decisions in architecture that needed to be fixed.  So, I\'m throwing one away, and building a new one from scratch.  However, I\'m still able to use 70-80% or so of my old code as I\'m still writing in Ruby and Rails.  </p>\n\n<p>The major factor that has helped with that is that Rails forces you to write well structured code with separation of business logic and presentation layers.  I didn\'t get it perfect the first time around, but since everything is fairly well separated and DRY, porting the code to Rails v2.1, re-architecting the problem areas, and re-writing some "problem" features has been a fairly pain free experience.</p>\n\n<p>So, by choosing a great technology from the start, I\'ve been able to throw one away, but still take with me 70-80% of the old stuff that still works.</p>\n'}, {'answer_id': 79167, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>In a later essay in The Mythical Man Month, Brooks warns that he\'s found that if you do indeed plan to throw 1 away, you\'ll end up throwing 2 away!</p>\n\n<p>I personally saw this happen in real life; we assigned a version 1 of the project as a quick throw-away to a mediocre programmer, because "we plan to throw it away later -- we will anyhow."  We ended up having to rewrite it for version 2, but that one got thrown away too.  I never saw version 3 - the company went out of business.</p>\n\n<p>I think when Brooks says "plan to throw one away, you will anyway" it\'s more like the statement "the number of bugs remaining to be found is \'n+1\'."  That is, it\'s a ha-ha-only-serious statement about Murphy\'s law, rather than practical advice.  The lessons to take away from it is that prototypes are valuable, good writing is rewriting, and don\'t be afraid to abandon something that isn\'t working.</p>\n\n<p>However, it has to come down to a judgement call because as Joel Spolsky has talked about in several essays, the option to throw away and start over is tempting because code is easier to write than to read, and more fun to write than to maintain, so your natural inclination will always be to start over even when that isn\'t really the best thing to do.</p>\n'}, {'answer_id': 79374, 'author': 'Paulus', 'author_id': 14209, 'author_profile': 'https://Stackoverflow.com/users/14209', 'pm_score': 4, 'selected': False, 'text': '<h2>Throw away early, refactor later</h2>\n\n<p>Throwing away is OK for small systems, but if the size of the system is huge, you simply do not have the resources to do so. </p>\n\n<p>You could, however, create a small pilot project that implements only the very essential features of the actual project. After some trial and error and learning and throwing away stuff, you end up with a solid core and a better understanding for the actual project. Then you let the size of the project grow, by adding all the features needed. But once you get there, no way you can throw away the core. Only refactoring.</p>\n'}, {'answer_id': 79396, 'author': 'Jeff Heigl', 'author_id': 14649, 'author_profile': 'https://Stackoverflow.com/users/14649', 'pm_score': 2, 'selected': False, 'text': "<p>Different situations require different approaches.  Personally I perfer refactoring to a better design whenever possible.  Refactoring leads to less bugs than a rewrite.</p>\n\n<p>But, even if you plan to throw one away, its still a good idea to write a bunch of acceptance tests to make sure your 2nd version is on the right track. Then you can migrate towards the next version piece by piece while ensuring your functionality isnt changing from the user's perspective. Sounds a bit like refactoring, just a little sloppier I guess.</p>\n"}, {'answer_id': 79441, 'author': 'kbaribeau', 'author_id': 8085, 'author_profile': 'https://Stackoverflow.com/users/8085', 'pm_score': 2, 'selected': False, 'text': "<p>One of the central points of The Mythical Man Month was that the hard part of software development is figuring out what to say, not how to say it.</p>\n\n<p>The way I've interpreted this recently is that the most value you get out of the first draft is the requirements you've gathered and preserved in the form of tests.  If you're careful not to test things that aren't actually requirements of the system, you <em>can</em> refactor your way out of any mess.</p>\n\n<p>So long as you don't code yourself into a trap where you have to start throwing out tests, you are OK to throw out as much code as you want without losing a significant amount of real work.</p>\n"}, {'answer_id': 79494, 'author': 'marcospereira', 'author_id': 4600, 'author_profile': 'https://Stackoverflow.com/users/4600', 'pm_score': 2, 'selected': False, 'text': '<p>When talking about Agile, you could do both, but in a general way, you will do <a href="http://www.extremeprogramming.org/rules/spike.html" rel="nofollow noreferrer">spikes</a> (prototypes) only to try specific issues, learn about them and be able to do better estimates. Throw away when you are doing a simple spike and refactoring when you are really coding the application.</p>\n\n<p>Kind Regards</p>\n'}, {'answer_id': 100977, 'author': 'quamrana', 'author_id': 4834, 'author_profile': 'https://Stackoverflow.com/users/4834', 'pm_score': 2, 'selected': False, 'text': '<p>My general advice here is to refactor an existing system away from its bad designs to a system with better designs.  This maintains the system and allows it to be deployed at all times.  If you start from scratch it may be a while before you can deploy, or never.</p>\n\n<p>If you are talking about just writing some brand new code where there is no existing system, then quite often its a good idea to write a little bit of code, however you want, then throw that away since it was never deployed and start again (using TDD).</p>\n'}, {'answer_id': 1915867, 'author': 'Kzqai', 'author_id': 69993, 'author_profile': 'https://Stackoverflow.com/users/69993', 'pm_score': 1, 'selected': False, 'text': "<p>I think that your version control system plays a large role here.  If you run a distributed version control system with easy branching (git, mercurial, these days), then you'll be able to prototype easier, and refactor easier, all while still having a valid working copy.  Anything else requires so much more discipline.</p>\n"}, {'answer_id': 1939503, 'author': 'Xav', 'author_id': 214431, 'author_profile': 'https://Stackoverflow.com/users/214431', 'pm_score': 0, 'selected': False, 'text': '<p>As a development manager in this organisation, I\'m "not allowed" to write production code. </p>\n\n<p>I (ab)use that rule to knock out quick, dirty proof-of-concept code that addresses one or other sticking point, then I check it in to source control and point a "proper" dev at it and say "Here\'s how it\'s done, now do it properly."</p>\n\n<p>That\'s as close as we get to "one to throw away" here, and it\'s probably taken me a couple of hours max to knock together. Spending time putting in things like error handling, boundary-checking and all the other bits that make good code would be a waste of time for this sort of work, yet it means that the guys who are getting paid to write production code can spend their time writing production code and don\'t have excuses like "it\'s only a prototype" when it comes to code-review time. </p>\n\n<p>Building one to throw away is too often used as an excuse for not doing the job properly. That means that you don\'t actually encounter enough of the issues in the process to learn enough to make it a good use of anyone\'s time. And doing it properly, only to throw it away, is even more wasteful.</p>\n\n<p>As several people have previously said, the most important feature in any software is that it ships. With that in mind, I\'d build "one to get people to pay me for" any day, and my mercilessness in terms of refactoring is to allow only enough of it to get a product that works and can be reasonably maintained.</p>\n'}, {'answer_id': 39946418, 'author': 'Stan J.', 'author_id': 6945699, 'author_profile': 'https://Stackoverflow.com/users/6945699', 'pm_score': 0, 'selected': False, 'text': "<p>It is easy to build one to throw away on any configuration management system that supports the concept of a branch. If you are introducing a radical design change into an existing system that is in the field and is the source of your paycheck; you darn well better branch; prototype; and throw it away if it doesn't work.</p>\n\n<p>Refactoring a large legacy cash cow system often leads to plain old fashioned hacking. Refactoring just sounds a lot better than hacking I guess. </p>\n"}]
['https://Stackoverflow.com/questions/78806', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1260685/']