您的位置:首页 > 编程语言

NO.56 代码优化初步 1.简化你的代码

2011-02-11 13:21 274 查看
--做为一个程序员,你要力求完美
--做为一个想完美的程序员,第一步,你写的代码要让别人看得舒服、看得明白而且快速——一眼就知道你想干嘛;保证以上几点,再想想还能不能再简化些?能4行代码实现的就不5行代码(当然你得遵守代码规范,你要是把一个左花括号移到上一行,这可不叫5行变4行)

在Ctrl+C,Ctrl+V的时候,考虑下代码能不能不那么长——把个性的地方剥离出来,代码则即简洁又清晰:
if ("C".equals(language)) {
buffer.append("  <b>菜单配置(").append(systemCode)
.append(")</b>");
buffer.append("   <img src="")
.append(contextPath)
.append("/common/images/btnAddChildMenu.gif" mce_src="")
.append(contextPath)
.append("/common/images/btnAddChildMenu.gif" name=btnAddChildMenu alt=添加子菜单 onclick=/"return postAction(fm,'")
.append(contextPath)
.append("/platform/processGgMenu.do?actionType=prepareInsert&ggMenuMenuID=0','MenuTreeRight')/">");
} else if ("E".equals(language)) {
buffer.append("  <b>菜單配置(").append(systemCode)
.append(")</b>");
buffer.append("   <img src="")
.append(contextPath)
.append("/common/images/btnAddChildMenu.gif" mce_src="")
.append(contextPath)
.append("/common/images/btnAddChildMenu.gif" name=btnAddChildMenu alt=添加子菜單 onclick=/"return postAction(fm,'")
.append(contextPath)
.append("/platform/processGgMenu.do?actionType=prepareInsert&ggMenuMenuID=0','MenuTreeRight')/">");
} else {
buffer.append("  <b>Config Menu(").append(systemCode)
.append(")</b>");
buffer.append("   <img src="")
.append(contextPath)
.append("/common/images/btnAddChildMenu.gif" mce_src="")
.append(contextPath)
.append("/common/images/btnAddChildMenu.gif" name=btnAddChildMenu alt=/"Add Sub Menu/" onclick=/"return postAction(fm,'")
.append(contextPath)
.append("/platform/processGgMenu.do?actionType=prepareInsert&ggMenuMenuID=0','MenuTreeRight')/">");
}

简单优化后
String name = "Config Menu";
String alt = "Add Sub Menu";
if ("C".equals(language)) {
name = "菜单配置";
alt = "添加子菜单";
} else if ("T".equals(language)) {
name = "菜單配置";
alt = "添加子菜單";
}
buffer.append("  <b>").append(name).append("(")
.append(systemCode).append(")</b>");
buffer.append("   <img src="")
.append(contextPath)
.append("/common/images/btnAddChildMenu.gif" mce_src="")
.append(contextPath)
.append("/common/images/btnAddChildMenu.gif" name=btnAddChildMenu alt=")
.append(alt)
.append(" onclick=/"return postAction(fm,'")
.append(contextPath)
.append("/platform/processGgMenu.do?actionType=prepareInsert&ggMenuMenuID=0','MenuTreeRight')/">");

曾经看过一大块代码:几个类里面除了变量值不一样,其它都一样,差不多是这个意思:
if ("senior manager".equals(roleCode)) {
System.out.println(" I am a "+roleCode);
}else if ("manager".equals(roleCode)) {
System.out.println(" I am a "+roleCode);
}else if ("programer".equals(roleCode)) {
System.out.println(" I am a "+roleCode);
}

想想你有没有做过这样的事情,确实让人想挠墙吧^^

练习:如何简化以下代码(提示,对于稍微复杂的情况,要考虑把共性的地方剥离成方法,将个性的东西做为参数)

GgRiskConfigValueDto ggRiskConfigValueDto = ggRiskConfigValueDao.findByPrimaryKey("01", "0000", "JHMENU_SWITCH");
if (ggRiskConfigValueDto != null && ggRiskConfigValueDto.getConfigValue() != null) {
if ("1".equals(ggRiskConfigValueDto.getConfigValue())) {
//
GgRiskConfigValueDto ggRiskConfigValueDtoTemp = ggRiskConfigValueDao.findByPrimaryKey("01", "0000",
"JHMENU_URL");
if (ggRiskConfigValueDtoTemp != null && ggRiskConfigValueDtoTemp.getConfigValue() != null) {
buffer.append("<div style='height:26px;'><table border=0 cellpadding=0 cellspacing=0 width=100%><tr>");
buffer.append("<td valign=middle class=/"menu" + 1 + "/"");
buffer.append(">");
buffer.append(" <img src="/common/images/imgIcon-1.gif" mce_src="common/images/imgIcon-1.gif" align='absmiddle'> "
+ "<a href="#" mce_href="#" onclick=/"window.open('" + ggRiskConfigValueDtoTemp.getConfigValue()
+ "')/" class='menu1' title='**子系统'>**子系统</a>");
buffer.append("</td></tr></table></div>");
}
}
}

ggRiskConfigValueDto = ggRiskConfigValueDao.findByPrimaryKey("01", "0000", "FXQMENU_SWITCH");
if (ggRiskConfigValueDto != null && ggRiskConfigValueDto.getConfigValue() != null) {
if ("1".equals(ggRiskConfigValueDto.getConfigValue())) {
//开启***菜单
GgRiskConfigValueDto ggRiskConfigValueDtoTemp = ggRiskConfigValueDao.findByPrimaryKey("01", "0000",
"FXQMENU_URL");
if (ggRiskConfigValueDtoTemp != null && ggRiskConfigValueDtoTemp.getConfigValue() != null) {
buffer.append("<div style='height:26px;'><table border=0 cellpadding=0 cellspacing=0 width=100%><tr>");
buffer.append("<td valign=middle class=/"menu" + 1 + "/"");
buffer.append(">");
buffer.append(" <img src="/common/images/imgIcon-1.gif" mce_src="common/images/imgIcon-1.gif" align='absmiddle'> "
+ "<a href="#" mce_href="#" onclick=/"window.open('" + ggRiskConfigValueDtoTemp.getConfigValue()
+ "')/" class='menu1' title='***子系统'>***子系统</a>");
buffer.append("</td></tr></table></div>");
}
}
}

ggRiskConfigValueDto = ggRiskConfigValueDao.findByPrimaryKey("01", "0000", "RESERVEMENU_SWITCH");
if (ggRiskConfigValueDto != null && ggRiskConfigValueDto.getConfigValue() != null) {
if ("1".equals(ggRiskConfigValueDto.getConfigValue())) {
//开启***菜单
GgRiskConfigValueDto ggRiskConfigValueDtoTemp = ggRiskConfigValueDao.findByPrimaryKey("01", "0000",
"RESERVEMENU_URL");
if (ggRiskConfigValueDtoTemp != null && ggRiskConfigValueDtoTemp.getConfigValue() != null) {
buffer.append("<div style='height:26px;'><table border=0 cellpadding=0 cellspacing=0 width=100%><tr>");
buffer.append("<td valign=middle class=/"menu" + 1 + "/"");
buffer.append(">");
buffer.append(" <img src="/common/images/imgIcon-1.gif" mce_src="common/images/imgIcon-1.gif" align='absmiddle'> "
+ "<a href="#" mce_href="#" onclick=/"window.open('" + ggRiskConfigValueDtoTemp.getConfigValue()
+ "')/" class='menu1' title='***子系统'>***子系统</a>");
buffer.append("</td></tr></table></div>");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: