限制代码量和switch语句 [英] Limiting amount of code and switch statements

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

问题描述

大家好,

i希望能帮助解决我在减少重复代码方面遇到的问题。



i am想要为动态页面编写一个方法,它接收一个未知对象(可能有4个选项)。



然后创建该类型对象的新实例加载所有属性(在对象构造函数中完成)。但需要每个可能性来告诉它要加载哪些对象相关对象,例如,如果产品循环通过product.RelatedContent,如果行业循环通过industry.RelatedContent如果你明白我的意思。



然后访问该对象的list属性以访问subObjects以根据这些细节构建html。但是,不是编写一个开关案例,因为每个可能性因而导致臭代码,并重复我自己,无论如何我是否可以编写代码更通用和松散耦合?当前代码(我只做了1个示例保存质量代码):



Hi All,
i was hoping for help with an issue i'm having with regards to cutting down repetitive code.

i am wanting to write a method for a dynamic page, that takes in a unknown object (possible 4 options).

It then creates a new instance of that type of object and loads all properties (done in object constructor). But need every posibiility to tell it which objects related objects to load, e.g if product loop through product.RelatedContent, if industry loop through industry.RelatedContent if you understand what i mean.

Then access a list property of that object to access subObjects to build html based on those details. But rather than writing a switch case, for every posibility and thus causing smelly code, and repeating myself, is there anyway i can write the code to be more generic and loosely coupled? current code (i've only done 1 example save mass code):

public partial class RelatedContent : System.Web.UI.Page
{
    Product prod;
    KnowHowArea kha;
    Khia khia;
    Industry ind;
    Belzona.ILanguage language;

    protected void Page_Load(object sender, EventArgs e)
    {
        var referer =  Request.QueryString["Referer"];
        var request = Request.QueryString["Request"];
        var number = Request.QueryString["Number"];
        language = Belzona.Language.GetByUIValue(Request.QueryString["Language"]);

        LoadReferer(referer, number,request);
    }

    private void LoadReferer(string referer, string number,string request)
    {
        StringBuilder sb = new StringBuilder();
        Guid guid = Guid.Parse(number);
        sb.Append("<ul id=\"i_lst\" data-role=\"listview\" class=\"related_links_nested\">");

        switch (referer)
        {
            case "prod":
                prod = new Belzona.ProductsMySQL.Product(number);
                switch (request)
                {
                    case "ind":
                        foreach (IndustryProxy ind in prod.RelatedIndustries)
                        {
                            sb.Append("<li class=\"ui-li-has-thumb ui-btn ui-btn-icon-right ui-li ui-btn-up-c\">");
                            sb.Append("<a data-transition=\"slide\" href=\"industry_info.aspx?guid=" + number + "&language=" + language.UIValue + "\">");
                        }
                        break;
                    case "khia":
                        foreach (Khia khia in prod.RelatedKnowHowInActions)
                        {
                            sb.Append("<li class=\"ui-li-has-thumb ui-btn ui-btn-icon-right ui-li ui-btn-up-c\">");
                            sb.Append("<a data-transition=\"slide\" href=\"khia_info.aspx?guid=" + number + "&language=" + language.UIValue + "\">");
                        }
                        break;
                    case "app":
                        foreach (Belzona.KnowHowAreas.KnowHowAreaProxy knowHow in prod.RelatedKnowHowAreas)
                        {
                            sb.Append("<li class=\"ui-li-has-thumb ui-btn ui-btn-icon-right ui-li ui-btn-up-c\">");
                            sb.Append("<a data-transition=\"slide\" href=\"khia_info.aspx?guid=" + number + "&language=" + language.UIValue + "\">");
                        }
                        break;
                }
                break;
            case "ind":

                ind = new Belzona.Industries.Industry(guid, language);
                break;
            case "khia":
                int id = int.Parse(number);
                khia = new Belzona.KnowHowInAction.Khia(id);
                break;
            case "app":
                kha = new Belzona.KnowHowAreas.KnowHowArea(guid, language);
                break;
        }
    }

推荐答案

我不喜欢重构代码,这是我咨询业务的一部分,但我我想给你一些建议:



1.更多通用?为此,我认为'ind'khia和'app对象必须实现一个你可以用/编程的公共接口,以创建一种带有一些套接字的类工厂,用于注入方法或其他物体。在没有真正了解整体数据结构和类的情况下很难说更多。



2.是的,我将单独处理'prod结果其他三种情况,把它放在自己的方法中。



3.重新使用StringBuilder:使用Clear方法(在.NET 4.0及更高版本中可用)清除它再次使用之前。



4.将重复的字符串移出'switch语句并使它们成为常量字符串,并重新使用它们的引用来创建更清晰,更易读的代码。
I am averse to refactoring code when that's part of my consulting practice, but I do want to give you some suggestions:

1. more Generic ? For this I think that 'ind 'khia, and 'app objects would have to all implement a common Interface that you could program with/to, to make a kind of "class factory" with some "sockets" in it for injection of methods, or other objects. hard to say more without really knowing the overall data-structures and Classes here.

2. yes, I'd separate out the handling of the 'prod result from the other three cases, put it in its own Method.

3. re-use the StringBuilder: use the Clear method (available in .NET 4.0 and later) to clear it before using it again.

4. move the repeated strings out of the 'switch statements and make them string Constants, and re-use the references to them to create cleaner, more readable code.


正如我已经告诉过你的那样,你无法最大限度地减少阻塞或开关的使用,因为它们都是不同的条件。直到您发布了该评论 - 这是有用的,这里将是您的应用程序块。



在第一个块中,你有三个执行相同活动的代码块,但它们的循环取决于每三个集合中的元素数量。所以你可以做的是,只计算它们的总数并循环(可以使用)并在其中添加代码块。



As I already told you that there is no way you can minimize the block, or the switch usage you're going to use them because they're all different conditions. Until you posted that comment - which was helpfull and here would be the block for your application.

Inside the first block, you're having three code blocks which perform the same activity but their loop depends on the number of elements in each three collections. So what you can do is, to just count the total of them and loop (for can be used) and add the code block in it.

int totalLoopCounts = prod.RelatedIndustries.Count + 
                      prod.RelatedKnowHowInActions.Count + 
                      prod.RelatedKnowHowAreas.Count;
// if Count is not available, use Count(), but Count should work to count the total
// Run the loop until it completes the process for all of the elements
for (int i = 0; i < totalLoopsCounts; i++)
{
    // for each of the item, loop and execute the following
    sb.Append("<li class="\"ui-li-has-thumb" ui-btn="" ui-btn-icon-right="" ui-li="" mode="hold" />    sb.Append("<a data-transition="\"slide\"" href="\"industry_info.aspx?guid="" number="" language.uivalue="">");
}
</a>





将以上代码添加到




Add the above code to the first switch below the

case "prod":
    prod = new Belzona.ProductsMySQL.Product(number);





提示:为什么要添加新的列表项而不关闭前一个?请修一下。



Tip: Why are you adding a new list item without closing the previous one? Fix that one please.


这篇关于限制代码量和switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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