IDesign C#编程规范(一)

类别:.NET开发 点击:0 评论:0 推荐:
 idesign发布了c#编程规范,小鸡射手从only4gurus下载浏览后决心抽时间翻译一下,以更好地学习。

   目录内容如下:

     1  命名规则和风格 naming conventions and style
     2  编码惯例 coding practices
     3  项目设置和结构 project settings and structure
     4  framework特别指导 framework specific guidelines
        4.1  数据访问 data access
        4.2  asp.net和web service asp.net and web services
        4.3  序列化 serialization
        4.4  多线程 multithreading
        4.5  remoting remoting
        4.6  安全 security
        4.7  服务组件 enterprise services
     5  资源 resources

      今天只翻译了命名规则部分,译文及原文对照如下,其中的tip是附加的,:-)

 

命名规则和风格 
naming conventions and style

1.  类和方法名采用pascal风格
    use pascal casing for type and method names
    public class someclass
    {
       public somemethod(){}
    }
2.  局部变量和方法参数采用camel风格
    use camel casing for local variable names and method arguments
    int number;
    void mymethod(int somenumber)
    {}
3.  接口名采用i作为前缀
    prefix interface name with i 
    interface imyinterface
    {..}
4.  私有成员变量采用m_作为前缀
    prefix private member variables with m_
    public class someclass
    {
       private int m_number;
    }
5.  自定义属性类名采用attribute作为后缀
    suffix custom attribute classes with attribute. 
6.  自定义异常类名采用exception作为后缀
    suffix custom exception classes with exception.
7.  采用动词-对象对命名方法,例如showdialog()
    name methods using verb-object pair, such as showdialog()
8.  有返回值的方法应该取名表示其返回值,例如getobjectstate()
    methods with return values should have a name describing the value returned, such as getobjectstate().
9.  采用描述性的变量名。
    use descriptive variable names.
    a) 避免采用单字母的变量名,如i或t;而是采用index或temp。
       avoid  single character variable names, such as i or  t. use  index or temp instead. 
    b) 对public和protected成员避免采用用匈牙利命名法
       avoid using hungarian notation for public or protected members. 
    c) 不要采用缩写(例如将number缩写为num)。
       do not abbreviate words (such as num instead of number).
10.  总是使用c#预定义的类型,而不是使用system命名空间中的别名。例如:采用object不用object,采用string不用string,采用int不用int32。
     always use c# predefined types rather than the aliases in the  system namespace.
     for example: 
     object not object
     string not string
     int    not int32
11.  对于泛型,类型采用大写字母。当处理.net类型type时保留后缀type。
     with generics,  use capital letters for types. reserve suffixing type when dealing with the .net type type.
     // 正确:
     //correct:
     public class linkedlist
     // 避免使用:
     //avoid: 
     public class linkedlist 
12.  采用有意义的命名空间名,例如产品名称或公司名称。
     use meaningful namespaces such as the product name or the company name.
13.  避免使用类的全称,而是采用using语句。
     avoid fully qualified type names. use the using statement instead. 
14.  避免在命名空间内使用using语句。
     avoid putting a using statement inside a namespace.
15.  将所有framework命名空间名放在一起,后面放自定义或第三方的命名空间名。
     group all framework namespaces together and put custom or third party namespaces underneath.
     using system;
     using system.collections;
     using system.componentmodel;
     using system.data;
     using mycompany;
     using mycontrols;
16.  采用委托推断,不要显式实例化委托。
     use delegate inference instead of explicit delegate instantiation 
     delegate void somedelegate();
     public void somemethod()
     {}
     somedelegate somedelegate = somemethod;
17.  严格遵守缩进格式。
     maintain strict indentation.
     a) 缩进采用3个空格。
        use 3 spaces for indentation.
     b) 不用采用tab或非标准的缩进,如1、2或4个空格。
        do not use tabs or non-standard indentation like 1, 2 or 4 spaces. 
18.  注释缩进和其注释的代码在同一层次。
       indent comment at the same level of indentation as the code you are documenting.
19.  所有注释要经过拼写检查。拼写错误的注释表明开发的草率。
      all comments should pass spell checking. misspelled comments indicate sloppy development. 
20.  所有成员变量应该定义在前面,和属性或方法间空开一行。
      all member variables should be declared at the top, with one line separating them from the properties or methods. 
     public class myclass
     {
        int m_number;
        string m_name;

        public void somemethod1()
        {}
        public void somemethod2()
        {}
     }
21.  局部变量的定义尽可能靠近它的初次使用。
       declare a local variable as close as possible to its first use.
22.  文件名应该体现其包含的类。
       a file name should reflect the class it contains.
23.  当使用partial类型且每部分分配一个文件时,以类型名加p和序数命名每个文件。
       when using partial types and allocating a part per file, name each file after the type suffixed with a p and an ordinal number:
     //in myclassp1.cs
     public partial class myclass
     {}
     //in myclassp2.cs
     public partial class myclass
     {}
24.  左大括号总是放在新行中。
       always place an open curly brace ({) in a new line.
25.  匿名方法模仿普通方法的布局,和匿名委托定义放在一行。
       with anonymous methods mimic the code layout of a regular method, aligned with the anonymous delegate declaration. 
     a) 遵守将左大括号放在新行的规则。
         comply with placing an open curly brace in a new line
     delegate void somedelegate(string somestring);
     //正确
     //correct: 
     public void invokemethod()
     {
        somedelegate somedelegate = delegate(string name)
                                    {
                                       messagebox.show(name);
                                    };
        somedelegate("juval");
     }
     //避免采用:
     //avoid
     public void invokemethod()
     {
        somedelegate somedelegate = delegate(string name){messagebox.show(name);};
        somedelegate("juval");
     }
26.  没有参数的匿名方法使用空括号。
       use empty parenthesis on parameter-less anonymous methods
     a) 仅当匿名方法可能被用于任何委托时省略括号。
         omit the parenthesis only if the anonymous method could have been used on any delegate.
     delegate void somedelegate();
     //correct 
     somedelegate somedelegate1 = delegate()
                                  {
                                     messagebox.show("hello");
                                  };
     //avoid 
     somedelegate somedelegate1 = delegate
                                  {
                                     messagebox.show("hello");
                                  };

本文地址:http://com.8s8s.com/it/it43869.htm