zoukankan      html  css  js  c++  java
  • C# 编码标准(一)

    一直想写一个自己用的代码标准,经过一段时间的优秀开源源码的观察和看其他人写的标准,感觉好的代码给人感觉就是舒服,也非常重要。所以把它们记录归纳总结,以备以后忘记,另外平时写代码的时候可以拿来参考下。下面的示例主要以Microsoft的代码为准。

     
    命名约定
    PascalCasing
    PascalCasing 每一个单词第一个字母大写,其余字母均小写。例如:FileAccess,ArraySegment等。
     
    除了参数、变量、常量外,所有命名空间名称、类、函数、接口、属性、事件、枚举等名称的命名,使用 Pascal 风格。
     
    camelCasing
     
    camelCasing 第一个单词首字母小写,其余单词首字母大写。例如:propertyName,filePath等。
     
    参数与变量的命名使用camelCasing.
     
    SCREAMING_CAPS
     
    SCREAMING_CAPS每个单词的所有字母都大写,单词与单词之间用"_"连接,该风格目前在c#中只用于const常量。
     
    如:public const string DEFAULT_PAGE = "default.aspx";
     
    私有变量的命名
     
    Private 的私有变量使用下划线"_"或"m_"+camelCasing的大小写规则,以便快速确认该变量的作用域。
     
    如: private int _userId;
     
      private int m_userId;
     
    一、命名约定与风格
     
    1、类型名与方法名用pascal命名规范:
     
     
        public class StringReader : TextReader
        {
           public override String ReadToEnd()
               {
                 ...
               }
       }
     
    2、局部变量与方法参数使用camel命名规范:
     
     
       internal static String InternalCopy(String sourceFileName, String destFileName, bool checkHost) 
       {
            string fullSourceFileName = Path.GetFullPathInternal(sourceFileName);
          ...
       }
     
    3、接口名加前缀I:
     
     
       public interface IComparer
       {
             // Compares two objects. An implementation of this method must return a
             // value less than zero if x is less than y, zero if x is equal to y, or a
             // value greater than zero if x is greater than y.
             int Compare(Object x, Object y);
        }
     
    4、私有成员变量前加前缀m_或_:
     
     
      unsafe internal class PathHelper 
      {   
            // maximum size, max be greater than max path if contains escape sequence
               private int m_capacity;
               // current length (next character position)
               private int m_length;
               // max path, may be less than capacity
               private int _maxPath;
          ....
      } 
    5、常量使用pascal或全部大写表示:
     
        private const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
        private const int FORMAT_MESSAGE_FROM_SYSTEM    = 0x00001000;
        private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;
      
        const string RsaKeyValue  = XmlSignatureConstantsNamespace + "RSAKeyValue";
     6、自定义特性类后加后缀Attribute:
     
      [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
        [ComVisible(true)]
        public sealed class DebuggerVisualizerAttribute: Attribute
     7、自定义异常类加后缀Exception:
     
      [Serializable]
        public class ArgumentException : SystemException, ISerializable
     8、方法命名使用“动词/对象”对,如 GetHashCode(...), 
     
      public unsafe static Array CreateInstance(Type elementType, int length)
     
     9、有返回值的方法名中需要描述此返回值,如GetObjectState()。
     
    10、使用描述性变量名。
     
      a.避免使用单个字母作为变量名,如i或t,用index或temp来代替。
     
      b.避免对公共成员或受保护成员使用匈牙利标记法。
     
          c.避免使用缩写(如把number写成num)。
     
    11、不要混淆使用c#中的预定义类型与CLR中的类型:
     
           如object 与Object, string与String,int与Int32
     
    12、对于泛型,使用该类型的首字母代替,当使用.NET的Type类型时才使用Type作为后缀:
     
      public class Dictionary<K,V>
     
      {...}
     
    13、使用有意见的命名空间名称,如产品名或公司名。
     
    14、避免使用完全限定类型名。用using语句代替。
     
    15、把所有的framework命名空间一起放在最上面,把自定义放在最下面,第三方命名空间放中间: 
     
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using ThreadPartyLibrary;
    using MyCompanyName;
    16、采用委托推断,不要显示实例化委托:
     
    delegate void SomeDelegate();
    public void SomeMethod()
    {...}
    SomeDelegate someDelegate = SomeMethod;
    17、使用Tab来进行缩进。
     
    18、所有的成员变量都必须在顶部声明,用一行把它们与属性或方法隔开。
     
    19、定义局部变量时,尽量使它靠近第一次使用它的地方。
     
    20、文件名能够反应它使用的类。
     
    21、使用左大括号({)时,换一行。
     
    22、if下面即使只有一行代码,也要加{}包围起来:
     
    23、为每一个命名空间建一个目录。如 MyProject.TestSuite.TestTier 使用MyProject/TestSuite/TestTier 作为路径。
     
    24、当一行中的代码太长时,建议使用下面的方式进行断行:
     
    Break after a comma----逗号后面
    Break after an operator----操作符后面
    Prefer higher-level breaks to lower-level breaks----
    Align the new line with the beginning of the expression at the same level on the previous line.
       如 longMethodCall(expr1, expr2,
     
                     expr3, expr4, expr5);
     
     Examples of breaking an arithmetic expression:
     
     PREFER:
     
     
     var = a * b / (c - g + f) +
     
             4 * z;
     
      var = a * b / (c - g + f) +
     
           4 * z;
     
     BAD STYLE – AVOID:
     
     var = a * b / (c - g +
     
              f) + 4 * z;
     
    25、声明变量时就进行初始化。
     
    26、从 Stream 继承的 Framework 类型以 Stream 结尾:
     
      如FileStream, MemoryStream等。
     
    27、当不理解一个方法时,可以把它们分解为更小部分,把为它们取恰当名称。
  • 相关阅读:
    三元组顺序结构实现稀疏矩阵相加,行序优先(Java语言描述)
    行逻辑链接的顺序表实现稀疏矩阵的相乘(Java语言描述)
    各种矩阵压缩原理(书本上没有讲到的具体公式推理过程)
    KMP算法的实现(Java语言描述)
    链表一元多项式计算器的实现(Java语言描述)
    链表的实现(Java语言描述)
    队列的链式存储方式的实现(Java语言描述)
    Java泛型数组
    三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述)
    利用栈实现算术表达式求值(Java语言描述)
  • 原文地址:https://www.cnblogs.com/gc2013/p/4223139.html
Copyright © 2011-2022 走看看