代码需要一些清理。 [英] Code needs some cleanup.

查看:102
本文介绍了代码需要一些清理。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信以下代码看起来并不专业,所以请提供一些建议,以便简短。



Hi, I am sure following code doesn''t look professional so please give some suggestions to make it short.

if (((DevExpress.Xpf.LayoutControl.GroupBox)sender).State == DevExpress.Xpf.LayoutControl.GroupBoxState.Maximized)
{
   Employee emp = (((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content) as Employee;
   if (emp != null)
   {
      User user = emp.user;
      ((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content = new Employee(user);
   }
   else
   {
      EmployeeMin empMin = (((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content) as EmployeeMin;
      User user = empMin.user;
      ((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content = new Employee(user);
   }
}
else
{
   Employee emp = (((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content) as Employee;
   if (emp != null)
   {
      User user = emp.user;
      ((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content = new EmployeeMin(user);
   }
   else
   {
      EmployeeMin empMin = (((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content) as EmployeeMin;
      User user = empMin.user;
      ((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content = new EmployeeMin(user);
   }
}

推荐答案

我的建议是



保持这个''((DevExpress.Xpf.LayoutControl.GroupBox)发送者)在一个位置变量中说mySender



替换''所有出现的''(( DevExpress.Xpf.LayoutControl.GroupBox)sender)''with mySender



如果你想进一步为myStent定义mySender.Content的另一个变量并替换所有出现的''((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content''with myContent



arh ..我刚注意到标签''优化''如果你正在重新安排这个代码块以改进程序优化然后你错了,你的编译器已经为你做了这个。
my suggestions are

Keep this ''((DevExpress.Xpf.LayoutControl.GroupBox)sender) in a location variable say mySender

replace all the occurrence of ''((DevExpress.Xpf.LayoutControl.GroupBox)sender)'' with mySender

if you want go further define another variable for mySender.Content as myContent and replace all the occurrence of ''((DevExpress.Xpf.LayoutControl.GroupBox)sender).Content'' with myContent

arh.. I just noticed the tag ''Optimization'' if you are re-arranging this code block to improve the program optimization then you are wrong, your compiler already doing this for you.


作为Adam R Harris你可以使用局部变量存储数据代码访问不止一次;在您的代码中,最常用的变量是发件人强制转换为 DevExpress.Xpf.LayoutControl.GroupBox ;第二个是内容属性。



以下是可能的安排:



As Adam R Harris you can use local variables to store data the code access more than once; in your code the variable most used is sender cast to DevExpress.Xpf.LayoutControl.GroupBox; the second one is its Content property.

The followin is a possible arrangement:

var box = sender as DevExpress.Xpf.LayoutControl.GroupBox;
if (box != null)
{
    var oldContent = box.Content;
    /* the following would be better as ...
    ContentType newContent = null;
    */
    var newContent = oldContent;

    var emp = oldContent as Employee;
    if (emp == null)
        emp = oldContent as EmployeeMin;

    if (box.State == DevExpress.Xpf.LayoutControl.GroupBoxState.Maximized)
    {
        newContent = new Employee(emp.user);
    }
    else
    {
        newContent = new EmployeeMin(emp.user);
    }

    if (newContent != oldContent)
        box.Content = newContent;
}





我用 var 来减少类型名称的外观。只有一条指令你应该明确地输入类型名称,但我不知道它所以我不能为你做;我用 ContentType 的块注释作为我不知道的类型的名称(可能是对象,或者 Employee 或基本类型)。 />


问候,

Daniele。



I used var to reduce type name appearance. There is only one instruction where you should explicitly put the type name, but I don''t know it so I can''t do it for you; I had sorrounded it with a block comment with ContentType as the name of the type I don''t know (maybe object, or Employee or a base type).

Regards,
Daniele.


这篇关于代码需要一些清理。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆