2013-09-26 24 views
0
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

请正确缩进您的代码。 – arshajii

+0

这个问题真的没有太多,有没有......只是少量的代码......还有一些神奇的变量......而你的日志变量是不一致的。这个截断的例子甚至*编译*在你的最后? – Makoto

+0

考虑让'client.call'和'GetDate.getDate'在失败时抛出异常(如果适用)?如果可以,我尝试*避免null值。 – user2246674

回答

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

您可以将它们组合起来,如:

if(dateResponse!=null && dateResponse.getDate() !=null) 
+0

对于'dateResponse == null && dateResponse.getDate()!= null'的情况(取决于您想要订购代码的方式),您仍然需要一个'if'。 –

+5

如果dateResponse为null,那么在world中dateResponse.getDate()是不是null?你只会得到一个空引用异常。 – Vulcronos

+0

这是java,所以它会短路 – Kevin

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

我真的不知道你的变量是什么类型(我的意思是,我真的不 - 我问这是否会编译,我有我的疑惑),所以这是关于我可以去与建议。