当前位置: 代码迷 >> J2SE >> 怎么重构如上代码
  详细解决方案

怎么重构如上代码

热度:61   发布时间:2016-04-24 00:32:20.0
如何重构如下代码
看到源代码里有如下程序段,感觉看着不舒服,但又不知道该如何重构,求助各位:
Java code
    if (entity != null                && entity.getDetail() != null                && entity.getDetail().getPrincipals() != null                && entity.getDetail().getPrincipals().size() >= 1) {                        PrincipalEntity principalEntity = (PrincipalEntity) entity.getDetail().getPrincipals().get(0);            if (principalEntity != null && principalEntity.getInfo() != null) {                return principalEntity.getInfo();            }        }


------解决方案--------------------
没法重构了,最多也是碰到不对就 return

Java code
if (entity == null) {    return;}if (entity.getDetail() == null) {    return;}if (entity.getDetail().getPrincipals() == null) {    return;}if (entity.getDetail().getPrincipals().size() < 1) {    return;}...
------解决方案--------------------
探讨

没法重构了,最多也是碰到不对就 return

Java code
if (entity == null) {
return;
}
if (entity.getDetail() == null) {
return;
}
if (entity.getDetail().getPrincipals() == null) {
return;
}
if (entity.get……
  相关解决方案