zoukankan      html  css  js  c++  java
  • 代码简洁(注意事项)

    internal class CleanCode
    {
    private CultureInfo m_CurrentCulture;
    private CultureInfo m_CurrentUICulture;
    private int m_Priority;
    private string m_Name;

    //提供有意义的命名

    //一行语句也尽量使用大括号
    private string FindElement(List<string> argElementList, string argDest)
    {
    if (argElementList == null || argElementList.Count == 0)
    {
    return "";
    }
    else
    {
    // return (from c in argElementList where c == argDest select c).First();

    //return argElementList.FindAll((value) => { return value == "张三"; });
    return argElementList.Find((value) => { return value == "张三"; });

    }
    }

    #region 方法抽象级别在同一层次上

    public void Init()
    {
    LocalInit();
    RemoteInit();
    }

    private void LocalInit()
    {
    }

    private void RemoteInit()
    {
    }
    #endregion

    /*单一职责原则:一个方法只做一件事
    *
    * 避免过长的方法或类:
    * 应该考虑是否可以拆成几个小的方法
    * 一般类型的代码超过300行则要考虑是否重构
    * 一般方法的代码超过30行、一屏也要考虑是否重构
    *
    * 只对外公布必要的操作:
    * 比如类、方法、属性,默认应该是private的
    *
    * 重构多个相关属性为一个类
    *
    * 不重复代码:
    * 代码重复不利于维护
    *
    * 避免过长的if、switch分支:使用“表驱动法”(查字典)
    *
    *
    */


    #region “表驱动法”(查字典)

    public enum Week
    {
    Monday, Tuseday, Wednesday, Thursday, Friday, Saturday, Sunday
    }
    static string GetChineseWeekInTable(Week argWeek)
    {
    //调用:GetChineseWeekInTable(Week.Friday)

    string[] chineseWeek = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
    return chineseWeek[(int)argWeek];
    }

    /// <summary>
    /// 调用方式
    /// </summary>
    void InvokeTest()
    {
    SampleClass sample = new SampleClass();
    var addMethod = typeof(SampleClass).GetMethod((ActioInTable(Week.Monday)));
    addMethod.Invoke(sample, null);
    }
    static string ActioInTable(Week argWeek)
    {
    string[] methods = { "Cleaning", "CleanCloset", "Working", "Working", "Working", "Shopping", "Shopping" };
    return methods[(int)argWeek];
    }

    class SampleClass
    {
    public void Cleaning()
    {
    Console.WriteLine("Cleaning..");
    }
    public void CleanCloset()
    {
    Console.WriteLine("CleanCloset..");
    }
    public void Working()
    {
    Console.WriteLine("Working..");
    }
    public void Shopping()
    {
    Console.WriteLine("Shopping..");
    }
    }

    #endregion

    }

  • 相关阅读:
    深入理解is_callable和method_exists
    php数据加密及数据存储和传输
    yii2深入理解之内核解析
    细说 unicode 、utf-8 、utf-16、ascii 、gbk 、gb2312
    RESTful API架构和oauth2.0认证机制(概念版)
    深入理解yii2之RBAC(模块化系统)
    yii rbac
    Yii2 设计模式
    深入理解PHP数组函数和预定义接口
    PHP 代码规范、流程规范、git规范
  • 原文地址:https://www.cnblogs.com/jx270/p/3633122.html
Copyright © 2011-2022 走看看