How much duplicated code do you tolerate?

In a recent code review, I noticed several lines of duplicate logic in a class (less than 15 lines). When I suggested that the author reorganize the code, he argued that the code was easier to understand in this way. After reading the code again, I have to agree that extracting duplicate logic will slightly degrade readability.

I know that DRY is a guide, not an absolute rule. But overall, are you ready to hurt readability on behalf of DRY?

+44
coding-style dry refactoring code-duplication
Feb 19 '10 at 17:20
source share
13 answers

Refactoring: Improving the Design of Existing Code

Rule of three

The first time you do something, you just do it. The second time you do something like this, you startle when duplicating, but you duplicate
subject anyway. The third time you do something like this, you are refactoring.

Three hits and refactoring.




Encoders at work

Seibel: So, for each of these XII calls, you write an implementation.
Have you ever found that you have accumulated many bits of very similar code?

Zawinski: Oh, yes, definitely. Usually the second or third time you cut and pasted
he likes this piece of code, well, it's time to stop cutting and gluing and the routine.

+53
Feb 19 2018-10-19
source share

I put up with nothing. I may end up having some due to time constraints or something else. But I still have not found a case where duplicate code is really justified.

Saying that this will impair readability, we can only assume that you are typing names poorly :-)

+37
Feb 19 '10 at 17:33
source share

Personally, I prefer the code to be understandable in the first place.

DRY is a service facilitation in code. Creating code that is less clear for deleting duplicate code in many cases leads to greater maintainability than repeating lines of code.

That being said, I agree that DRY is a good target when necessary.

+25
Feb 19 '10 at 17:22
source share

If this code has a clear goal or support for P technical support, you should usually reorganize it. Otherwise, you will have a classic problem with cloned code: in the end, you will find that you need to change the code that supports P, and you will not find all the clones that implement it.

Some people suggest that 3 or more instances be a threshold for refactoring. I believe that if you have two of them, you should do it; finding another clone [or even knowing that they can exist] in a large system is difficult, whether you have two or three or more.

This answer is now provided in the context of the absence of any clone search tools. If you can reliably find clones, then the original reason for refactoring (avoiding maintenance errors) is less persistent (the usefulness of having a named abstraction is still real). What you really want is a way to find and track clones; abstracting them is one way to ensure that you can “find” them (making the search trivial).

A tool that can reliably find clones, at least, can prevent you from avoiding error cloning service errors. One such tool (I am the author) is CloneDR . CloneDR finds clones using the langauge target structure as a guide, and thus finds clones regardless of layout spaces, changes to comments, renamed variables, etc. (It is implemented for a number of languages, including C, C ++, Java, C #, COBOL and PHP). CloneDR will find clones in large systems without giving any indication. Shown are the revealed clones, as well as an anti-malware, which is essentially an abstraction that you could write instead. Versions of it (for COBOL) are now integrated with Eclipse and show you when you edit inside the clone in the buffer, as well as where other clones are located, so you can check / review others while you are there. (One thing you can do is reorganize them :).

I used to think that cloning was simply a mistake, but people do it because they don’t know how the clone will differ from the original, and therefore the final abstraction is not clear at the moment the cloning action occurs. Now I believe that cloning is good if you can track the clones and try to reorganize after the abstraction becomes clear.

+11
Feb 19 '10 at 17:25
source share

As soon as you repeat something , you create several places for making changes, if you find that you have made a mistake, you need to expand it, edit, delete, or any other dozens of other reasons that you may encounter with this, change the situation.

In most languages, extracting a block using the named method rarely can damage your readability.

This is your code, with your standards, but my main answer to your question is "how much?" no...

+8
Feb 19 '10 at 17:53
source share

You didn’t say which language, but in most IDEs this is a simple Refactor → Extract method. How much simpler it is, and one method with some arguments is much more convenient than two blocks of duplicated code.

+3
Feb 19 '10 at 17:24
source share

It is very difficult to say abstractly. But, in my opinion, even one line of duplicated code should be turned into a function. Of course, I do not always achieve this high standard on my own.

+3
Feb 19 '10 at 17:26
source share

The DRY point is maintainability. If the code is harder to understand, it is harder to maintain, so if refactoring hurts readability, you may not be able to fulfill the DRY goal. For less than 15 lines of code, I would be inclined to agree with your classmate.

+2
Feb 19 '10 at 17:24
source share

Refactoring can be difficult, and it depends on the language. All languages ​​have limitations, and sometimes a reorganized version of duplicate logic can be linguistically more complex than repeating code.

Often, duplication of LOGIC code occurs when two objects with different base classes have similarities in how they work. For example, two GUI components display values, but do not implement a common interface for accessing this value. Refactoring of such a system requires either methods containing more general objects than necessary, followed by label verification and casting, otherwise the class hierarchy should be rethought and restructured.

This situation is different from the fact that the code was exactly duplicated. I would not create a new interface class if I only assumed that it would be used twice, and both times within the same function.

+2
Feb 19 '10 at 22:55
source share

In general, no. In any case, for readability. There is always a way to reorganize duplicate code into an intention that reveals a general method that reads like a book, IMO.

If you want to make the DRY violation argument to avoid dependency injection, this can lead to more weight, and you can get Ayende's opinion with confidence, as well as code to illustrate the point here .

If your developer is actually Ayende, although I would hold DRY firmly and gain readability using intent-detection methods.

VN

+1
Feb 19 '10 at 17:35
source share

I do not accept duplicate code. If something is used in several places, it will be part of the structure, or at least a useful library.

The best line of code is a line of code not written.

+1
Jul 06 '10 at 8:29
source share

In fact, it depends on many factors, on what code is used, readability, etc. In this case, if there is only one copy of the code, and it is easier to read this way, then perhaps this is normal. But if you need to use the same code in third place, I would seriously think about reorganizing it into a general function.

0
Feb 19 '10 at 17:25
source share

Readability is one of the most important things that code can have, and I don’t want to compromise. Duplicated code is a bad smell, not a deadly sin.

As they say, there are problems.

If this code should be the same and not match, then there is a risk of maintainability. I would have comments in each place pointing to another, and if it were necessary in third place, I would reorganize it. (I really have such code in two different programs that do not have the corresponding code files, so the comments in each program point to another.)

You did not say that the lines form a single whole, performing some function that you can easily describe. If they do, reorganize them. This is unlikely to be the case, since you agree that the code is more readable, built-in in two places. However, you can look for more or less similarities and perhaps consider a function that simplifies the code. Just because a dozen lines of code are repeated does not mean that a function should consist of that dozen lines or more.

0
Feb 19 '10 at 17:33
source share



All Articles