zoukankan      html  css  js  c++  java
  • 项目中遇到的一些需要重构的问题以及解决方案1应该尽量减少缩进层次

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

    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);
                }                        
            }


    作者:Lance
    出处:http://www.cnblogs.com/nuaalfm/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    layui框架如何在停止服务用户操作任何步骤自动跳转登录页
    h5移动端和ios以及安卓客户端的接口联调
    sublime 主要使用方法
    内外边距、浮动、布局相关
    js介绍
    css的三个特性 背景透明设置
    选择器 导航制作相关
    表单综合
    自定义列表dl
    相对路径和绝对路径
  • 原文地址:https://www.cnblogs.com/nuaalfm/p/1727422.html
Copyright © 2011-2022 走看看