Java – when to merge code duplicates
We use the code static analysis tool (sonar) to detect duplicate code in the project It seems that there are many repeated instances in the code, but most of them are less than 10 lines and appear only once In the "comments" of the stackoverflow community, where should you draw the boundaries of merging code duplication? For example, for a single copy, should it be > = 10 rows, or should we consider the number of duplicate rows, for example Consider > 10 repetitions occurring more than once
Solution
If you want to do the same, and if you want to apply all changes applied to one copy to other duplicates, merge the codes You may not always want this to happen You may have two pieces of code that are almost identical, but achieve completely different purposes to meet different requirements Demand fluctuates, and soon you will find that your unified part can't keep up with changes in different directions (I often encounter this situation when overused)
So the rule is simple: merge code that does the same thing No matter how long You don't want to make the same changes in 1000 places Don't try to merge parts of the code. Unless you find that they have enough in common, they may be refactored into a useful subroutine Also remember that performance - critical code is not the best place for best practices