<p>I've just coded a 700 line class. Awful. I hang my head in shame.  It's as opposite to <a href="http://en.wikipedia.org/wiki/Don&#39;t_repeat_yourself" rel="nofollow noreferrer">DRY</a> as a British summer.</p>

<p>It's full of cut and paste with minor tweaks here and there.  This makes it's a prime candidate for refactoring.  Before I embark on this, I'd thought I'd ask when you have lots of repetition, what are the first refactoring opportunities you look for?</p>

<p>For the record, mine are probably using:</p>

<ol>
<li>Generic classes and methods</li>
<li>Method overloading/chaining.</li>
</ol>

<p>What are yours?</p>

[{'answer_id': 60105, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 2, 'selected': False, 'text': '<p>#region</p>\n\n<p>I made a 1,000 line class only one line with it!</p>\n\n<p>In all seriousness, the best way to avoid repetition is the things covered in your list, as well as fully utilizing polymorphism, examine your class and discover what would best be done in a base class, and how different components of it can be broken away a subclasses.</p>\n'}, {'answer_id': 60114, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 1, 'selected': False, 'text': '<p>First of all, I would recommend refactoring much sooner than when you are done with the first version of the class.  Anytime you see duplication, eliminate it ASAP.  This may take a little longer initially, but I think the results end up being a lot cleaner, and it helps you rethink your code as you go to ensure you are doing things right.</p>\n\n<p>As for my favorite way of removing duplication.... Closures, especially in my favorite language (Ruby).  They tend to be a really concise way of taking 2 pieces of code and merging the similarities.  Of course (like any "best practice" or tip), this can not be blindly done... I just find them really fun to use when I can use them.</p>\n'}, {'answer_id': 60124, 'author': 'Nick Randell', 'author_id': 5932, 'author_profile': 'https://Stackoverflow.com/users/5932', 'pm_score': 1, 'selected': False, 'text': "<p>One of the things I do, is try to make small and simple methods that I can see on a single page in my editor (visual studio).</p>\n\n<p>I've learnt from experience that making code simple makes it easier for the compiler to optimise it. The larger the method, the harder the compiler has to work!</p>\n\n<p>I've also recently seen a problem where large methods have caused a memory leak. Basically I had a loop very much like the following:</p>\n\n<pre>\nwhile (true)\n{\n  var smallObject = WaitForSomethingToTurnUp();\n  var largeObject = DoSomethingWithSmallObject();\n}\n</pre>\n\n<p>I was finding that my application was keeping a large amount of data in memory because even though 'largeObject' wasn't in scope until smallObject returned something, the garbage collector could still see it.</p>\n\n<p>I easily solved this by moving the 'DoSomethingWithSmallObject()' and other associated code to another method.</p>\n\n<p>Also, if you make small methods, your reuse within a class will become significantly higher. I generally try to make sure that none of my methods look like any others!</p>\n\n<p>Hope this helps.</p>\n\n<p>Nick</p>\n"}, {'answer_id': 60196, 'author': 'callingshotgun', 'author_id': 2876, 'author_profile': 'https://Stackoverflow.com/users/2876', 'pm_score': 1, 'selected': False, 'text': '<p>"cut and paste with minor tweaks here and there" is the kind of code repetition I usually solve with an entirely non-exotic approach-  Take the similar chunk of code, extract it out to a seperate method.  The little bit that is different in every instance of that block of code, change that to a parameter.</p>\n\n<p>There\'s also some easy techniques for removing repetitive-looking if/else if and switch blocks, courtesy of Scott Hanselman:\n<a href="http://www.hanselman.com/blog/CategoryView.aspx?category=Source+Code&amp;page=2" rel="nofollow noreferrer">http://www.hanselman.com/blog/CategoryView.aspx?category=Source+Code&amp;page=2</a></p>\n'}, {'answer_id': 60234, 'author': 'Ant', 'author_id': 3709, 'author_profile': 'https://Stackoverflow.com/users/3709', 'pm_score': 1, 'selected': False, 'text': '<p>I might go something like this:</p>\n\n<p>Create custom (private) types for data structures and put all the related logic in there. Dictionary&lt;string, List&lt;int&gt;&gt; etc.</p>\n\n<p>Make inner functions or properties that guarantee behaviour. If you’re continually checking conditions from a publically accessible property then create an private getter method with all of the checking baked in.</p>\n\n<p>Split methods apart that have too much going on. If you can’t put something succinct into the  or give it a good name, then start breaking the function apart until the code is (even if these “child” functions aren’t used anywhere else). </p>\n\n<p>If all else fails, slap a [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] on it and comment why.</p>\n'}, {'answer_id': 60252, 'author': 'airportyh', 'author_id': 5304, 'author_profile': 'https://Stackoverflow.com/users/5304', 'pm_score': 2, 'selected': False, 'text': "<p>I like to start refactoring when I need to, rather than the first opportunity that I get. You might say this is somewhat of an agile approach to refactoring. When do I feel I need to? Usually when I feel that the ugly parts of my codes are starting to spread. I think ugliness is okay as long as they are contained, but the moment when they start having the urge to spread, that's when you need to take care of business. </p>\n\n<p>The techniques you use for refactoring should start with the simplest. I would strongly recommand Martin Fowler's book. Combining common code into functions, removing unneeded variables, and other simple techniques gets you a lot of mileage. For list operations, I prefer using functional programming idioms. That is to say, I use internal iterators, map, filter and reduce(in python speak, there are corresponding things in ruby, lisp and haskell) whenever I can, this makes code a lot shorter and more self-contained.</p>\n"}, {'answer_id': 60272, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': '<p>Sometimes by the time you "complete functionality" using copy and paste code, you\'ve come to a point that it is maimed and mangled enough that any attempt at refactoring will actually take much, much longer than refactoring it at the point where it was obvious.</p>\n\n<p>In my personal experience my favorite "way of removing repetition" has been the "Extract Method" functionality of Resharper (although this is also available in vanilla Visual Studio).</p>\n\n<p>Many times I would see repeated code (some legacy app I\'m maintaining) not as whole methods but in chunks within completely separate methods. That gives a perfect opportunity to turn those chunks into methods.</p>\n\n<p>Monster classes also tend to reveal that they contain more than one functionality. That in turn becomes an opportunity to separate each distinct functionality into its own (hopefully smaller) class.</p>\n\n<p>I have to reiterate that doing all of these is not a pleasurable experience at all (for me), so I really would rather do it right while it\'s a small ball of mud, rather than let the big ball of mud roll and then try to fix that.</p>\n'}]
['https://Stackoverflow.com/questions/60100', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1122/']