![]() ![]() Note: -files is deprecated since PMD 6.52.0. Required List of files and directories to process. Required The minimum token length which should be reported as a duplicate. Novice as much as advanced readers may want to read on on Refactoring Guru for more in-depth strategies, use cases and explanations. Introduce a common ancestor to those class hierarchies.If the duplication occurs consistently in unrelated hierarchies:.Extract a method and pull it up in the class hierarchy, along with common fields.If the duplication occurs in siblings within a class hierarchy:.Extract the duplicated logic into a private method.Extract a local variable if the duplicated logic is not prohibitively long.If the duplication is local to a method or single class:.Once you have located some duplicates, several refactoring strategies may apply depending of the scope and extent of the duplication. We thus advise developers to use CPD to help remove duplicates, not to help keep duplicates in sync. If the duplicates are already there, then they should be refactored out. ![]() Otherwise, the most viable solution is to not duplicate. Now, if the code may never change in the future, then this is not a problem. Segments of code initially supposed to do the same thing may grow apart undetected upon further refactoring. This means the task of finding duplicates to keep them in sync when doing subsequent refactorings can no longer be entrusted to an automated tool – adding more burden on the maintainer. However, failure to keep the code in sync may mean automated tools will no longer recognise these blocks as duplicates. Automated tools like CPD can help with that to some extent. It’s certainly important to know where to get CPD, and how to call it, but it’s worth stepping back for a moment and asking yourself why you should care about this, being the occurrence of duplicate code blocks.Īssuming duplicated blocks of code are supposed to do the same thing, any refactoring, even simple, must be duplicated too – which is unrewarding grunt work, and puts pressure on the developer to find every place in which to perform the refactoring. It can also be run with Maven by using the cpd-check goal on the Maven PMD Plugin. ![]() It can be used via command-line, or via an Ant task. Duplicate code can be hard to find, especially in a large project.īut PMD’s Copy/Paste Detector (CPD) can find it for you!ĬPD works with Java, JSP, C/C++, C#, Go, Kotlin, Ruby, Swift and many more languages. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |