删除 if/else 和 try 子句中的重复代码
Sonar 已将下面提到的代码标记为重复,并且我无法理解如何重构代码,因为代码片段存在于 if/else 和 try 子句中。
第一种方法:
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(###) {
}
}
}
第二种方法:
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(###) {
}
}
}
提前感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于该方法是重复的,我建议您重构它并创建一个私有方法并从类中的不同位置使用它,或者如果从多个类使用该方法,那么我建议您创建一个实用程序类并移动Utility 类中的方法,并从任意多个地方使用它。
这样声纳就不会麻烦你了。下面的示例代码。
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.