一种可以分解三元表达式的工具

Bea*_*Hat 13 c# visual-studio-2008

我正在使用的当前项目的先前开发人员似乎决定创建一些有效且无法管理的代码.

在整个代码中,我发现了多条件三元表达式.翻译和重写/重构它们变得令人头疼.

有没有人知道一个免费工具,独立或作为VS 2008的加载项,可以分解三元表达式?CodeRush在这个项目上没有预算.如果需要,我会继续重新编码,但我想在这里有一点希望.

以下是该问题的一个示例:

sNoteType = objSelection.Items[1].Selected ? 
    objSelection.Items[0].Selected ? 
    objSelection.Items[3].Selected ? 
    objSelection.Items[4].Selected ? 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " :
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " :
    string.Empty + "LT " : 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ?
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "BA " : 
    objSelection.Items[4].Selected ? 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "LT " : 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "LS " : 
    objSelection.Items[3].Selected ? 
    objSelection.Items[4].Selected ? 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "LT " : 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "BA " : 
    objSelection.Items[4].Selected ? 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "LT " : 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "FD " : 
    objSelection.Items[0].Selected ? 
    objSelection.Items[3].Selected ? 
    objSelection.Items[4].Selected ? 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "LT " : 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "BA " : 
    objSelection.Items[4].Selected ? 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "LT " : 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "LS " : 
    objSelection.Items[3].Selected ? 
    objSelection.Items[4].Selected ? 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "LT " : 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "BA " : 
    objSelection.Items[4].Selected ? 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "LT " : 
    objSelection.Items[5].Selected ? 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty + "EV " : 
    objSelection.Items[2].Selected ? 
    string.Empty + "OV " : 
    string.Empty;
Run Code Online (Sandbox Code Playgroud)

Joh*_*ers 11

代码的含义实际上比它看起来简单得多.继续使用ReSharper,我发现了以下内容:

string sNoteType;
var items = objSelection.Items;

var item0Selected = items[0].Selected;
string item3NotSelectedValue;
if (items[1].Selected)
{
    item3NotSelectedValue = item0Selected ? "LS " : "FD ";
}
else
{
    item3NotSelectedValue = item0Selected ? "LS " : string.Empty;
}

if (items[2].Selected)
{
    sNoteType = "OV ";
}
else
{
    if (items[5].Selected)
    {
        sNoteType = "EV ";
    }
    else
    {
        if (items[4].Selected)
        {
            sNoteType = "LT ";
        }
        else
        {
            if (items[3].Selected)
            {
                sNoteType = "BA ";
            }
            else
            {
                sNoteType = item3NotSelectedValue;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于Jon完成了所有艰苦的工作,所以这是一个我认为归结为精华的编辑.显然,你想尽快在这个代码周围进行测试 - 因为我无法想象在解码这个怪物时不会犯错误,而自动重构只能让你到目前为止(不是很好,从样本的外观来看)放在这里):

var items = objSelection.Items;
string sNoteType = string.Empty;    
if (items[0].Selected && items[1].Selected) {
    sNoteType = "LS ";
} else if (items[1].Selected) {
    sNoteType = "FD ";
} else if (items[2].Selected) {
    sNoteType = "OV ";
} else if (items[3].Selected) {
    sNoteType = "BA ";    
} else if (items[4].Selected) {
    sNoteType = "LT ";
} else if (items[5].Selected) {
    sNoteType = "EV ";
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*ann 8

ReSharper可以将三元转换为if/else.

我实际上通过ReSharper运行它,输出同样可怕.祝你在重构方面好运.

if (objSelection.Items[1].Selected)
            if (objSelection.Items[0].Selected)
                if (objSelection.Items[3].Selected)
                    if (objSelection.Items[4].Selected)
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "LT ";
                        }
                    else
                    {
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "BA ";
                        }
                    }
                else
                {
                    if (objSelection.Items[4].Selected)
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "LT ";
                        }
                    else
                    {
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "LS ";
                        }
                    }
                }
            else
            {
                if (objSelection.Items[3].Selected)
                    if (objSelection.Items[4].Selected)
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "LT ";
                        }
                    else
                    {
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "BA ";
                        }
                    }
                else
                {
                    if (objSelection.Items[4].Selected)
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "LT ";
                        }
                    else
                    {
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "FD ";
                        }
                    }
                }
            }
        else
        {
            if (objSelection.Items[0].Selected)
                if (objSelection.Items[3].Selected)
                    if (objSelection.Items[4].Selected)
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "LT ";
                        }
                    else
                    {
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "BA ";
                        }
                    }
                else
                {
                    if (objSelection.Items[4].Selected)
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "LT ";
                        }
                    else
                    {
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "LS ";
                        }
                    }
                }
            else
            {
                if (objSelection.Items[3].Selected)
                    if (objSelection.Items[4].Selected)
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "LT ";
                        }
                    else
                    {
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "BA ";
                        }
                    }
                else
                {
                    if (objSelection.Items[4].Selected)
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "LT ";
                        }
                    else
                    {
                        if (objSelection.Items[5].Selected)
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty + "EV ";
                        else
                        {
                            if (objSelection.Items[2].Selected) sNoteType = string.Empty + "OV ";
                            else sNoteType = string.Empty;
                        }
                    }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)


小智 5

CodeRush Xpress(免费)可以做到这一点以及更多.看到这里.


Mag*_*ken 5

此代码应该与原始怪物的行为相同:

string sNoteType;
if (objSelection.Items[2].Selected)
{
    sNoteType = "OV ";
}
else if (objSelection.Items[5].Selected)
{
    sNoteType = "EV ";
}
else if (objSelection.Items[4].Selected)
{
    sNoteType = "LT ";
}
else if (objSelection.Items[3].Selected)
{
    sNoteType = "BA ";
}
else if (objSelection.Items[0].Selected)
{
    sNoteType = "LS ";
}
else if (objSelection.Items[1].Selected)
{
    sNoteType = "FD ";
}
else
{
    sNoteType = string.Empty;
}
Run Code Online (Sandbox Code Playgroud)

为了达到这个目的,我开始假设无论代码目前做什么都是正确的.然后我编写了一个脚本来生成64个测试用例 - 每个测试用例对应一个真值的组合.想象一下这一堆:

[TestCase("EV ", "000001")]
[TestCase("LT ", "000010")]
[TestCase("EV ", "000011")]
[TestCase("BA ", "000100")]
[TestCase("EV ", "000101")]
Run Code Online (Sandbox Code Playgroud)

然后,我逐一看清模式并逐一添加if语句,直到根本不再需要怪物为止.我不能保证这是原作者想象的逻辑,但我可以保证它在所有可能的情况下都表现相同.