減小if...的使用

最近維護一批代碼,其中包括一堆if...的使用,多的狀況嵌套八、9層,痛苦不堪,因此搜尋一些能夠下降if...else的方法來改善一下代碼,寫個簡單總結。架構

 

 

 第一種:less

優化前 ide

if (measuredValue > 8)
    return 5 * measuredValue * measuredValue - 3;
            
if (measuredValue > 4)
    return 4 * measuredValue - 2;

if (measuredValue >= 0)
    return 3 * measuredValue - 1;

return 2 * measuredValue;

使用列表和linq優化後(摘自:https://www.linkedin.com/pulse/if-less-programming-c-jiri-pokorny優化

using System;
using System.Linq;

namespace ConsoleApp1
{
    internal class Program
    {
        private static readonly MeasureTransform[] Transformations =
           new MeasureTransform[]
           {
            // 這個順序決定了判斷前後順序
            new MeasureTransform(m => m > 8, m => 5 * m * m - 3),
            new MeasureTransform(m => m > 4, m => 4 * m - 2),
            new MeasureTransform(m => m >= 0, m => 3 * m - 1),
            new MeasureTransform(m => true, m => 2 * m)
           };

        private static void Main(string[] args)
        {
            var executor = Transformations.First(t => t.CanApply(16));
            Console.Write(executor.Transform(16));
        }
    }

    internal class MeasureTransform
    {
        public MeasureTransform(Func<int, bool> canApply, Func<int, int> transform)
        {
            CanApply = canApply ?? throw new ArgumentNullException(nameof(canApply));
            Transform = transform ?? throw new ArgumentNullException(nameof(transform));
        }
        public Func<int, bool> CanApply { get; set; }
        public Func<int, int> Transform { get; set; }
    }
}

第二種:使用邏輯運算符改善ui

using System;
using System.Linq;

namespace ConsoleApp1
{
    internal class Program
    {

        private static void Main(string[] args)
        {
            int a = 10;
            int b = 10;

            // 優化前
            if (a > 5)
            {
                if (b > 10)
                {
                    Console.Write("");
                }
            }

            // 優化後
            if (a > 5 && b > 10)
            {
                Console.Write("");
            }
        }
    }
}

第三種:從業務邏輯角度看看有沒有多餘的判斷spa

using System;
using System.Linq;

namespace ConsoleApp1
{
    internal class Program
    {

        private static void Main(string[] args)
        {
            System.UInt32 a = 10;
            int b = (int)a + 10;

            // 優化前
            if (a > 10)
            {
                if (b > 10)
                {
                    Console.Write("");
                }
            }

            // 優化後
            if (a > 10)
            {
                Console.Write("");
            }
        }
    }
}

第四種:使用三元運算符
code

優化前orm

namespace ConsoleApp1
{
    internal class Program
    {

        private static void Main(string[] args)
        {
            System.UInt32 a = 10;
            uint c;
            if (a > 10)
            {
                c = a;
            }
            else
            {
                c = a + 10;
            }
        }
    }
}
View Code

優化後blog

using System;
using System.Linq;

namespace ConsoleApp1
{
    internal class Program
    {

        private static void Main(string[] args)
        {
            System.UInt32 a = 10;
            int b = (int)a + 10;

            // 優化前
            var c = a > 10 ? a : a + 10;

        }
    }
}
View Code

第五種:太多if..else if...效率低,使用switch...case...,也好看點。ip

第六種:從架構層面使用依賴注入,反射之類的,參考https://www.c-sharpcorner.com/forums/how-can-i-remove-multiple-if-statement

相關文章
相關標籤/搜索