I have the following code:
private Facility updateFacility(Facility newFacility, Facility oldFacility) {
if (newFacility.getCity() != null)
oldFacility.setCity(newFacility.getCity());
if (newFacility.getContactEmail() != null)
oldFacility.setContactEmail(newFacility.getContactEmail());
if (newFacility.getContactFax() != null)
oldFacility.setContactFax(newFacility.getContactFax());
if (newFacility.getContactName() != null)
oldFacility.setContactName(newFacility.getContactName());
// ......
}
There are around 14 such checks and assignments. That is, except for a few, I need to modify all the fields of the oldFacility object. I'm getting a cyclomatic complexity of this code 14, which is "greater than 10 authorized" as per SonarQube. Any ideas upon how to reduce the cyclomatic complexity?
You could use Java Reflection for avoiding that copy/paste/write-same-Problem:
Now you can simple change the properties[] array when there are new properties in the Facility class which you want to update that way.
EDIT: If you use the return type of the getter method to find the setter method, it is not neccessary to assume that the properties of Facility are all of the same type.
CAVEATS: Be careful in method renaming! This code will lead to runtime errors if you rename or remove methods from the Facility class. If you have to possibility to change the code of the Facility class, you should consider using an annotation to indicate which properties should be updated.
I think you can apply Builder Pattern to resolve the issue, it may help you remove the frustration in the loop of if statement. Please see this link for more detials
The not null check seems pointless to me since the
NullPointerException
won't be thrown if you slightly modify your example like this:This will assign null values to references which were referencing to nulls anyway and will not cause any issues.
Assuming you were doing something like
newFacility.getCity().toString()
then the checks would be useful.You can override hashCode and equals methods in Facility class and do as follows:
NOTE : You can include all params or only those which decide the uniqueness. Its up to you.
Hope this helps!
You could copy the fields for the
oldFacility
object that you don't want to modify to some other variables, then update the wholeoldFacility
object, and just replace the fields that you didn't want to change with the content stored in the other variables. i.e.So copy the data that you want to keep out of
oldFacility
first, then substituteoldFacility
fornewFacility
, and replace the required attributes ofnewFacility
with the data fromoldFacility
.At some point in your program, you will have to implement the logic:
Without having a global look at your project, what you can do is to move that logic inside the setters of each property:
This way, your
update
method would simply be: