美文网首页简友广场
{C#-02} 简化代码

{C#-02} 简化代码

作者: 码农猫爸 | 来源:发表于2020-04-06 18:18 被阅读0次

目的

  • 简简单单,才能明明白白
  • 用时与复杂度成反比
  • 以下方法,面向函数级,已实测。
  • 方法1:使用?:
namespace SimplifyCode
{
    public class Choice
    {
        bool weekend = true;

        // 常规
        public string Complex()
        {
            if (weekend)
            {
                return "rest";
            }
            else
            {
                return "work";
            }
        }

        // 卫函数,减少层次
        public string Guard()
        {
            if (weekend) return "rest";

            return "work";
        }

        // 条件操作符
        public string Operator()
        {
            return weekend ? "rest" : "work";
        }

        // 表达式
        public string Lambda()
            => weekend ? "rest" : "work";
    }
}
  • 方法2:使用||
using System.Windows.Forms;

namespace SimplifyCode
{
    public class Validation
    {
        public void Complex()
        {
            if (IsWrongLicense()) return;

            IsWrongPassword();
        }

        public void Simple()
        {
            var result =
                IsWrongLicense() ||
                IsWrongPassword();
        }

        private bool IsWrongLicense()
        {
            bool result = true; // 省略具体判定

            if (result)
            {
                MessageBox.Show("Not right license!");
            }

            return result;
        }

        private bool IsWrongPassword()
        {
            bool result = true; // 省略具体判定

            if (result)
            {
                MessageBox.Show("Not right password!");
            }

            return result;
        }
    }
}
  • 方法3:使用?.
using System;
using System.Windows.Forms;

namespace SimplifyCode
{
    public delegate void ClickContent(string content);

    public class UserCtrl
    {
        public event ClickContent ContentClicked;

        public void Complex_Click(object sender, EventArgs e)
        {
            if (ContentClicked == null) return;

            ContentClicked.Invoke((sender as TextBox).Text);
        }

        public void Simple_Click(object sender, EventArgs e)
        {
            ContentClicked?.Invoke((sender as TextBox).Text);
        }
    }

    public class SomeForm
    {
        private UserCtrl ctrl = new UserCtrl();

        public SomeForm()
        {
            ctrl.ContentClicked += OnContentClicked;
        }

        private void OnContentClicked(string content) { }
    }
}
  • 方法4:使用??
namespace SimplifyCode
{
    public class Info
    {
        public string ServerIP { get; set; }
        public string DefaultServerIP { get; set; }
    }

    public class App
    {
        public Info info = new Info();

        public string Complex_GetServerIP()
        {
            string serverIP = info.ServerIP;
            if (info.ServerIP != null)
            {
                return serverIP;
            }

            return info.DefaultServerIP;
        }

        public string Simple_GetServerIP()
            => info.ServerIP ?? info.DefaultServerIP;
    }
}
  • 方法5:使用$
namespace SimplifyCode
{
    public class Sql
    {
        private string schema, table;

        public Sql(string schema, string table)
        {
            this.schema = schema;
            this.table = table;
        }

        public string Complex_GetMaxID()
        {
            return string.Format($"use {0};"
                + $"\n select max(id) from {1};",
                schema, table);
        }

        public string Simple_GetMaxID()
        {
            return $"use {schema};"
                + $"\n select max(id) from {table};";
        }
    }
}

相关文章

  • {C#-02} 简化代码

    目的 简简单单,才能明明白白 用时与复杂度成反比 以下方法,面向函数级,已实测。 方法1:使用?: 方法2:使用|...

  • 简化代码

    1.android studio 快速删除无用的import包 https://blog.csdn.net/bzl...

  • 代码简化

    lombok 库是一个使用java注解,自动生成getter和setter方法的库官网 https://proje...

  • {C#}构造器

    背景 生产环境时,简化代码。 单测环境时,简化代码。

  • js:倒计时(天时分秒)

    html代码(简化): js代码:

  • js:点击别处隐藏div

    html代码(简化): js代码:

  • 代码简化之路

    这段时间一直在啃算法,很久都没有写过文章了,主要是算法这东西被人反反复复写,水平高低的文章都有,我也就不想凑这个热...

  • 代码简化探索

    代码简化探索 对于 for 循环中的代码,非常的丑陋,也不能扩展 思考1:使用自定义加强的 ArrayList,可...

  • 如何自定义注解

    什么是注解?为了简化代码,推荐使用大量注解,简化代码,提高开发效率springMVC,springboot 自定义...

  • 简化代码之Java

    1. 去除数组中的空值和null值,并返回新的数组 2. 拼接字符串,若字符串不为空则以空格隔开,为空则忽略(比如...

网友评论

    本文标题:{C#-02} 简化代码

    本文链接:https://www.haomeiwen.com/subject/pqkyuhtx.html