public void method() {
returnValue = optional.absent();
try {
GetDate dateResponse = client.call();
if (dateResponse != null) {
myDate = dateResponse.getDate();
if (myDate != null) {
returnValue = convertDateToAnotherDate() //actual function
if (!returnValue.isPresent()) {
LOG.error("Missing required fields");
}
} else {
LOG.error("No myDate");
}
} else {
LOG.error("Service returned no dateResponse");
}
} catch (CustomException exception) {
LOG.error("Custom service failed");
}
return retVal;
}
0
A
回答
3
您所需要的,如果的,但你可以重新排列,以使其更清晰了很多的逻辑组织,遵循以下原则,如果:
- 失败早
- 减少筑巢
- 不声明变量,直到需要/最小化它们的范围
应用上述原则后:
public void method() {
try {
GetDate dateResponse = client.call();
if (dateResponse == null) {
LOG.error("Service returned no dateResponse");
return optional.absent();
}
myDate = dateResponse.getDate();
if (myDate == null) {
LOG.error("No myDate");
return optional.absent();
}
returnValue = convertDateToAnotherDate() //actual function
if (returnValue.isPresent())
return returnValue;
LOG.error("Missing required fields");
} catch (CustomException exception) {
LOG.error("Custom service failed");
}
return optional.absent();
}
注意现在的测试都是正面测试(使用==而不是!=,这是我们的小脑能理解得更好)。缩进(嵌套)减少,可读性提高。 returnValue变量只在代码中间需要,所以不需要提前声明。
+1
+1提到什么*我们的小脑袋*可以变得更好。 – devnull
0
0
鉴于你反应,如果这些值返回null
会发生什么的方式,也许是一个更好的想法抛出异常从调用方法,并记录异常。您也可以将您的dateResponse
更改为Optional<GetDate>
,并在缺席时适当运作。
考虑:
public Optional<Date> method() {
Optional<Date> returnValue = Optional.absent();
Optional<GetDate> dateResponse = client.call();
if (dateResponse.isPresent()) {
try {
Date myDate = dateResponse.getDate();
returnValue = convertDateToAnotherDate(date); //actual function
} catch (IllegalStateException e) {
LOG.error(e);
}
} else {
LOG.error("Service returned no dateResponse");
}
return returnValue;
}
我假设
client
将返回Optional<Date>
,如果它的存在,我们会表现正常。我执行正常的业务逻辑,好像
null
的威胁是不可能的。- 如果发生错误(我认为这是一个
IllegalStateException
),我期待我调用的方法抛出它。 - 我记录发生的异常,并在构造异常时提供有意义的消息。
的getDate
一个例子结构可以这样写:
public Date getDate() {
if(date == null) {
throw new IllegalStateException("No date present");
}
return date;
}
...现在我们到一个if
。
我真的不知道你的变量是什么类型(我的意思是,我真的不 - 我问这是否会编译,我有我的疑惑),所以这是关于我可以去与建议。
相关问题
- 1. 如何减少if语句的数量
- 2. 如何减少if语句?
- 3. 如何尽量减少与if语句
- 4. 关于在我的代码中加快/ if语句的建议?
- 5. 使代码通过减少if语句
- 6. 关于Haskell代码片段的建议?
- 7. 减少嵌套If-语句的数量
- 8. 如何减少PHP中if-else语句的数量?
- 9. 如何减少此代码?
- 10. 如何优化我的if语句或减少if语句?
- 11. 如何减少这里使用的“if”语句的数量?
- 12. 如何减少if-else语句
- 13. 如何减少if/else语句Javascript
- 14. 有关如何简化此代码的任何建议?
- 15. 关闭HTML代码中的一段代码,如if语句
- 16. 优化此代码的任何建议?
- 17. 在多个if语句中相同的操作,如何减少代码冗余
- 18. 如何减少此代码的冗余?
- 19. 关于如何阅读Symfony2源代码的任何建议?
- 20. 减少我的if-else语句中使用的变量数
- 21. 如何减少此代码重复
- 22. 如何减少if的数量
- 23. 重构PHP代码 - 减少foreach语句的数量
- 24. 如何减少if if语句并使其更好?
- 25. 如何在compilejava.net中使用此代码生成If else语句?
- 26. 如何将If If语句添加到代码段
- 27. Java:如何减少无法合并它们的if-else语句的数量
- 28. 如何通过减少“if”语句的数量来改进这个简单的代码?
- 29. 如何在此if/else语句中添加if/else语句?
- 30. 快速减少if语句的长度
请正确缩进您的代码。 – arshajii
这个问题真的没有太多,有没有......只是少量的代码......还有一些神奇的变量......而你的日志变量是不一致的。这个截断的例子甚至*编译*在你的最后? – Makoto
考虑让'client.call'和'GetDate.getDate'在失败时抛出异常(如果适用)?如果可以,我尝试*避免null值。 – user2246674