'Removing duplicate codes from if/else and try clause
Sonar has marked the below-mentioned code as duplicate, and I am not able to understand how to refactor the code as the fragments of the code exist in the if/else and try clause.
First Method:
private void createGenericCar(List<Result> results, GenericCar genericCarNode) {
if (genericCarNodeId == null || genericCarNodeId.isEmpty()) {
results.add(new Result.Builder().state(State.ERROR).errorMessage(#########).build());
} else {
List<Object> logAttributes = GenericCarDataHelper.getGenericCarLogAttribute(genericCarNodeId);
var transactionManager = new TransactionManager();
transactionManager.createTransactionContext();
try {
genericCarPersistenceService.validateGenericCarAttributes(genericCarNode);
NodeNamesInfo names = genericCarDecorator.getNodeNameInfo(genericCarNode);
transactionManager.startTransaction();
transactionResults = new ArrayList<>();
String nodeInternalName = names.getInternalName();
String parentPartTypeInternalName = Constants.Types.NODE_yyy_INTERNAL_NAME;
String parent = genericCarNode.getParent();
var parentInternalName = "";
if (parent.equals(Constants.Classification.GENERIC_CAR_NODE_GUID)) {
parentInternalName = Constants.Classification.GENERIC_CAR_FIRST_NODE;
parentPartTypeInternalName = Constants.Types.ROOT_INTERNAL_NAME;
} else if (!parent.isEmpty()) {
parentInternalName = Constants.Classification.GENERIC_CAR_INTERNAL_NAME_PREFIX + parent;
}
$
$
$(not duplicated code)
$
$
} catch(###) {
}
}
}
Second method:
private void updateGenericCar(List<Result> results, GenericCar genericCarNode) {
if (genericCarNodeId == null || genericCarNodeId.isEmpty()) {
results.add(new Result.Builder().state(State.ERROR).errorMessage(#########).build());
} else {
List<Object> logAttributes = GenericCarDataHelper.getGenericCarLogAttribute(genericCarNodeId);
var transactionManager = new TransactionManager();
transactionManager.createTransactionContext();
try {
genericCarPersistenceService.validateGenericCarAttributes(genericCarNode);
NodeNamesInfo names = genericCarDecorator.getNodeNameInfo(genericCarNode);
transactionManager.startTransaction();
transactionResults = new ArrayList<>();
String partTypeInternalName = Constants.Types.NODE_yyy_INTERNAL_NAME;
String parent = genericCarNode.getParent();
String parentInternalName = GenericCarDataHelper.getGenericCarParentInternalName(parent);
String nodeHierachy = classificationService.getNodeHierarchy(names.getInternalName());
var updateNodeResult = classificationService.updateNode(names.getInternalName(), names.getDisplayNameEn(), names.getDisplayNameDe(), Collections.emptyMap(), parentInternalName);
$
$
$(not duplicated code)
$
$
} catch(###) {
}
}
}
Thanks for the help in advance!
Solution 1:[1]
Since the method is duplicate, I would suggest you to either refactor it and create one private method and use it from different places in your class, or if method is being used from multiple classes then I would suggest you to create a Utility class and move the method in Utility class and use it from as many places you want.
This way sonar wont trouble you. Sample code below.
public class MyUtility {
public static someUniqueCodeBlock() {
List<Object> logAttributes = GenericCarDataHelper.getGenericCarLogAttribute(genericCarNodeId);
var transactionManager = new TransactionManager();
transactionManager.createTransactionContext();
try {
genericCarPersistenceService.validateGenericCarAttributes(genericCarNode);
NodeNamesInfo names = genericCarDecorator.getNodeNameInfo(genericCarNode);
transactionManager.startTransaction();
transactionResults = new ArrayList<>();
String partTypeInternalName = Constants.Types.NODE_yyy_INTERNAL_NAME;
String parent = genericCarNode.getParent();
String parentInternalName = GenericCarDataHelper.getGenericCarParentInternalName(parent);
String nodeHierachy = classificationService.getNodeHierarchy(names.getInternalName());
var updateNodeResult = classificationService.updateNode(names.getInternalName(), names.getDisplayNameEn(), names.getDisplayNameDe(), Collections.emptyMap(), parentInternalName);
} catch(###) {
}
}
class Caller {
private void createGenericCar(List<Result> results, GenericCar genericCarNode) {
if (genericCarNodeId == null || genericCarNodeId.isEmpty()) {
results.add(new Result.Builder().state(State.ERROR).errorMessage(#########).build());
} else {
MyUtility.someUniqueCodeBlock();
$
$
$(not duplicated code)
$
$
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
