In C # (or any language), what is your favorite way to remove repetitions?

I just coded a class of 700 lines. Awful. I am ashamed of my head. This is the opposite of DRY like the British summer.

It is full of cut and paste with slight modifications here and there. This makes him a prime candidate for refactoring. Before I get down to this, I thought I would ask, when you have a lot of repetitions, what are the first refactoring options you are looking for?

For the record, my probably use:

  • General classes and methods
  • Overload / chaining methods.

What are yours?

+4
source share
7 answers

I like to start refactoring when I need it, and not the first opportunity I get. You could say that this is a somewhat moving approach to refactoring. When do I feel what I need? Usually when I feel that the ugly parts of my codes begin to spread. I think that ugliness is in order as long as they are contained, but at the moment when they have a desire to spread, this is when you need to do business.

The methods used for refactoring should begin with the simplest. I would highly recommend Martin Fowler's book. Combining common code with functions, removing unnecessary variables and other simple methods, you get a lot of mileage. For list operations, I prefer to use the idioms of functional programming. That is, I use internal iterators, map, filter and reduce (in python they say there are corresponding things in ruby, lisp and haskell), when possible, this makes the code much shorter and more autonomous.

+4
source

#region

I made 1000 class lines with only one line!

In all seriousness, the best way to avoid repetition is that which is included in your list, and that you can fully use polymorphism, study your class and find out what is best done in the base class, and how various components can subclass it.

+2
source

Sometimes, when you "complete functionality" using copy and paste code, you come to the conclusion that it is crippled and crippled so much that any refactoring attempt will actually take a lot of time, much longer than refactoring at the point where it was obvious.

In my personal experience, my favorite “repetition removal method” was the “Extract Method” Resharper functionality (although this is also available in Visual Studio Visual Studio).

Many times I saw repeating code (some outdated application that I support) not as whole methods, but in pieces within separate methods. This provides an excellent opportunity to turn these pieces into methods.

Monster classes also show that they contain more than one functionality. This, in turn, becomes an opportunity to split each individual functionality into its own (hopefully smaller) class.

I have to repeat that doing all this is not a pleasant experience at all (for me), so I really would rather do it right while it is a small ball of dirt, instead of letting a large ball of dirt and then try to fix it.

+2
source

First of all, I would recommend refactoring much earlier than when you are done with the first version of the class. Anytime you see duplication, eliminate it as soon as possible. This may take a little longer, but I think the results will end up being much cleaner and that helps you rethink your code when you go to make sure that you are doing everything right.

As for my favorite way to remove duplication ... Closing, especially in my favorite language (Ruby). They are usually a very concise way of taking two pieces of code and merging the similarities. Of course (like any "best practice" or hint), this cannot be done blindly ... I just find them very funny when I can use them.

+1
source

One of the things I do is try to make small and simple methods that I can see on one page in my editor (visual studio).

From experience, I have seen that simple code generation makes the compiler easier to optimize. The larger this method, the harder the compiler!

I also recently saw a problem when large methods caused a memory leak. I basically had a loop very similar to the following:

  while (true)
 {
   var smallObject = WaitForSomethingToTurnUp ();
   var largeObject = DoSomethingWithSmallObject ();
 }

I found that my application stores a large amount of data in memory, because although "largeObject" was not in the area until smallObject returned anything, the garbage collector could still see it.

I easily solved this by moving "DoSomethingWithSmallObject ()" and other related code to another method.

In addition, if you make small methods, your reuse within the class will become much higher. I usually try to make sure that none of my methods are like the others!

Hope this helps.

Nick

+1
source

"cut and paste with little tricks here and there" is a repetition of code that I usually solve with a completely non-exotic approach. Take a similar piece of code, extract it to a separate method. The small bit that differs in each instance of this block of code, change it to a parameter.

There are also some simple methods for removing duplicate if / else if and switch blocks kindly provided by Scott Hanselman: http://www.hanselman.com/blog/CategoryView.aspx?category=Source+Code&page=2

+1
source

I could go something like this:

Create custom (private) types for data structures and place all the logic associated with them there. Dictionary <string, List <int →>, etc.

Make internal functions or properties that guarantee behavior. If you constantly check conditions from a public property, create a private getter method with all the checks baked into.

Separate methods that go on too much. If you cannot put something compressed or give it a good name, then start breaking the function until the code is there (even if these "child" functions are used elsewhere).

If all else fails, apply [SuppressMessage ("Microsoft.Maintainability", "CA1502: AvoidExcessiveComplexity")] and comment on why.

+1
source

Source: https://habr.com/ru/post/1276674/


All Articles