Contents
What to do if you find the same code in two methods?
If the same code is found in two or more methods in the same class: use Extract Method and place calls for the new method in both places. If the same code is found in two subclasses of the same level: Use Extract Method for both classes, followed by Pull Up Field for the fields used in the method that you’re pulling up.
Is there a tool to determine code similarity?
What you really want to do is see if there is code cloned (copied) across the two projects (both projects consisting of possibly large sets of files). You can do this by running a clone detection tool. Wikipedia lists a variety of them.
What to do when you find duplicate code in two subclasses?
If the same code is found in two subclasses of the same level: Use Extract Method for both classes, followed by Pull Up Field for the fields used in the method that you’re pulling up. If the duplicate code is inside a constructor, use Pull Up Constructor Body.
How to create multiple methods in a class in Java?
As discussed in Behavior Of Java Classes Using Methods we can create methods in a class. This program shows how we can create multiple methods in a class and how we can call one method from another method. Here we have create a Student class with member variables name, english, telugu, hindi, maths, science and social.
Can a class have more than one compareTo method?
You can only have one compareTo () method in your class. If you want to sort the same class more than one way, create Comparator implementations.
How to create different comparison classes in Java?
If subclassing doesn’t make sense, you need to create different comparison classes. In Java, you often use a Comparator for comparisons. Create several (or create a configurable comparator): IsbnComparator, AuthorComparator, etc.
What to do when you find duplicate code in two classes?
If duplicate code is found in two different classes: If the classes aren’t part of a hierarchy, use Extract Superclass in order to create a single superclass for these classes that maintains all the previous functionality. If it’s difficult or impossible to create a superclass, use Extract Class in one class and use the new component in the other.