我是Lambda的新手,我想将具有很多if-else的简单代码转换为Lambda代码。 谁能解释我该怎么做?我不太了解如何构建它
public void registerModule(HttpServletRequest req, ModuleType moduleType) {
LOGGER.debug("Register New Module - " + moduleType.name());
ModuleEntityGenerator moduleEntityGenerator = new ModuleEntityGenerator();
try {
if (!req.getParts().isEmpty() && !req.getParameterMap().isEmpty()) {
ModuleEntityDao moduleEntityDao = moduleEntityGenerator.get(req, moduleType);
if (moduleEntityDao != null) {
if (processRegistryDal.getModule(moduleType, moduleEntityDao.getId()) == null) { // Check BA is not already exist
processRegistryDal.addNewModule(moduleEntityDao);
} else { // If already exists just update the current row
processRegistryDal.updateModule(moduleEntityDao);
}
} else {
LOGGER.error("The BA object is null. There is nothing to register");
}
} else {
LOGGER.error("The rest request is empty.No info to register");
}
} catch (IOException e) {
LOGGER.error("IO Error\n" + e.getMessage());
} catch (ServletException e) {
LOGGER.error("Servlet Error\n" + e.getMessage());
}
}
答案 0 :(得分:2)
这里与lambda无关,只是一个快速的清理提示。早期的return
是一种扁平化代码的好方法,可以使代码的表观复杂度更接近其实际复杂度。只需反转您的if
条件的含义,拉出错误日志,然后返回即可。看看这一转换可以使您的代码更好。
public void registerModule(HttpServletRequest req, ModuleType moduleType) {
LOGGER.debug("Register New Module - " + moduleType.name());
try {
if (req.getParts().isEmpty() || req.getParameterMap().isEmpty()) {
LOGGER.error("The rest request is empty.No info to register");
return;
}
ModuleEntityGenerator moduleEntityGenerator = new ModuleEntityGenerator();
ModuleEntityDao moduleEntityDao = moduleEntityGenerator.get(req, moduleType);
if (moduleEntityDao == null) {
LOGGER.error("The BA object is null. There is nothing to register");
return;
}
if (processRegistryDal.getModule(moduleType, moduleEntityDao.getId()) == null) { // Check BA is not already exist
processRegistryDal.addNewModule(moduleEntityDao);
} else { // If already exists just update the current row
processRegistryDal.updateModule(moduleEntityDao);
}
} catch (IOException e) {
LOGGER.error("IO Error\n" + e.getMessage());
} catch (ServletException e) {
LOGGER.error("Servlet Error\n" + e.getMessage());
}
}
答案 1 :(得分:1)
如果只是简单地重新组织代码,就不会得到太多嵌套的if
语句。
例如逆转条件。代替:
if (a) {
if (b) {
// code here
} else {
// ERROR
}
} else {
// ERROR
}
将其转过来并使用else-if
构造:
if (! a) {
// ERROR
} else if (! b) {
// ERROR
} else {
// code here
}
一个附带的好处是,短的“ ERROR”逻辑在if
条件旁边处理,而不是出现在更远的地方,因为条件和操作分开得太远了。
使用您的代码,可以像这样简化:
public void registerModule(HttpServletRequest req, ModuleType moduleType) {
LOGGER.debug("Register New Module - " + moduleType.name());
try {
ModuleEntityDao moduleEntityDao;
if (req.getParts().isEmpty() || req.getParameterMap().isEmpty()) {
LOGGER.error("The rest request is empty.No info to register");
} else if ((moduleEntityDao = new ModuleEntityGenerator().get(req, moduleType)) == null) {
LOGGER.error("The BA object is null. There is nothing to register");
} else if (processRegistryDal.getModule(moduleType, moduleEntityDao.getId()) == null) { // Check BA is not already exist
processRegistryDal.addNewModule(moduleEntityDao);
} else { // If already exists just update the current row
processRegistryDal.updateModule(moduleEntityDao);
}
} catch (IOException e) {
LOGGER.error("IO Error\n" + e.getMessage());
} catch (ServletException e) {
LOGGER.error("Servlet Error\n" + e.getMessage());
}
}
答案 2 :(得分:0)
如何将每个if
分成范围较小的单独方法。
主要方法看起来很简单。
public void registerModule(HttpServletRequest req, ModuleType moduleType) {
LOGGER.debug("Register New Module - " + moduleType.name());
try {
if (!isRequestEmpty(req))
addOrUpdateModule(createModule(req, moduleType));
} catch (IOException e) {
LOGGER.error("IO Error\n" + e.getMessage());
} catch (ServletException e) {
LOGGER.error("Servlet Error\n" + e.getMessage());
}
}
Helper方法。如果开发人员需要详细信息,请查看它。
private static boolean isRequestEmpty(HttpServletRequest req) {
if (req.getParts().isEmpty() || req.getParameterMap().isEmpty()) {
LOGGER.error("The rest request is empty.No info to register");
return true;
}
return false;
}
private static ModuleEntityDao createModule(HttpServletRequest req, ModuleType moduleType) {
ModuleEntityDao module = new ModuleEntityGenerator().get(req, moduleType);
if (module != null)
return module;
LOGGER.error("The BA object is null. There is nothing to register");
return module;
}
private void addOrUpdateModule(ModuleEntityDao module) {
if(module == null)
return;
if(processRegistryDal.getModule(module.getModuleType(), module.getId()) == null)
processRegistryDal.addNewModule(moduleEntityDao);
else
processRegistryDal.updateModule(moduleEntityDao);
}
答案 3 :(得分:0)
这不是lamda,但是可以通过调用“ return”在输入无效的情况下立即退出函数,从而使程序易于读取。
if (a != null && b != null) {
// c
}
else {
// d
}
到
if (a == null || b == null) {
// d
return;
}
// c
示例
public void registerModule(HttpServletRequest req, ModuleType moduleType) {
try {
LOGGER.debug("Register New Module - " + moduleType.name());
ModuleEntityGenerator moduleEntityGenerator = new ModuleEntityGenerator();
if (req.getParts().isEmpty() || req.getParameterMap().isEmpty()) {
LOGGER.error("The rest request is empty.No info to register");
return;
}
ModuleEntityDao moduleEntityDao = moduleEntityGenerator.get(req, moduleType);
if (moduleEntityDao == null) {
LOGGER.error("The BA object is null. There is nothing to register");
return;
}
if (processRegistryDal.getModule(moduleType, moduleEntityDao.getId()) == null) {
// Check BA is not already exist
processRegistryDal.addNewModule(moduleEntityDao);
return;
}
// If already exists just update the current row
processRegistryDal.updateModule(moduleEntityDao);
} catch (Exception ex) {
LOGGER.error("Error\n" + ex.getMessage());
}
}