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

项目中遇到的一些需要重构的问题以及解决方案1-应该尽量减少缩进层次

2011年02月05日 ⁄ 综合 ⁄ 共 6923字 ⁄ 字号 评论关闭

简单的判断对任何人来说都不难理解,而且加入条件判断非常容易,这往往导致大家无节制的使用深层次的条件判断,但如果层次过多,嵌套过多代码会非常不容易理解,这里结合项目中的几段代码来说说如何去除不必要的条件嵌套。

1、尽量去除else

修改前:

 

代码

 public object GetEntityById(int? id, NameValueCollection nameValues)
        {
            
object entity;
            
if (id != null)
            {
                entity 
= EntityBll.GetEntityById(id.Value);
            }
            
else
            {
                entity 
= EntityBll.GetNewEntity();

                if (nameValues != null)
                {
                    
foreach (var n in nameValues.AllKeys)
                    {
                        EntityBll.SetEntityPropertyValue(entity, n, nameValues[n]);
                    };
                }
            }
            
return entity;
        }

 

修改后:

 

代码

public object GetEntityById(int? id, NameValueCollection nameValues)
        {
            
            
if (id != null)
            {
                
return EntityBll.GetEntityById(id.Value);
            }
            
object entity= EntityBll.GetNewEntity();

            if (nameValues != null)
            {
                
foreach (var n in nameValues.AllKeys)
                {
                    EntityBll.SetEntityPropertyValue(entity, n, nameValues[n]);
                };
            }

            return entity;
        }

 

 2、适当调整条件顺序去除条件层次

修改前:

 

代码

public string GetString(string key, string culture)
        {
            Check.Argument.IsNotEmpty(key, 
"key");
            Check.Argument.IsNotEmpty(culture, 
"culture");
            var condition 
= new Dictionary<stringstring>();
            condition.Add(CoreConstDefine.CodeColumnCode, key);
            condition.Add(
"LanguageName", culture);
            var dbLocalize 
= localize.GetEnumerable(condition).FirstOrDefault();
            var displayName 
= string.Empty;
            
if (dbLocalize == null)
            {
                
if (GetCulture() == CoreConstDefine.LanguageCN)
                {
                    dbLocalize 
= localize.GetNew();
                    dbLocalize.Code 
= key;
                    dbLocalize.LanguageName 
= CoreConstDefine.LanguageCN;
                    dbLocalize.DisplayName 
= string.Empty;
                    localize.Save(dbLocalize);
                }
            }
            
else
            {
                displayName 
= dbLocalize.DisplayName;
            }

            return GetCultureString(culture, key, displayName);
        }

 

 

 修改后:

 

代码

 public string GetString(string key, string culture)
        {
            Check.Argument.IsNotEmpty(key, 
"key");
            Check.Argument.IsNotEmpty(culture, 
"culture");
            var condition 
= new Dictionary<stringstring>();
            condition.Add(CoreConstDefine.CodeColumnCode, key);
            condition.Add(
"LanguageName", culture);
            var dbLocalize 
= localize.GetEnumerable(condition).FirstOrDefault();
            var displayName 
= string.Empty;
            
if (dbLocalize != null)
            {
                displayName 
= dbLocalize.DisplayName;

            }
            else if (GetCulture() == CoreConstDefine.LanguageCN)
            {
                dbLocalize 
= localize.GetNew();
                dbLocalize.Code 
= key;
                dbLocalize.LanguageName 
= CoreConstDefine.LanguageCN;
                dbLocalize.DisplayName 
= string.Empty;
                localize.Save(dbLocalize);
            }
            
return GetCultureString(culture, key, displayName);
        }

 

3、不要因为性能问题而使代码重复

修改前

代码

 private void chkAll_Click(object sender, RoutedEventArgs e)
        {
            CheckBox chk 
= sender as CheckBox;
            
bool check = chk.IsChecked.Value;
            
if (check)
            {
                
foreach (object obj in dgUserList.ItemsSource)
                {
                    chk 
= dgUserList.Columns[0].GetCellContent(obj).FindName("chkUser"as CheckBox;
                    
if (chk != null)
                    {
                        chk.IsChecked 
= true;
                    }
                }
            }
            
else
            {
                
foreach (object obj in dgUserList.ItemsSource)
                {
                    chk 
= dgUserList.Columns[0].GetCellContent(obj).FindName("chkUser"as CheckBox;
                    
if (chk != null)
                    {
                        chk.IsChecked 
= false;
                    }
                }
            }
        }

 

 

修改后:

 

代码

 private void chkAll_Click(object sender, RoutedEventArgs e)
        {
            CheckBox chk 
= sender as CheckBox;
            
bool check = chk.IsChecked.Value;
            
foreach (object obj in dgUserList.ItemsSource)
            {
                chk 
= dgUserList.Columns[0].GetCellContent(obj) as CheckBox;
                
if (chk != null)
                {
                    chk.IsChecked 
= check;
                }
            }
        }

 

 

4、及时return

修改前

代码

private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            
            
if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                txtUserName.Focus();
            }
            
else
            {
                
if (string.IsNullOrEmpty(txtPwd.Password.Trim()))
                {
                    txtPwd.Focus();
                }
                
else
                {
                    
try
                    {
                        EndpointAddress address 
= new EndpointAddress(GlobalData.loginService);
                        PollingDuplexHttpBinding binding 
= new PollingDuplexHttpBinding();

                        proxy = new LoginServiceClient(binding, address);

                        //LoginService.User
                        proxy.LoginMessageReceived += new EventHandler<LoginMessageReceivedEventArgs>(proxy_LoginMessageReceived);
                        proxy.LoginAsync(EnterpriseId 
+txtUserName.Text, txtPwd.Password);
                    }
                    
catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }              
            }            
        }

 

 

修改后:

 

代码

 private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            
if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                txtUserName.Focus();
                
return;
            }

            if (string.IsNullOrEmpty(txtPwd.Password.Trim()))
            {
                txtPwd.Focus();
                
return;
            }

            try
            {
                EndpointAddress address 
= new EndpointAddress(GlobalData.loginService);
                PollingDuplexHttpBinding binding 
= new PollingDuplexHttpBinding();

                proxy = new LoginServiceClient(binding, address);

                //LoginService.User
                proxy.LoginMessageReceived += new EventHandler<LoginMessageReceivedEventArgs>(proxy_LoginMessageReceived);
                proxy.LoginAsync(EnterpriseId 
+txtUserName.Text, txtPwd.Password);
            }
            
catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }                        
        }

 

 

抱歉!评论已关闭.