首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

怎么重构如上代码

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

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

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

热点排行