gpt4 book ai didi

java - 降低圈复杂度的最佳方法

转载 作者:行者123 更新时间:2023-12-02 04:55:06 24 4
gpt4 key购买 nike

下面的方法的圈复杂度为 13,我遵循一些使用三元运算符进行小 if else 的方法,但对于长代码,最好的方法是什么使用什么设计模式来恢复 if else 条件或任何其他方式来降低圈复杂度,如 sonarQube 显示圈复杂度错误。我是新手,请帮助我理解整个过程

  @RequestMapping(value = "/submitUser", method=RequestMethod.POST)
public String submitUser(@ModelAttribute User userBean,Locale locale, RedirectAttributes redirectAttributes, HttpSession session, Model model) {
boolean status = false, isAdd = false;
String imagePath = "", task = "";
isAdd = userBean.getAdd();
/*if(isAdd) {
task = "add";
} else { // replace this by task=isAdd?"add":"edit";
task = "edit"; //
}*/
GlobalLogger.logApplicationDebugLog("Received request to " + task + " user ", LOGGER);
if (session != null && session.getAttribute("UserImagePath") != null) {
imagePath = session.getAttribute("UserImagePath").toString();
}
int currentUserId = Integer.parseInt(session.getAttribute(SessionKeyConstants.USER_ID).toString());
try {
status = iuser.submitUser(userBean, imagePath,currentUserId);
if(isAdd) {
if (status) {
GlobalLogger.logInfoLog("User "+userBean.getUserName()+" has been " + task + "ed successfully", LOGGER);
redirectAttributes.addFlashAttribute("successMsg", messageSource.getMessage("operationMsg.addUserSuccess", new String[] {userBean.getEmpEmail()}, locale));
return "redirect:/users.action";
} else {
GlobalLogger.logApplicationDebugLog("Error in adding user "+userBean.getUserName(), LOGGER);
redirectAttributes.addFlashAttribute("errorMsg", messageSource.getMessage("operationMsg.addUserFailure", new String[] {}, locale));
redirectAttributes.addFlashAttribute("isAdd", isAdd);
redirectAttributes.addFlashAttribute("userBean", userBean);
return "redirect:/addUser.action?isAdd=true";
}
} else {
if (status) {
GlobalLogger.logInfoLog("User "+userBean.getUserName()+" has been " + task + "ed successfully", LOGGER);
redirectAttributes.addFlashAttribute("successMsg", messageSource.getMessage("label.addSuccessMsg", new String[] {userBean.getFirstName()}, locale));
return "redirect:/users.action";
} else {
GlobalLogger.logApplicationDebugLog("Error in adding user "+userBean.getUserName(), LOGGER);
redirectAttributes.addFlashAttribute("errorMsg", messageSource.getMessage("label.addFailedMsg", new String[] {userBean.getFirstName()}, locale));
redirectAttributes.addFlashAttribute("isAdd", isAdd);
redirectAttributes.addFlashAttribute("userBean", userBean);
return "redirect:/addUser.action?isAdd=false";
}
}
} catch (UserException e) {
GlobalLogger.logApplicationDebugLog("User Name or Employee Id exists for user "+userBean.getUserName(), LOGGER);
redirectAttributes.addFlashAttribute("userBean", userBean);
redirectAttributes.addFlashAttribute("errorMsg", messageSource.getMessage(e.getErrorcode(), new String[] {}, locale));
return "redirect:/addUser.action?isAdd=true";
}
}

最佳答案

解决方法过长问题的最直接方法是将它们分解成碎片。将方法的各个部分提取到从原始方法调用的单独方法中。通常可以使用 IDE 自动完成,例如Eclipse(菜单重构/提取方法)。

在您的方法中,isAdd==true 和 false 的代码几乎相同。您可以将此代码提取到单独的方法中,例如。 String redirectUser(boolean isAdd, boolean status, <other attribs>) {...} 。这样你的方法就会简单得多。

第一个更改很简单 - 提取方法(这可以自动完成):

    @RequestMapping(value = "/submitUser", method=RequestMethod.POST)
public String submitUser(@ModelAttribute User userBean,Locale locale, RedirectAttributes redirectAttributes, HttpSession session, Model model) {
boolean status = false, isAdd = false;
String imagePath = "", task = "";
isAdd = userBean.getAdd();
GlobalLogger.logApplicationDebugLog("Received request to " + task + " user ", LOGGER);
if (session != null && session.getAttribute("UserImagePath") != null) {
imagePath = session.getAttribute("UserImagePath").toString();
}
int currentUserId = Integer.parseInt(session.getAttribute(SessionKeyConstants.USER_ID).toString());
try {
status = iuser.submitUser(userBean, imagePath,currentUserId);
return redirectUser(userBean, locale, redirectAttributes, status,
isAdd, task);
} catch (UserException e) {
GlobalLogger.logApplicationDebugLog("User Name or Employee Id exists for user "+userBean.getUserName(), LOGGER);
redirectAttributes.addFlashAttribute("userBean", userBean);
redirectAttributes.addFlashAttribute("errorMsg", messageSource.getMessage(e.getErrorcode(), new String[] {}, locale));
return "redirect:/addUser.action?isAdd=true";
}
}

private String redirectUser(User userBean, Locale locale,
RedirectAttributes redirectAttributes, boolean status,
boolean isAdd, String task) {
if(isAdd) {
if (status) {
GlobalLogger.logInfoLog("User "+userBean.getUserName()+" has been " + task + "ed successfully", LOGGER);
redirectAttributes.addFlashAttribute("successMsg", messageSource.getMessage("operationMsg.addUserSuccess", new String[] {userBean.getEmpEmail()}, locale));
return "redirect:/users.action";
} else {
GlobalLogger.logApplicationDebugLog("Error in adding user "+userBean.getUserName(), LOGGER);
redirectAttributes.addFlashAttribute("errorMsg", messageSource.getMessage("operationMsg.addUserFailure", new String[] {}, locale));
redirectAttributes.addFlashAttribute("isAdd", isAdd);
redirectAttributes.addFlashAttribute("userBean", userBean);
return "redirect:/addUser.action?isAdd=true";
}
} else {
if (status) {
GlobalLogger.logInfoLog("User "+userBean.getUserName()+" has been " + task + "ed successfully", LOGGER);
redirectAttributes.addFlashAttribute("successMsg", messageSource.getMessage("label.addSuccessMsg", new String[] {userBean.getFirstName()}, locale));
return "redirect:/users.action";
} else {
GlobalLogger.logApplicationDebugLog("Error in adding user "+userBean.getUserName(), LOGGER);
redirectAttributes.addFlashAttribute("errorMsg", messageSource.getMessage("label.addFailedMsg", new String[] {userBean.getFirstName()}, locale));
redirectAttributes.addFlashAttribute("isAdd", isAdd);
redirectAttributes.addFlashAttribute("userBean", userBean);
return "redirect:/addUser.action?isAdd=false";
}
}
}

现在,您可以比较 if 的两种情况,因为它们非常相似。也许您可以重写它们以避免重复。例如,不要 return "redirect:/addUser.action?isAdd=true";你可以写 return "redirect:/addUser.action?isAdd="+ isAdd.

重构是一个迭代过程。您应该进行一些小的更改,运行单元测试以确保没有任何问题,然后重复直到结果令人满意。

关于java - 降低圈复杂度的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28848329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com