<p>I've joined a team that works on a product. This product has been around for ~5 years or so, and uses ASP.NET WebForms. Its original architecture has faded over time, and things have become relatively disorganized throughout the solution. It's by no means terrible, but definitely can use some work; you all know what I mean.</p>

<p>I've been performing some refactorings since coming on to the project team about 6 months ago. Some of those refactorings are simple, Extract Method, Pull Method Up, etc. Some of the refactorings are more structural. The latter changes make me nervous as there isn't a comprehensive suite of unit tests to accompany every component. </p>

<p>The whole team is on board for the need to make structural changes through refactoring, but our Project Manager has expressed some concerns that we don't have adequate tests to make refactorings with the confidence that we aren't introducing regression bugs into the system. He would like us to write more tests first (against the existing architecture), then perform the refactorings. My argument is that the system's class structure is too tightly coupled to write adequate tests, and that using a more Test Driven approach while we perform our refactorings may be better. What I mean by this is not writing tests against the existing components, but writing tests for specific functional requirements, then refactoring existing code to meet those requirements. This will allow us to write tests that will probably have more longevity in the system, rather than writing a bunch of 'throw away' tests.</p>

<p>Does anyone have any experience as to what the best course of action is? I have my own thoughts, but would like to hear some input from the community.</p>

[{'answer_id': 20269, 'author': 'Jay Mooney', 'author_id': 733, 'author_profile': 'https://Stackoverflow.com/users/733', 'pm_score': 2, 'selected': False, 'text': '<p>Can you re-factor in parallel?  What I mean is re-write the pieces you want to refactor using TDD, but leave the existing code base in place.  Then phase out the existing code when your new tests meet the needs for your PM?</p>\n'}, {'answer_id': 20271, 'author': 'Ian Nelson', 'author_id': 2084, 'author_profile': 'https://Stackoverflow.com/users/2084', 'pm_score': 4, 'selected': True, 'text': '<p>Your PM\'s concerns are valid - make sure you get your system under test before making any major refactorings.</p>\n\n<p>I would strongly recommend getting a copy of Michael Feather\'s book <a href="https://rads.stackoverflow.com/amzn/click/com/0131177052" rel="noreferrer" rel="nofollow noreferrer">Working Effectively With Legacy Code</a> (by "Legacy Code" Feathers means any system that isn\'t adequately covered by unit tests).  This is chock full of good ideas for how to break down those couplings and dependencies you speak of, in a safe manner that won\'t risk introducing regression bugs.</p>\n\n<p>Good luck with the refactoring programme; in my experience it\'s an enjoyable and cathartic process from which you can learn a lot.</p>\n'}, {'answer_id': 24162, 'author': 'jonezy', 'author_id': 2272, 'author_profile': 'https://Stackoverflow.com/users/2272', 'pm_score': 0, 'selected': False, 'text': '<p>Just tossing out a second recommendation for Working Effectively with Legacy Code, an excellent book that really opened my eyes to the fact that almost any old / crappy / untestable code can be wrangled!</p>\n'}, {'answer_id': 79863, 'author': 'Josh', 'author_id': 11702, 'author_profile': 'https://Stackoverflow.com/users/11702', 'pm_score': 1, 'selected': False, 'text': '<p>I would also like to throw in a suggestion to visit the <a href="http://www.refactoring.com/" rel="nofollow noreferrer">Refactoring</a> website by Martin Fowler. He literally wrote the book on this stuff.</p>\n\n<p>As far as introducing unit tests into the equation the best method I have found is to find a top level component and identify all the external dependencies it has on concrete objects and replace them with interfaces. Once you\'ve done that it will be a lot easier to write unit tests against your code base and you can do it one component at a time. Even better, you won\'t have to throw away any unit tests.</p>\n\n<p>Unit testing ASP.Net can be tricky, but there are plenty of frameworks that make it easier to do. <a href="http://www.asp.net/mvc/" rel="nofollow noreferrer">ASP.Net MVC</a>, and <a href="http://www.codeplex.com/websf" rel="nofollow noreferrer">WCSF</a> to name a few.</p>\n'}, {'answer_id': 454298, 'author': 'Theo Lenndorff', 'author_id': 53881, 'author_profile': 'https://Stackoverflow.com/users/53881', 'pm_score': 0, 'selected': False, 'text': '<p>Totally agree with the answer from <a href="https://stackoverflow.com/users/2084/ian-nelson">Ian Nelson</a>. Additionally I would start to get some "high level" tests (functional or component tests) in place to preserve the behaviour from the view point of the user. This point might be the most important concern for your PM.</p>\n'}]
['https://Stackoverflow.com/questions/20262', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/769/']