读书人

怎么重构如上代码

发布时间: 2012-08-19 21:09:48 作者: rapoo

如何重构如下代码
看到源代码里有如下程序段,感觉看着不舒服,但又不知道该如何重构,求助各位:

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……

读书人网 >J2SE开发

热点推荐