When to consolidate duplicate code

We use the static code analysis tool (Sonar) to detect duplicate code in our project. It seems that there are many duplicate instances in the code, but most of them are less than 10 lines and occur only once. In the “opinion” of the Stackoverflow community, where should you draw a line when consolidating code duplication? For example, if it should be> = 10 rows for individual duplicates, or you should consider the total number of duplicate rows, for example. consider> 10 duplicates that occur more than once. For context, the programming languages ​​I'm looking at are Java and ActionScript. I understand that this question may not come to a final answer, but some clear instructions on this issue can save me a lot of time refactoring the code (or increase the time :)

+3
source share
4 answers

Consolidate the code if it is designed to do the same, and if you want all changes to apply to one duplicate to apply to another duplicate. You may not always want this. You can have two codes that do almost the same thing, but fill out completely different goals to meet different requirements. Requirements fluctuate, and soon you will find that the section that you combined cannot keep up with the shifts occurring in different directions (a situation that I encountered quite often when I overdid it).

, : , . . 1000 . , , , , , , . , .

Greetz
back2dos

+2

, , . , . , - " ", , .

+5

: .

:. , , - , , . , , , . , , , .

, - , . , . , , ( 10 ), . , , , .

+2

Consolidate your code (especially small blocks like the 10 lines you mention) only if it has a common purpose. If the code snippets really perform different tasks (but the implementation can be the same), then there is no benefit from the project point of view of their consolidation, and you may have to separate them later if you do.

+2
source

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


All Articles