现在的位置: 首页 > 综合 > 正文

If Else 优化之道

2013年10月10日 ⁄ 综合 ⁄ 共 4017字 ⁄ 字号 评论关闭

    每当看到一个方法中有几百行代码,里面被层层的If Else所包围,我都感叹,为什么我们不能对If Else优化一下呢?早些年,Table很流行,搭建页面框架比较简单,但是慢慢人们发现层层的嵌套,却并不利于代码的阅读,于是人们发明了Div来代替Table。If Else尽管处理业务逻辑比较简单,但是层层的嵌套却不利于代码的阅读,而且降低了系统的效率,为什么我们不能优化一下If Else呢?

    其实,优化If Else很简单,只需三招即可搞定。
    第一招:将满足判断条件更高的结构写在前面。因为If Else每执行一段代码,都要判断条件是否满足,如果不满足,则向下继续寻找满足的条件,直到找到符合的条件或者If结构结束。因此, 将满足判断条件更高的结构写在前面 ,就会减少条件的判断,降低系统执行的时间,提高系统的效率。在某些情况下,使用Switch的效率要比If Else高,因为switch条件不会重复的进行条件判断。
    第二招:使用二分法进行条件判断。严格来说,使用二分法并不能降低程序的执行次数,只是对于那些条件符合判定的概率大致相同的情况下,能够降低平均查找时间。在写程序时,这样的条件基本上不会遇到,不过这里还是给出示例代码,希望朋友们在遇到这种情况下,知道有一种二分法可以降低平均查找次数。
    示例代码如下:
getProperty : function(key){
if(key == 1){
}else if(key == 2){
}else if(key == 3){
}else if(key == 4){
}else if(key == 5){
}else if(key == 6){
}else if(key == 7){
}else if(key == 8){
}else if(key ==9){
}
}
halfGetProperty : function(key){
if(k > 5){
if(key >7){
if(k == 8){
}else{
}
}else{
if(key == 7)else{
}
}
}else {
if(key < 3){
if(key == 2){
}else{
}
}else{
if(key == 3){
}else if(key == 4){
}else{
}
}
}
}
    第三招:使用数组或者配置文件优化系统结构。每当看到一个长达几千行的代码中,充斥着无数个If Else,我都想吐。虽然这些结构都比较类似,但是由于代码段长度过长,已经失去了可读性。每当需要修改代码的时候,都要去那几千行代码中找到自己对应的If结构,费时又费力,而且更改的风险还特别高。
    如果我们采用配置文件或者数组来处理这些差异的部分,就会发现类的结构就会变得聚合度特别强,而耦合度特别低,而且代码的可读性特别高,如果有需要更改的地方,很快就能定位到相关的代码。
    下面的代码是加载表单的一个例子,因为要保持系统结构的一致性,很多模块重用了一个界面,但是我们要根据不同的业务,对表单上的内容(Tab页和Buttons)做不同的显示。通过代码我们就能够看出在当前的代码情况下,如果有新需求增加进来,仍然要重用这个界面,更改起来的差异有多大。
    
   //原来加载表单的方法
init : function(type){
if(type == "A"){
this.tabContainer.addChild(A);
this.tabContainer.addChild(E);
this.tabContainer.removeChild(B);
this.tabContainer.removeChild(C);
this.tabContainer.removeChild(D);
this.ok.domNode.style.display = "inline-block";
this.save.domNode.style.display = "inline-block";
this.cancel.domNode.style.display = "inline-block";
}else if(type == "B"){
this.tabContainer.addChild(B);
this.tabContainer.addChild(E);
this.tabContainer.removeChild(A);
this.tabContainer.removeChild(C);
this.tabContainer.removeChild(D);
this.ok.domNode.style.display = "inline-block";
this.save.domNode.style.display = "none";
this.cancel.domNode.style.display = "inline-block";
}else if(type == "C"){
this.tabContainer.addChild(C);
this.tabContainer.addChild(E);
this.tabContainer.removeChild(B);
this.tabContainer.removeChild(C);
this.tabContainer.removeChild(D);
this.ok.domNode.style.display = "none";
this.save.domNode.style.display = "none";
this.cancel.domNode.style.display = "inline-block";
}else if(type == "D"){
this.tabContainer.addChild(D);
this.tabContainer.addChild(E);
this.tabContainer.removeChild(B);
this.tabContainer.removeChild(C);
this.tabContainer.removeChild(A);
this.ok.domNode.style.display = "inline-block";
this.save.domNode.style.display = "inline-block";
this.cancel.domNode.style.display = "none";
}else{
this.tabContainer.addChild(A);
this.tabContainer.addChild(B);
this.tabContainer.addChild(C);
this.tabContainer.addChild(D);
this.tabContainer.addChild(E);
this.ok.domNode.style.display = "inline-block";
this.save.domNode.style.display = "inline-block";
this.cancel.domNode.style.display = "inline-block";
}
}
//使用数组加载表单
var config = {
A : {
tabContainer : ["A","E"],
buttons : ["ok","save","cancel"]
},
B : {
tabContainer : ["B","E"],
buttons : ["ok","cancel"]
},
C : {
tabContainer : ["C","E"],
buttons : ["cancel"]
},
D : {
tabContainer : ["A","E"],
buttons : ["ok","save"]
},
E : {
tabContainer : ["A","B","C","D","E"],
buttons : ["ok","save","cancel"]
}
}
//构建当前业务模块的界面显示
initConfig : function(type){
this.buildTabContainer(type);
this.buildButtons(type);
}
//根据配置,构建当前业务模块所需的TabContent
buildTabContainer : function(type){
var toLoadTabContent = this.config[type].tabContainer;
for(var i=0,l=toLoadTabContent.length;i<l;i++){
var tabDijit = this._createTabContentPane(toLoadTabContent[i]);
if(tabDijit){
this.tabContainer.addChild(tabDijit);
}
}
},
//根据配置,构建当前业务模块所需的功能Buttons
buildButtons : function(type){
var buttonsConf = this.config[type].buttons;
for(var i=0; i<buttonsConf.length; i++){
var buttonName = buttonsConf[i];
var button = this._buildButton(buttonName);
this[buttonName] = button;
dojo.style(button.domNode, {"display": "inline-block"});
button.placeAt(this.buttonPane.containerNode);
}
},
_createTabContentPane : function(tabContentName){
//构建ContentPane Dijit,省去代码实现
},
_buildButton : function(buttonName){
//构建当前业务所需的Buttons,省去代码实现
},
    以上只是我在开发过程中,遇到的关于If Else优化的方法,如果你有更好的方法,欢迎分享~@@~。
    

抱歉!评论已关闭.