<p>I am completely on board with the ideas behind TDD, Refactoring and Patterns however it does seem like there is a huge gaping whole in these ideas, mainly that they are great for dev teams of 1, but when you start refactoring code that 10 people are working on you start getting merge conflicts all over the place and most diff/merge software can't tell that you refactored a function into its own class.</p>

<p>How do you clean up your code, by refactoring, without causing major headaches for everyone on your team?</p>

[{'answer_id': 68479, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 1, 'selected': False, 'text': '<p>Well, in practice it is rarely an issue.  Usually the different team members are working on different areas of the code, so there is no conflict.  Also, the bulk of the refactoring will go in when you are doing your TDD (which might even be before you check your code in, but most definitely before others start using and modifying it).</p>\n\n<p>If you find you are conflicting a lot due to refactorings, try checking in more frequently, or let people know who might be working on the same code that you are about to do some major rework.  Communication always helps.</p>\n'}, {'answer_id': 68483, 'author': 'Benjamin Autin', 'author_id': 1440933, 'author_profile': 'https://Stackoverflow.com/users/1440933', 'pm_score': 3, 'selected': False, 'text': '<p>Small changes committed often.</p>\n\n<p>As for your example, you would start by creating the class, committing that change. Then adding a similar function in the class as the old one and commit that change. Then change all the references from the old function to the new class function, commit that. Then remove the old function and commit that change.</p>\n\n<p>Of course, no one said it was going to be easy.</p>\n'}, {'answer_id': 68497, 'author': 'nlanza', 'author_id': 9373, 'author_profile': 'https://Stackoverflow.com/users/9373', 'pm_score': -1, 'selected': False, 'text': '<p>Communication.</p>\n\n<p>Tools can\'t solve this for you, unless the specific tool is your email or IM client.</p>\n\n<p>It\'s the same as if you were making any other major change in a shared project -- you need to be able to tell your coworkers/collaborators "hey, hands off for a couple of hours, I have a big change to the FooBar module coming in".</p>\n\n<p>Alternately, if you\'re going to be making a change so major that it has the potential to cause huge merge conflicts with the work of 10 other people, run the change by them beforehand. Have a code review. Ask for architectural input. Then, when you\'re as close to consensus as you\'re likely to get, take that virtual lock on the section of the repository you need, check in your changes, and send out an all-clear.</p>\n\n<p>It\'s not a perfect solution, but it\'s as close as you\'ll get. Lots of source control systems support explicit locks on sections of the source base, but I\'ve never really seen those lead to good results in these areas. It\'s a social problem, and you only really need to resort to technical solutions if you can\'t trust the people you\'re working with.</p>\n'}, {'answer_id': 68499, 'author': 'Bradley Harris', 'author_id': 10503, 'author_profile': 'https://Stackoverflow.com/users/10503', 'pm_score': 2, 'selected': False, 'text': '<p>Frequent check ins.  Team members should be checking in their changes and re-syncing their sandboxes <strong>at least</strong> once per day.  With more frequent check ins merge conflicts will occur less often and be easier to manage when they do occur. </p>\n'}, {'answer_id': 68528, 'author': 'kemiller2002', 'author_id': 1942, 'author_profile': 'https://Stackoverflow.com/users/1942', 'pm_score': 0, 'selected': False, 'text': "<p>You have to start slow and small.  Take a portion of code and look at all of the external interfaces.  You have to absolutely make sure that these don't change.  Now that you have defined that start to look at the internal structure of it and slowly change it around.  You'll have to work in small steps and check in frequently to avoid massive merge conflicts, which is one of the biggest problems you're going to have to work against.  In a team that size you'll never be able to check everything out and magically make it all better for them.  You might want to let people know ahead of time what you are going to do.  (which you should always plan out what you do before you do it anyway).  If other people are working on it, let them know what is going to change and how it will affect the class etc. </p>\n\n<p>The biggest thing you're going to have find out before you start trying is if people are on board with you.  If not, it might be a lost cause and will cause strife.  In this case, bad code and a functioning team that understands the mess the way it is might be better than refactored code.  In know this is counter intuitive, but a boss at my old job put it this way. He said the code is horrible, but it works, and the developers here know how it works, and that means the 1000 people using it can do their job which means we get to keep ours.  We hated the code and wanted to change it, but he was right.</p>\n"}, {'answer_id': 68542, 'author': 'Dana', 'author_id': 7856, 'author_profile': 'https://Stackoverflow.com/users/7856', 'pm_score': 1, 'selected': False, 'text': "<p>I think your team has to be on-board with your changes.  If you're doing large refactorings, making big changes to your codebase and object hierarchy, you're going to want to discuss the changes as a team.</p>\n"}, {'answer_id': 68553, 'author': 'Mike Reedell', 'author_id': 4897, 'author_profile': 'https://Stackoverflow.com/users/4897', 'pm_score': 0, 'selected': False, 'text': "<p>In my experience, merge conflicts are rarely an issue when doing small and medium scale refactoring on agile projects.  Large refactoring efforts can introduce some merge pain, but if it's done in bite size chunks, the pain can be reduced significantly.  Merge pain can also be reduced by using Subversion as your SCM as SVN will auto-merge non-conflicting changes.</p>\n\n<p>This strategy has worked well for teams I've been a part of, and most of those teams are 4+ developer pairs.</p>\n"}, {'answer_id': 68679, 'author': 'marcospereira', 'author_id': 4600, 'author_profile': 'https://Stackoverflow.com/users/4600', 'pm_score': 2, 'selected': False, 'text': '<p>I think that you should ask some questions to know why refactoring could hurts source control. </p>\n\n<ul>\n<li>Why are 10 people changing the same code at the same time?</li>\n<li>There are better tools to help you when doing refactoring/merges (maybe <a href="http://www.infoq.com/articles/dvcs-guide" rel="nofollow noreferrer">distributed version control</a>)?</li>\n</ul>\n\n<p>For the first question, maybe you haven\'t good separation of concerns and the code is tightly coupled. Or maybe the teams are not communicating well when assigning to tasks. For the second question, well, try some good dvcs (<a href="http://git.or.cz/" rel="nofollow noreferrer">git</a>, <a href="http://www.selenic.com/mercurial/wiki/" rel="nofollow noreferrer">mercurial</a>, <a href="http://bazaar-vcs.org/" rel="nofollow noreferrer">bazaar</a>) and see if any can help you.</p>\n\n<p>Kind Regards</p>\n'}, {'answer_id': 142925, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 1, 'selected': False, 'text': "<p>When I think a refactoring is going to be difficult to merge, I do this:</p>\n\n<ol>\n<li>Warn my team that the change is coming, and check if there are any pending changes that will be difficult to merge.</li>\n<li>Make sure I understand the change I'm going to make, so I can make it quickly.  Enhance test coverage now, if needed.</li>\n<li>Synchronize my machine to the latest source.</li>\n<li>Refactor, test, and commit.</li>\n<li>Notify my team, so they can synch up to my changes.</li>\n</ol>\n\n<p>Note that I'm making my refactoring change separately from functionality changes.</p>\n"}]
['https://Stackoverflow.com/questions/68459', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']