【问题标题】:Simplify Switch Statement in following code在以下代码中简化 Switch 语句
【发布时间】:2015-11-17 16:10:47
【问题描述】:

我怎样才能使这段代码的大小更小,效率更高:) ..? 到目前为止,我已经完成了一个更大的旧代码,但我认为它仍然很大。

if (affix == Mod.Affix)
{
    Graphics.DrawText(text, textSize, position, Color.White);

    switch (levels)
    {
        case 1:
        {
            Size level = Graphics.DrawText(text, textSize, position, Color.Yellow);
            if (level != new Size()) 
            { 
                position.Y += level.Height; 
            }
        } break;

        case 2:
        {
            Size level = Graphics.DrawText(text, textSize, position, Color.Red);
            if (level != new Size()) 
            {
                position.Y += level.Height; 
            }
        } break;

        case 3:
        {
            Size level = Graphics.DrawText(text, textSize, position, Color.Green);
            if (level != new Size()) 
            { 
                position.Y += level.Height; 
            }
        } break;

        default:
            Size nextLevel = Graphics.DrawText(text, textSize, position, Color.Black);

            if (nextLevel != new Size()) 
            { 
                position.Y += nextLevel.Height;
            } 
        break;
    }
}

提前谢谢你!

【问题讨论】:

  • 你在做if(level != new Size())时想要完成什么?
  • @Matias Cicero - 将文本对齐到同一位置。
  • 在情况 1-3 中,您只需要维护 Color。其余代码可以移到开关外。

标签: c# switch-statement code-size


【解决方案1】:

使用字典将级别映射到颜色:

private static Dictionary<int, Color> levelColors = new Dictionary<int, Color>
{
    { 1, Color.Yellow },
    { 2, Color.Red },
    { 3, Color.Green }
};

然后您可以将方法更改为:

Color color;
if (!levelColors.TryGetValue(levels, out color)) // try and get the color for the level
{
    color = Color.Black; // Default to black if no level color found
}

Size level = Graphics.DrawText(text, textSize, position, color);
if (level != new Size())
{
    position.Y += level.Height;
}

这样,您无需在添加/更改级别颜色时修改方法,只需更新字典即可。

【讨论】:

  • 这看起来是最好的解决方案,如果需要,我可以在以后添加更多关卡。非常感谢您。
【解决方案2】:

试试这样的:

Color color = new Color();
switch (levels)
{
    case 1:
        color = Color.Yellow;
        break;

    case 2:
        color = Color.Red;
        break;

    case 3:
        color = Color.Green;
        break;

    default:
        color = Color.Black;
        break;
}
Size level = Graphics.DrawText(text, textSize, position, color);
if (level != new Size()) // ???
{ 
    position.Y += level.Height;
}

【讨论】:

    【解决方案3】:

    您正在为每种颜色执行相同的代码,试试这个:

    switch (levels)
    {
        case 1:
            AddHeight(Color.Yellow);
            break;
        case 2:
            AddHeight(Color.Red);
            break;
        case 3:
            AddHeight(Color.Green);
            break;
        default:
            AddHeight(Color.Black);
            break;
    }
    
    public void AddHeight(Color color){
        Size level = Graphics.DrawText(text, textSize, position, color);
        if (level != new Size()) // ???
        { 
            position.Y += level.Height;
        }
    }
    

    【讨论】:

      【解决方案4】:

      您可以有一个预定义的映射“level_to_color”,例如(例如在静态构造函数中初始化):

      Dictionary<int,Color> _levelToColor = new Dictionary<int,Color>();
      _levelToColor.Add(1, Color.Yellow);
      _levelToColor.Add(2, Color.Red);
      _levelToColor.Add(3, Color.Green);
      

      然后您的代码可能如下所示:

      Color color = _levelToColor.ContainsKey( level ) ?  _levelToColor[level] : Color.Black;
      Size level = Graphics.DrawText(text, textSize, position, color);
      position.Y += level.Height;
      

      【讨论】:

        【解决方案5】:
        if (affix == Mod.Affix)
        {
            Graphics.DrawText(text, textSize, position, Color.White);
        
            Size level =
                 (levels == 1) ? Graphics.DrawText(text, textSize, position, Color.Yellow) 
                 :((levels == 2) ? Graphics.DrawText(text, textSize, position, Color.Red) 
                 : ((levels == 3) ? Graphics.DrawText(text, textSize, position, Color.Green) 
                 : Graphics.DrawText(text, textSize, position, Color.Black)));
        
            if (level != new Size()) 
            { 
                position.Y += nextLevel.Height;
            }
        }
        

        【讨论】:

        • 这很快就会变得难以阅读,嵌套的三元运算符相当邪恶!
        • 是的,我同意,但他要求更短——出于可读性的原因,我不喜欢短。
        • 我认为我的解决方案对于他的特定场景来说足够可读,因为只有 4 个案例。如果它更大,我会建议一些不同的东西。我的观点是我重视可读性而不是行数。这种模式肯定会变得混乱,但在这种情况下似乎仍然合适。
        猜你喜欢
        • 2022-06-14
        • 1970-01-01
        • 2021-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多