Difference between revisions of "Coding guideline - Common rules"

From Catglobe Wiki
Jump to: navigation, search
(Attribute declaration)
 
(17 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
<accesscontrol>Main:MyGroup</accesscontrol>
 
== Introduction ==
 
== Introduction ==
  
Line 50: Line 51:
  
 
''<u>Should</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 
''<u>Should</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    public sealed class CatTask : PersistentObject, ICatTaskClientObject, IComparable  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">public </font><font color="#0000ff">sealed </font><font color="#0000ff">class </font><font color="#2b91af">CatTask</font> : <font color="#2b91af">PersistentObject</font>, <font color="#2b91af">ICatTaskClientObject</font>, <font color="#2b91af">IComparable</font></font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        private sealed class CatTasksAccess : AccessBase  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private </font><font color="#0000ff">sealed </font><font color="#0000ff">class </font><font color="#2b91af">CatTasksAccess</font> : <font color="#2b91af">AccessBase</font></font>
        {  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{</font>
        }  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
''<u>Should not</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 
''<u>Should not</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
 
    public sealed class CatTask : PersistentObject, ICatTaskClientObject, IComparable
 
    {
 
    }
 
  
    [Serializable]  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">public </font><font color="#0000ff">sealed </font><font color="#0000ff">class </font><font color="#2b91af">CatTask</font> : <font color="#2b91af">PersistentObject</font>, <font color="#2b91af">ICatTaskClientObject</font>, <font color="#2b91af">IComparable</font></font>
    public sealed class StringConstant  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{</font>
    {  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
    }  
+
 
</source>
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">[<font color="#2b91af">Serializable</font>]</font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">public </font><font color="#0000ff">sealed </font><font color="#0000ff">class </font><font color="#2b91af">StringConstant</font></font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{</font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
 +
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
*Classes member should be grouped into sections (Fields, Constructors, Properties, Events, Methods, Private interface implementations, Nested types). And there should be a region name in <font color="#0047ff">''#endregion''</font> instructive although it is not required. Regions can be shown/hide while viewing with VS thus increasing the readability.
 
*Classes member should be grouped into sections (Fields, Constructors, Properties, Events, Methods, Private interface implementations, Nested types). And there should be a region name in <font color="#0047ff">''#endregion''</font> instructive although it is not required. Regions can be shown/hide while viewing with VS thus increasing the readability.
  
For example:  
+
For example: <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    public sealed class CatTask : PersistentObject, ICatTaskClientObject, IComparable  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">public </font><font color="#0000ff">sealed </font><font color="#0000ff">class </font><font color="#2b91af">CatTask</font> : <font color="#2b91af">PersistentObject</font>, <font color="#2b91af">ICatTaskClientObject</font>, <font color="#2b91af">IComparable</font></font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        #region AccessClass  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#region </font>AccessClass</font>
 +
 
 +
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private </font><font color="#0000ff">sealed </font><font color="#0000ff">class </font><font color="#2b91af">CatTasksAccess</font> : <font color="#2b91af">AccessBase</font></font>
 +
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{</font>
 +
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
 +
 
 +
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#endregion </font>AccessClass</font>
  
        private sealed class CatTasksAccess : AccessBase
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#region </font>Private Instance Fields</font>
        {
 
        }
 
  
        #endregion AccessClass
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private int</font> resourceId;</font>
 +
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private int</font> resourceTemplateId;</font>
  
        #region Private Instance Fields  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#endregion </font>Private Instance Fields</font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
  
        private int resourceId;
+
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
        private int resourceTemplateId;  
 
  
        #endregion Private Instance Fields
+
''<u>A propose class file with regions:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
    }
 
</source>
 
  
''<u>A propose class file with regions:</u>''
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">public </font><font color="#0000ff">class </font><font color="#2b91af">SampleClass</font></font>
<source lang="csharp">
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
    public class SampleClass  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#region </font>Nested Enums, Structs, and Classes</font>
    {
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#endregion </font>Nested Enums, Structs, and Classes</font>
        #region Nested Enums, Structs, and Classes  
 
        #endregion Nested Enums, Structs, and Classes  
 
  
        #region Constructors & Finalizers  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#region </font>Constructors &amp; Finalizers</font>
        #endregion Constructors & Finalizers  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#endregion </font>Constructors &amp; Finalizers</font>
  
        #region Member variables  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#region </font>Member variables</font>
        #endregion Member variables  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#endregion </font>Member variables</font>
  
        #region roperties  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#region </font>roperties</font>
        #endregion Properties  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#endregion </font>Properties</font>
  
        #region Methods  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#region </font>Methods</font>
        #endregion Methods  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">#endregion </font>Methods</font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
*There should not be more than ''<u>500 lines of code</u>'' inside one file. Files containing more than 500 lines have less readability than the ones with less than 500 lines. There is one exception that we should not take into account on this rules, that are auto-generated files since most of the time we do not maintain these files directly.  
 
*There should not be more than ''<u>500 lines of code</u>'' inside one file. Files containing more than 500 lines have less readability than the ones with less than 500 lines. There is one exception that we should not take into account on this rules, that are auto-generated files since most of the time we do not maintain these files directly.  
Line 161: Line 166:
 
*Open braces must always be at the beginning of the line after the statement that begins the block. This increases readability of code.
 
*Open braces must always be at the beginning of the line after the statement that begins the block. This increases readability of code.
  
''<u>Must:</u>''  
+
''<u>Must:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    if ((CatTaskStatus.Status == CatTaskCommon.CatTaskStatus.Failed)  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">if </font>((CatTaskStatus.Status == CatTaskCommon.<font color="#2b91af">CatTaskStatus</font>.Failed)</font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        CatTaskCompleted ctc = CatTaskCompleted.GetByLatestCompletedDate(this);  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#2b91af">CatTaskCompleted</font> ctc = <font color="#2b91af">CatTaskCompleted</font>.GetByLatestCompletedDate(<font color="#0000ff">this</font>);</font>
        if (ctc != null)
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">if </font> (ctc&nbsp;!= <font color="#0000ff">null</font>) </font>
            completedDate = ctc.CompletedDate;  
+
:::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">completedDate = ctc.CompletedDate;</font>
 +
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">return </font>completedDate;</font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">else</font></font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
 +
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">...</font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
  
        return completedDate;  
+
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
    }
 
    else
 
    {
 
        ...
 
    }
 
</source>
 
  
 
''<u>Must not:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 
''<u>Must not:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    if ((CatTaskStatus.Status == CatTaskCommon.CatTaskStatus.Failed) { => Wrong brace location
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">if </font>((CatTaskStatus.Status == CatTaskCommon.<font color="#2b91af">CatTaskStatus</font>.Failed) <font color="#ff0000">'''{'''</font><font color="#ff0000"><font face="Arial, sans-serif">'''=&gt; Wrong brace's location'''</font></font> </font>
        CatTaskCompleted ctc = CatTaskCompleted.GetByLatestCompletedDate(this);  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#2b91af">CatTaskCompleted</font> ctc = <font color="#2b91af">CatTaskCompleted</font>.GetByLatestCompletedDate(<font color="#0000ff">this</font>);</font>
        if (ctc != null) {
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">if </font>(ctc&nbsp;!= <font color="#0000ff">null</font>) <font color="#ff0000">'''{'''</font> </font>
            completedDate = ctc.CompletedDate;  
+
:::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">completedDate = ctc.CompletedDate;</font>
        }  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
        return completedDate;  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">return </font> completedDate;</font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
*For anonymous methods and lambda expressions, the braces must be indent at the same level with its declaration.
 
*For anonymous methods and lambda expressions, the braces must be indent at the same level with its declaration.
  
''<u>Example:</u>''  
+
''<u>Example:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
 
    private  SampleDelegate _mySampleDelegate = delegate()
 
                                                {
 
                                                };  
 
  
    private SampleDelegate _mySampleDelegate = (name) =>
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private </font> SampleDelegate _mySampleDelegate = <font color="#0000ff">delegate</font>() </font>
                                              {  
+
::::::::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">&nbsp;&nbsp;&nbsp;'''{'''</font>
                                              };  
+
::::::::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">&nbsp;&nbsp;&nbsp;'''};'''</font>
</source>
+
 
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private </font> SampleDelegate _mySampleDelegate = (name) =&gt; </font>
 +
::::::::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">&nbsp;&nbsp;&nbsp;'''{'''</font>
 +
::::::::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">&nbsp;&nbsp;&nbsp;'''}''';</font>
 +
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
*Must not put block that contains more than 2 statements in the same line:
 
*Must not put block that contains more than 2 statements in the same line:
  
''<u>Must not</u>'':  
+
''<u>Must not</u>'': <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
if (ctc != null) {completedDate = ctc.CompletedDate; return completedDate; }
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">if </font> (ctc&nbsp;!= <font color="#0000ff">null</font>) {<font color="#0000ff"></font>completedDate = ctc.CompletedDate; <font color="#0000ff">return</font> completedDate; }</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
*With single statement blocks, using of braces is optionnal. It is encourage to not use the braces to save number of line of code.
 
*With single statement blocks, using of braces is optionnal. It is encourage to not use the braces to save number of line of code.
  
''<u>Should:</u>''  
+
''<u>Should:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    if (ctc != null)
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">if </font> (ctc&nbsp;!= <font color="#0000ff">null</font>) </font>
        completedDate = ctc.CompletedDate;  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">completedDate = ctc.CompletedDate;</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
''<u>Should not:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 
''<u>Should not:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    if (ctc != null)  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">if </font> (ctc&nbsp;!= <font color="#0000ff">null</font>)</font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#c5000b">{</font> </font>
        completedDate = ctc.CompletedDate; => 3 lines of code do the same as 1 line  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#c5000b">completedDate = ctc.CompletedDate;</font> <font color="#ff0000"><font face="Arial, sans-serif">'''=&gt; 3 lines of code do the same as 1 line'''</font></font></font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#c5000b">}</font></font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
*There are exceptions that brace should be placed on the same line as the keyword that requires braces.
 
*There are exceptions that brace should be placed on the same line as the keyword that requires braces.
  
 
''<u>One statement code block:</u>''  
 
''<u>One statement code block:</u>''  
<source lang="csharp">
+
 
    public int CompletedMailTemplateId  
+
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
    {
+
 
        get { return completedMailTemplateId; } => It is much nicer than having 4 lines of code  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">public </font><font color="#0000ff">int </font> CompletedMailTemplateId</font>
        set { completedMailTemplateId = value; }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
    }  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">get</font> { <font color="#0000ff">return </font> completedMailTemplateId; } <font color="#ff0000"><font face="Arial, sans-serif">'''=&gt; It is much nicer than having 4 lines of code'''</font></font></font>
</source>
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">set</font> { completedMailTemplateId = <font color="#0000ff">value</font>; }</font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
 +
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
''<u>.NET 3.0 property syntax:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 
''<u>.NET 3.0 property syntax:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
 
    public int MyProperty { get; set; }
 
</source>
 
  
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">public </font><font color="#0000ff">int </font>MyProperty { <font color="#0000ff">get</font>; <font color="#0000ff">set</font>; }</font>
  
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 
<div style="border: 1pt solid rgb(0, 0, 0); padding: 5px; background: rgb(192, 192, 192) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(0, 0, 255);">
 
<div style="border: 1pt solid rgb(0, 0, 0); padding: 5px; background: rgb(192, 192, 192) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(0, 0, 255);">
 
'''Tips:''' <br> All rules in this section can be applied automatically by applying a common ''Visual Studio settings file''. Developers just have to remember to apply these rules by pressing ''Ctrl + K + D'' (under default installation) or ''Ctrl + E + C ''using Resharper before committing.  
 
'''Tips:''' <br> All rules in this section can be applied automatically by applying a common ''Visual Studio settings file''. Developers just have to remember to apply these rules by pressing ''Ctrl + K + D'' (under default installation) or ''Ctrl + E + C ''using Resharper before committing.  
</div>
+
</div>  
 
 
 
=== Commenting  ===
 
=== Commenting  ===
  
Line 793: Line 804:
 
*For elements that have more than one Attribute declaration, then each <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">Attribute</font> must be put on one line.
 
*For elements that have more than one Attribute declaration, then each <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">Attribute</font> must be put on one line.
  
''<u>Must not:</u>''  
+
''<u>Must not:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    [Serializable, STAThread, Obsolete("This class is obsolete. Use class Abc instead.")]  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">[<font color="#2b91af">Serializable</font>, <font color="#2b91af">STAThread</font>, <font color="#2b91af">Obsolete</font>(<font color="#a31515">"This class is obsolete. Use class Abc instead."</font>)]</font>
    public class SampleClass  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">public </font><font color="#0000ff">class </font><font color="#2b91af">SampleClass</font></font>
</source>
 
  
''<u>Must:</u>''  
+
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    [Serializable]  
+
''<u>Must:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
    [STAThread]  
+
 
    [Obsolete("This class is obsolete. Use class Abc instead.")]  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">[<font color="#2b91af">Serializable</font>]</font>
    public class SampleClass  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">[<font color="#2b91af">STAThread</font>]</font>
</source>
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">[<font color="#2b91af">Obsolete</font>(<font color="#a31515">"This class is obsolete. Use class Abc instead."</font>)]</font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">public </font><font color="#0000ff">class </font><font color="#2b91af">SampleClass</font></font>
 +
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
== Coding guidelines ==
 
== Coding guidelines ==
Line 815: Line 828:
 
*All access modifier must be explicitly given, except in interface's declaration where <font color="#0000ff"><font face="Courier New, monospace">public</font></font> is undertood by default.
 
*All access modifier must be explicitly given, except in interface's declaration where <font color="#0000ff"><font face="Courier New, monospace">public</font></font> is undertood by default.
  
''<u>Must not:</u>''  
+
''<u>Must not:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    class SampleClass  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">class </font><font color="#2b91af">SampleClass</font></font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        int sampleInt;  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">int </font>sampleInt;</font>
        void SampleMethod()  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">void </font>SampleMethod()</font>
        {  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{</font>
        }  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
==== Variables and Types ====
 
==== Variables and Types ====
Line 830: Line 844:
 
*If possible, initialize variables at declaration time.
 
*If possible, initialize variables at declaration time.
  
''<u>Should:</u>''  
+
''<u>Should:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    private int primaryId = 0;  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private </font><font color="#0000ff">int </font>primaryId = 0;</font>
    private string sampleString = string.Empty;  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private </font><font color="#0000ff">string </font>sampleString = <font color="#0000ff">string</font>.Empty;</font>
    private List<int> sampleList = new List<int>();  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private </font><font color="#2b91af">List</font>&lt;<font color="#0000ff">int</font>&gt; sampleList = <font color="#0000ff">new</font> <font color="#2b91af">List</font>&lt;<font color="#0000ff">int</font>&gt;();</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
*Must use fully qualified name of types, declare the type by using statements. In case there are duplication in type's name, define aliases.
 
*Must use fully qualified name of types, declare the type by using statements. In case there are duplication in type's name, define aliases.
  
''<u>Must not:</u>''  
+
''<u>Must not:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
 
    ResourceCollectionPager pager = CatGlobe.Framework.Services.SessionManager.Current.DataModule.Get<ResourceCollectionPager>(Request["sessionid"]);
 
</source>
 
  
''<u>Must:</u>''
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#2b91af">ResourceCollectionPager</font> pager = CatGlobe.Framework.Services.<font color="#2b91af">SessionManager</font>.Current.DataModule.Get&lt;<font color="#2b91af">ResourceCollectionPager</font>&gt;(Request[<font color="#a31515">"sessionid"</font>]);</font>
<source lang="csharp">
 
    using CatGlobe.Framework.Services;
 
    ResourceCollectionPager pager = SessionManager.Current.DataModule.Get<ResourceCollectionPager>(Request["sessionid"]);  
 
</source>
 
  
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
  
 +
''<u>Must:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 +
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">using</font> CatGlobe.Framework.Services;</font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#2b91af">ResourceCollectionPager</font> pager = <font color="#2b91af">SessionManager</font>.Current.DataModule.Get&lt;<font color="#2b91af">ResourceCollectionPager</font>&gt;(Request[<font color="#a31515">"sessionid"</font>]);</font>
 +
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 
<div style="border: 1pt solid rgb(0, 0, 0); padding: 5px; background: rgb(192, 192, 192) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(0, 0, 255);">
 
<div style="border: 1pt solid rgb(0, 0, 0); padding: 5px; background: rgb(192, 192, 192) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(0, 0, 255);">
 
'''Tips:''' <br> Always choose “using” over fully qualified name from Resolve menu of Visual Studio. [[Image:Coding Guidelines Common rules 1.0 html m6738e672.png|center|400px|Resolve menu of VS2008]]  
 
'''Tips:''' <br> Always choose “using” over fully qualified name from Resolve menu of Visual Studio. [[Image:Coding Guidelines Common rules 1.0 html m6738e672.png|center|400px|Resolve menu of VS2008]]  
 
</div>  
 
</div>  
''<u>Example of using alias:</u>''  
+
''<u>Example of using alias:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    using Threading = System.Threading;  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">using </font>Threading = System.Threading;</font>
    using Timers = System.Timers;  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">using </font>Timers = System.Timers;</font>
    using Web = System.Web.UI;  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">using </font>Web = System.Web.UI;</font>
  
    namespaceCatGlobe.DataAccess  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">namespace</font>CatGlobe.DataAccess</font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        public class SampleClass  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">public </font><font color="#0000ff">class </font><font color="#2b91af">SampleClass</font></font>
        {
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
            private Threading.Timer _threadTimer1;  
+
:::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private </font>Threading.<font color="#2b91af">Timer</font> _threadTimer1;</font>
            private Timers.Timer _threadTimer2;  
+
:::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private </font>Timers.<font color="#2b91af">Timer</font> _threadTimer2;</font>
            private Web.Timer _threadTimer3;  
+
:::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">private </font>Web.<font color="#2b91af">Timer</font> _threadTimer3;</font>
        }  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
==== Constants and Literals ====
 
==== Constants and Literals ====
Line 875: Line 891:
 
*Never hardcoded strings that will be presented to end-users. Use <font face="Courier New, monospace" size="2" color="#33a3a3" style="font-size: 9pt;">Resoures</font> class instead.
 
*Never hardcoded strings that will be presented to end-users. Use <font face="Courier New, monospace" size="2" color="#33a3a3" style="font-size: 9pt;">Resoures</font> class instead.
  
''<u>Must not</u>'':  
+
''<u>Must not</u>'': <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="xml">
+
 
    <span>Username</span>  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">&lt;</font><font color="#a31515">span</font><font color="#0000ff">&gt;</font>Username<font color="#0000ff">&lt;/</font><font color="#a31515">span</font><font color="#0000ff">&gt;</font></font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 +
 
 +
''<u>Must</u>'': <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
  
''<u>Must</u>'':
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">&lt;</font><font color="#a31515">span</font><font color="#0000ff">&gt;</font>&lt;%<font color="#0000ff">=</font>Resources.Username&nbsp;%&gt;<font color="#0000ff">&lt;/</font><font color="#a31515">span</font><font color="#0000ff">&gt;</font></font>
<source lang="xml">
+
 
    <span><%=Resources.Username %></span>  
+
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
</source>
 
  
 
*Try to use @ instead escape characters symbol
 
*Try to use @ instead escape characters symbol
  
 
''<u>Should:</u>''  
 
''<u>Should:</u>''  
<source lang="csharp">
+
 
    string filename = @"D:\test\test.xml";
+
:<font color="#0000ff">string </font>filename = <font color="#a31515">@"D:\test\test.xml"</font>;
</source>
 
  
 
==== Direct cast vs. <font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">as</font> keyword ====
 
==== Direct cast vs. <font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">as</font> keyword ====
  
Following code block:  
+
Following code block: <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    CustomClass a = src as CustomClass;
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#2b91af">CustomClass</font> a = src <font color="#0000ff">as </font><font color="#2b91af">CustomClass</font>;</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
can be considered the same as the code block below: <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 
can be considered the same as the code block below: <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
 
    if (src is CustomClass)
 
        return (CustomClass)src;
 
    else
 
        return null;
 
</source>
 
  
Consequently, it is encouraged to use <font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">as</font> keyword over directcasting, because it is neat and safer.
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">if </font>(src <font color="#0000ff">is</font> <font color="#2b91af">CustomClass</font>) </font>
 +
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">return </font>(<font color="#2b91af">CustomClass</font>)src;</font>
 +
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">else </font>
 +
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">return </font><font color="#0000ff">null</font>;</font>
 +
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 +
 
 +
Consequently, it is encouraged to use <font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">as</font> keyword over directcasting, because it is neat and safer.  
  
 
=== Exceptions handling ===
 
=== Exceptions handling ===
Line 913: Line 932:
 
*Catch least generic exception first
 
*Catch least generic exception first
  
''<u>Must not:</u>''  
+
''<u>Must not:</u>'' <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    try  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">try</font></font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        using (FileStream fs = newFileStream(@"D:\test.xml")) { }  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">using </font>(<font color="#2b91af">FileStream</font> fs = <font color="#0000ff">new</font><font color="#2b91af">FileStream</font>(<font color="#a31515">@"</font>D:\test.xml<font color="#a31515">"</font>)) { }</font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
    catch (Exception) { }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">catch </font> (<font color="#2b91af">Exception</font>) { }</font>
    catch (IOException)  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">catch </font> (<font color="#2b91af">IOException</font>)</font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        =>Unreachable code  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font face="Arial" color="#ff0000">=&gt;Unreachable code</font></font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
*Do not use exception for controlling flows, instead try to validate input parameter. Most of the time exceptions can be prevented by proper input validation.
 
*Do not use exception for controlling flows, instead try to validate input parameter. Most of the time exceptions can be prevented by proper input validation.
  
 
''<u>Should not</u>'': <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 
''<u>Should not</u>'': <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    string input = "something";  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">string </font>input = <font color="#a31515">"something"</font>;</font>
    try  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">try</font></font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        DoWork(input);  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">DoWork(input);</font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
    catch (Exception e)  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">catch </font>(<font color="#2b91af">Exception</font> e)</font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        DoAlternateWork(input);  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">DoAlternateWork(input);</font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
*All exception must be re-thrown, the outest layer (usually GUI win-form or web-form) must responsible for handling the exception properly.
 
*All exception must be re-thrown, the outest layer (usually GUI win-form or web-form) must responsible for handling the exception properly.
  
''<u>Must not</u>'':  
+
''<u>Must not</u>'': <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    try  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">try</font></font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        DoWork();  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">DoWork();</font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
    catch (Exception e)  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">catch </font>(<font color="#2b91af">Exception</font> e)</font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        // do nothing  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#008000">// do nothing</font></font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
*If not creating a wrapper exception, use “<font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">throw</font>” instead of “<font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">throw</font><font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">e</font>” to reserve the call-stack. “<font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">throw</font>” statement's stack trace points to the code place that actually causes the error, while “<font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">throw</font><font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">e</font>”'s stack trace points to the location of the “<font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">throw</font><font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">e</font>” statement only.
 
*If not creating a wrapper exception, use “<font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">throw</font>” instead of “<font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">throw</font><font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">e</font>” to reserve the call-stack. “<font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">throw</font>” statement's stack trace points to the code place that actually causes the error, while “<font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">throw</font><font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">e</font>”'s stack trace points to the location of the “<font face="Courier New, monospace" size="2" color="#0000ff" style="font-size: 9pt;">throw</font><font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">e</font>” statement only.
  
''<u>Must not</u>'':  
+
''<u>Must not</u>'': <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
<source lang="csharp">
+
 
    catch (IOException ioe)  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">catch </font>(<font color="#2b91af">IOException</font> ioe)</font>
    {
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
        Logger.LogError(ioe);  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#2b91af">Logger</font>.LogError(ioe);</font>
        throw ioe; => Must be throw;  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">throw </font>ioe; <font color="#ff0000"><font face="Arial, sans-serif">=&gt; Must be</font> throw;</font></font>
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
</source>
+
 
 +
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
 +
 
 +
''<u>Correct</u>'': <font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>
  
''<u>Correct</u>'':
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">catch </font>(<font color="#2b91af">IOException</font> ioe)</font>
<source lang="csharp">
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
    catch (IOException ioe)  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#2b91af">Logger</font>.LogError(ioe);</font>
    {
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">throw </font><font color="#0000ff">new </font><font color="#2b91af">CustomeException</font>(<font color="#a31515">"error"</font>, ioe);</font>
        Logger.LogError(ioe);  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
        throw new CustomeException("error", ioe);  
+
 
    }  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">catch </font>(<font color="#2b91af">IOException</font> ioe)</font>
    catch (IOException ioe)  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">{ </font>
    {
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#2b91af">Logger</font>.LogError(ioe);</font>
        Logger.LogError(ioe);  
+
::<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"><font color="#0000ff">throw</font>;</font>
        throw;  
+
:<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;">}</font>
    }  
+
 
</source>
+
<font face="Courier New, monospace" size="2" color="#000000" style="font-size: 9pt;"></font>  
  
 
== Document revisions ==
 
== Document revisions ==

Latest revision as of 05:39, 18 October 2013

<accesscontrol>Main:MyGroup</accesscontrol>

Introduction

This document aims at giving common rules that developers MUST follow to ensure not only consistency in the whole application but also the ease of code reading and maintaining. Due to the fact that we are working mainly on C# language, all examples will be given in this language only.

Please refer to the .NET Framework Design Guidelines (http://msdn.microsoft.com/en-us/library/czefa0ke.aspx) for a complete set of guidelines suggested by Microsoft.

Terms and Conventions

Naming Terms

There are 3 terms used for naming files, class names, variable names, etc. All are listed in this section.

Pascal case

The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. You can use Pascal case for identifiers of three or more characters.

For example: CatGlobeImageButton, LanguageEditor

Camel case

The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.

For example: resourceId, resourceTemplateId

Uppercase All letters in the identifier are capitalized. Use this convention only for identifiers that consist of two or fewer letters.

For example: ID, UI, IO

Style Guidelines

File Organization

Location

  • Class file should be put in directory whose path follows class's namespace. This makes finding a class easier.

For example: code file of class CatGlobe.Web.UI.WebControls.CatGlobeImageButton should be found in side {CODE_BASE}\CatGlobe\Web\UI\WebControls

Naming

  • Always use Pascal for naming code file.
  • Source files should be given the name of the first public class in the file plus “.cs”. The purpose of this naming is clear, it increases the consistent of organization and also the maintainability.

For example: code file {CODE_BASE}\CatGlobe\Web\UI\WebControls\CatGlobeImageButton.cs should have the first public class as CatGlobeImageButton

Content

  • There should be only one namespace declaration in one code file only.
  • One source file should contain only one public class/interface/enum, although multiple internal classes are allowed. Delegates and Interfaces are allowed to be put in the same file as class/interface/enum.

Should

public sealed class CatTask : PersistentObject, ICatTaskClientObject, IComparable
{
private sealed class CatTasksAccess : AccessBase
{
}
}

Should not

public sealed class CatTask : PersistentObject, ICatTaskClientObject, IComparable
{
}
[Serializable]
public sealed class StringConstant
{
}

  • Classes member should be grouped into sections (Fields, Constructors, Properties, Events, Methods, Private interface implementations, Nested types). And there should be a region name in #endregion instructive although it is not required. Regions can be shown/hide while viewing with VS thus increasing the readability.

For example:

public sealed class CatTask : PersistentObject, ICatTaskClientObject, IComparable
{
#region AccessClass
private sealed class CatTasksAccess : AccessBase
{
}
#endregion AccessClass
#region Private Instance Fields
private int resourceId;
private int resourceTemplateId;
#endregion Private Instance Fields
}

A propose class file with regions:

public class SampleClass
{
#region Nested Enums, Structs, and Classes
#endregion Nested Enums, Structs, and Classes
#region Constructors & Finalizers
#endregion Constructors & Finalizers
#region Member variables
#endregion Member variables
#region roperties
#endregion Properties
#region Methods
#endregion Methods
}

  • There should not be more than 500 lines of code inside one file. Files containing more than 500 lines have less readability than the ones with less than 500 lines. There is one exception that we should not take into account on this rules, that are auto-generated files since most of the time we do not maintain these files directly.
  • Each line of code should not exceed 120 characters. Lines with more than 120 characters normally require code viewer using the horizontal scrollbar thus decrease readability.

Tabs, Indenting, Spacing and Bracing

Tabs and Indenting

  • Tab characters (\0x09) MUST NOT be used in code. All tabs must be replaced by 3 space characters.
  • All indentation MUST be done with 3 space characters.

Spacing Spaces improve readability by decreasing code density. Here are some guidelines for the use of space characters within code:

  • Do use a single space after a comma between function arguments.
Right:Console.In.Read(myChar,0,1);
Wrong: Console.In.Read(myChar,0,1);
  • Do not use a space after the parenthesis and function arguments.
Right: CreateFoo(myChar, 0, 1);
Wrong: CreateFoo( myChar, 0, 1 )
  • Do notuse spaces between a function name and parenthesis.
Right: CreateFoo();
Wrong: CreateFoo ()
  • Do notuse spaces inside brackets.
Right: x = dataArray[index];
Wrong: x = dataArray[ index ];
  • Do use a single space before flow control statements.
Right: while (x == y)
Wrong: while(x==y)
  • Do use a single space before and after comparison operators.
Right: if (x == y)
Wrong: if (x==y)

Bracing

  • Open braces must always be at the beginning of the line after the statement that begins the block. This increases readability of code.

Must:

if ((CatTaskStatus.Status == CatTaskCommon.CatTaskStatus.Failed)
{
CatTaskCompleted ctc = CatTaskCompleted.GetByLatestCompletedDate(this);
if (ctc != null)
completedDate = ctc.CompletedDate;
return completedDate;
}
else
{
...
}

Must not:

if ((CatTaskStatus.Status == CatTaskCommon.CatTaskStatus.Failed) {=> Wrong brace's location
CatTaskCompleted ctc = CatTaskCompleted.GetByLatestCompletedDate(this);
if (ctc != null) {
completedDate = ctc.CompletedDate;
}
return completedDate;
}

  • For anonymous methods and lambda expressions, the braces must be indent at the same level with its declaration.

Example:

private SampleDelegate _mySampleDelegate = delegate()
   {
   };
private SampleDelegate _mySampleDelegate = (name) =>
   {
   };

  • Must not put block that contains more than 2 statements in the same line:

Must not:

if (ctc != null) {completedDate = ctc.CompletedDate; return completedDate; }

  • With single statement blocks, using of braces is optionnal. It is encourage to not use the braces to save number of line of code.

Should:

if (ctc != null)
completedDate = ctc.CompletedDate;

Should not:

if (ctc != null)
{
completedDate = ctc.CompletedDate; => 3 lines of code do the same as 1 line
}

  • There are exceptions that brace should be placed on the same line as the keyword that requires braces.

One statement code block:

public int CompletedMailTemplateId
{
get { return completedMailTemplateId; } => It is much nicer than having 4 lines of code
set { completedMailTemplateId = value; }
}

.NET 3.0 property syntax:

public int MyProperty { get; set; }

Tips:
All rules in this section can be applied automatically by applying a common Visual Studio settings file. Developers just have to remember to apply these rules by pressing Ctrl + K + D (under default installation) or Ctrl + E + C using Resharper before committing.

Commenting

Comments should be used to describe intention, algorithmic overview, and/or logical flow. It would be ideal, if from reading the comments alone, someone other than the author could understand a function’s intended behavior and general operation. While there are no minimum comment requirements and certainly some very small routines need no commenting at all, it is hoped that most routines will have comments reflecting the programmer’s intent and approach.

  • Use // or /// for commenting but not /* ... */. The last comment style is out of date, it is useful in the old time when coder wanted to comment a large block of code. Using such comment will save a lot of time as writing // on each line. However, with new editor, comment/uncomment block of code is just a matter of selecting the block and calling a short cut key.
  • Do not decorate your comment with special charaters.

Must not:

// *********************************
// Comments block
// **********************************

  • Avoid comments that explain the obvious. Code should be self-explanatory. Good code with readable variable and method names should not require comments.

Should not

/// <summary>
/// Resource Id
/// </summary>
private int resourceId;

=> Comment that makes no sense. Could be something like: <The primary id of the <see cref=”CatGlobe.Domain.Common.Resource” /> that this class is working with.
  • Include Task-List keywords flags to enable comment-filtering. These comment can be tracked by using Visual Studio Task List window or just simply by searching.

Example:

// TODO: Fix this
// HACK: work-around temporarily while waiting for better solution
// UNDONE: Will be done in version xxxx

Tips:
In Visual Studio, more keywords can be defined in Tools Environment → Task List. More detail description can be found here http://msdn.microsoft.com/en-us/library/zce12xx2(VS.80).aspx

  • Always apply C# comment-blocks (///) to public, protected and internal declaration. The comment will be displayed as tooltip in editor and this helps method's user a lot for writing code correctly.

See picture below:

Class comment displayed inside help text


  • Use comment tags whenever possible, this could give very much help on generating help files. The most used tags are: <summary>, <param>, <paramref>, <return>, <exception>, <see>, <seeAlso>.

Class comment:

/// <summary>
/// This is my sample class for illustating comment on coding guide line document
/// </summary>
public class SampleClass
{
}

Method comment:

/// <summary>
/// This is my test method. It is created for demonstrating how to write comment for
/// a method.
/// </summary>
/// <param name="arg1">This is sample argument 1. It will be used for ... </param>
/// <param name="arg2">This is sample argument 2. It will be used for ...</param>
/// <returns>Return the result string.</returns>
protected string MyMethod(string arg1, string arg2)
{
}

Tips:
In Visual Studio, typing three slashes on the line right before a declaration will automatically create a template for comment on the correspondence element.

An explaination on the comment can be found here: http://msdn.microsoft.com/en-us/magazine/cc302121.aspx

A detail explaination of all comment tags can be found here: http://thoughtpad.net/alan-dean/cs-xml-documentation.

Naming

Common rules

  • Always use Camel Case or Pascal Case for naming. With private instance member, prefix variable name with an underscore '_'.

Pascal case for types (classes/interfaces/delegates/enums/structs) and methods:

public class SomeClass
{
public void SomeMethod() { }
}

Camel case for member variables and parameters:

public void MyMethod(int someNumber)
{
int _number;
}

Camel case preceding by an underscore '_' for private/protected fields:

public class SomeClass
{
private int _number;
protected string _name;
}

Pascal case for enum's values:

public enumCodeReviewResult
{
None,
Approval,
PartialDisapproval,
CompleteDisapproval,
NeedRework,
}

  • Must use Upper Case for naming constant and separate words with underscore character '_'

Example:

public const int DATABASE_CONNECTION_TIMEOUT = 500;

  • Should not create namespaces, classes, methods, properties, fields, or parameters that vary only by capitalization.

Should not:

public void MyMethod(string arg1, string Arg1)
{
}

  • Should not use names that begin with numeric characters.
  • Should not use single char variable names, except in For/While loop.
  • Always choose meaningful and specific names.
  • Should not use abbreviations unless the full name is excessive (over 20 characters). With Visual Studio IDE's IntelliSense, it is very easy finding a variable no matter how long the name is.

Should not:

public List<string> UDFList;

=> this stands for User Defined list, the name hide the purpose of the owner variable from code reader

Namespace naming

The general rule for naming namespaces is to use the company name followed by the technology name and optionally the feature and design.

Example:

namespace CatGlobe.DiffStat.Console;
namespace CatGlobe.DiffStat.WinApp;

Type naming

  • Use noun or pronoun for naming classes/interfaces.

Should not:

public class Run
{
}

Should:

public class Runner
{
}

  • MUST prefix an interface with character I.

Example:

public int erface ISomeInterface
{
}

  • MUST suffix custom attribute classes with Attribute.

Example:

public class MyCustomAttribute: System.Attribute
{
}

  • MUST suffix custom exeptions classes with Exeption.

Example:

public class MyException: System.Exception
{
}

  • MUST NOT suffix any enums with Enum.

Must not:

public enum ResourceTypeEnum=> it is better if Enum is removed
{
}

Method and Property naming

  • Use a verb-object pair for naming methods.

Example:

public void TestCatTask();
public void GetName();

  • If returned type is Boolean, try to prefix method name with “Can”, “Is” or “Has”.

Example:

public bool CanSave()
public bool IsEditable()
public bool HasAccess()

  • Never perfix a property name with “Get”, “Set”. It is because when using value of a property it is already understand as calling to a “Get” method.

Must not:

public int GetIntValue { get; set; }

Must:

public int IntValue { get; set; }

Generic types naming

  • Should use single capital letters for generic types.

Should:

public class List<T>where T : string
{
}

Code organization

using Statement

  • All using statements should be put outside any namespaces statements.

Should:

using CatGlobe.Web.UI.WebControls;
using CatGlobe.Web.Common.ClassBase;
using CatGlobe.Web.Common.ServerMethods;
using CatGlobe.Domain.SiteVisitor.ServiceHandler;
namespace CatGlobe.Web.Common.Resources.List;
{
}

Should not:

namespace CatGlobe.Web.Common.Resources.List
{
using CatGlobe.Web.UI.WebControls;
using CatGlobe.Web.Common.ClassBase;
using CatGlobe.Web.Common.ServerMethods;
using CatGlobe.Domain.SiteVisitor.ServiceHandler;
}

  • Related using statements should be group together. This helps reader an overview of which library will be used in the remaining code.

Example:

using System;
using System.Xml;
using System.IO;
using Infragistics.WebUI.UltraWebGrid; => Reference Infragistics
using Infragistics.WebUI.UltraWebNavigator;
using Infragistics.WebUI.UltraWebToolbar;
using CatGlobe.DataAccess;=> Reference CatGlobe's DataAccess
using CatGlobe.Framework.GroupBuilder; => Reference CatGlobe's Framework
using CatGlobe.Framework.Resources;

Internal Types

  • Types that are declared inside a class must be group together. And the group must be put right after class definition.

Example:

public class ResourceList
{
#region Types
/// <summary>
/// Collection of <seecref="CatGlobe.Domain.Common.Resource"/>s,
/// in which we can sort the whole list or
/// fetch resources based on some criteria.
/// </summary>
private class ResourceCollection
{
}
/// <summary>
/// Provide methods for Save/Delete a single <seecref="CatGlobe.Domain.Common.Resource"/>
/// or collection of <see cref="CatGlobe.Domain.Common.Resource"/>s.
/// </summary>
private class ResourceHandler
{
}
#endregion Types
}

Member and Local Variables

  • All class's member variable must be put at top of class (after internal types declaration). This increase readability of code, since at the first look the viewer can tell what are members of class and also theirs types.
  • Each variable of different types must be declared on different lines.

Must:

private string _sampleString;
private int _sampleInt;
private long _sampleLong1, _sampleLong2, _sampleLong3;

Must not:

private string _sampleString;
private int _sampleInt;

  • Local variables must be clared as close as possible to its first use and must be in the same code block that use it.

Methods

  • A method must not exceed 100 lines. Methods with more than 100 lines usually decrease readability and thus maintainability.
  • Methods that relate to each others by any means should be grouped together.

Example:

#region Database Access Methods
#endregion Database Access Methods
#region Private Utility Methods
#endregion Private Utility Methods
#region Serialization Methods
#endregion Serialization Methods

  • Interface implementations must be grouped together.

Example:

public interface ISample
{
void SampleMethod();
}
public class SampleImplementation: ISample
{
#region ISample Members
void ISample.SampleMethod()
{
}
#endregion ISample Members
}

Indentation deep

  • All methods' indentation must not exceed a deep level of 3. Methods, whose indentation exceeds 3 in deep level, decrease readability, maintainability and also increase complexity.

Must not:

if (HasAccess)
{
for (int i = 0; i < COUNT; i++)
{
if (list[i].IsTrue)
{
switch (resourceType)
{
=> Code inside this block are too deep
}
}
}
}

Tips:
There are 2 methods for reducing the deep level of code block:

  • Reorganizing if statement

Original code:

public void SampleMethod(bool someCondition)
{
if (someCondition)
{
for (int i = 0; i < COUNT; i++)
{
}
}
}

Changed code:

public void SampleMethod(bool someCondition)
{
if (!someCondition)
return ;
for (int i = 0; i < COUNT; i++)
{
}
}

=> The indentation's deep level has been decreased by 1.

  • Creating private method

Original code:

public void SampleMethod(bool someCondition)
{
if (HasAccess)
{
for (int i = 0; i < COUNT; i++)
{
if (list[i].IsTrue)
{
foreach (int value in list[i].Values)
{
if (value > 0)
{
for (int j = 0; j < value; j++)
{
if (anotherCondition)
{
}
}
}
}
}
}
}
}

Changed code:

public void SampleMethod(bool someCondition)
{
if (HasAccess)
{
for (int i = 0; i < COUNT; i++)
{
if (list[i].IsTrue)
{
ProcessValues(list[i].Values);
}
}
}
}
private void ProcessValues(List<int> values)
{
foreach (int value in values)
{
if (value > 0)
{
for (int j = 0; j < value; j++)
{
if (anotherCondition)
{
}
}
}
}
}

=> The indentation's deep level is dreased more than 1

Attribute declaration

  • For elements that have more than one Attribute declaration, then each Attribute must be put on one line.

Must not:

[Serializable, STAThread, Obsolete("This class is obsolete. Use class Abc instead.")]
public class SampleClass

Must:

[Serializable]
[STAThread]
[Obsolete("This class is obsolete. Use class Abc instead.")]
public class SampleClass

Coding guidelines

Basic language usage

Access modifier

  • All access modifier must be explicitly given, except in interface's declaration where public is undertood by default.

Must not:

class SampleClass
{
int sampleInt;
void SampleMethod()
{
}
}

Variables and Types

  • If possible, initialize variables at declaration time.

Should:

private int primaryId = 0;
private string sampleString = string.Empty;
private List<int> sampleList = new List<int>();

  • Must use fully qualified name of types, declare the type by using statements. In case there are duplication in type's name, define aliases.

Must not:

ResourceCollectionPager pager = CatGlobe.Framework.Services.SessionManager.Current.DataModule.Get<ResourceCollectionPager>(Request["sessionid"]);

Must:

using CatGlobe.Framework.Services;
ResourceCollectionPager pager = SessionManager.Current.DataModule.Get<ResourceCollectionPager>(Request["sessionid"]);

Tips:
Always choose “using” over fully qualified name from Resolve menu of Visual Studio.
Resolve menu of VS2008

Example of using alias:

using Threading = System.Threading;
using Timers = System.Timers;
using Web = System.Web.UI;
namespaceCatGlobe.DataAccess
{
public class SampleClass
{
private Threading.Timer _threadTimer1;
private Timers.Timer _threadTimer2;
private Web.Timer _threadTimer3;
}
}

Constants and Literals

  • Never hardcoded strings that will be presented to end-users. Use Resoures class instead.

Must not:

<span>Username</span>

Must:

<span><%=Resources.Username %></span>

  • Try to use @ instead escape characters symbol

Should:

string filename = @"D:\test\test.xml";

Direct cast vs. as keyword

Following code block:

CustomClass a = src as CustomClass;

can be considered the same as the code block below:

if (src is CustomClass)
return (CustomClass)src;
else
return null;

Consequently, it is encouraged to use as keyword over directcasting, because it is neat and safer.

Exceptions handling

  • Catch least generic exception first

Must not:

try
{
using (FileStream fs = newFileStream(@"D:\test.xml")) { }
}
catch (Exception) { }
catch (IOException)
{
=>Unreachable code
}

  • Do not use exception for controlling flows, instead try to validate input parameter. Most of the time exceptions can be prevented by proper input validation.

Should not:

string input = "something";
try
{
DoWork(input);
}
catch (Exception e)
{
DoAlternateWork(input);
}

  • All exception must be re-thrown, the outest layer (usually GUI win-form or web-form) must responsible for handling the exception properly.

Must not:

try
{
DoWork();
}
catch (Exception e)
{
// do nothing
}

  • If not creating a wrapper exception, use “throw” instead of “throwe” to reserve the call-stack. “throw” statement's stack trace points to the code place that actually causes the error, while “throwe”'s stack trace points to the location of the “throwe” statement only.

Must not:

catch (IOException ioe)
{
Logger.LogError(ioe);
throw ioe; => Must be throw;
}

Correct:

catch (IOException ioe)
{
Logger.LogError(ioe);
throw new CustomeException("error", ioe);
}
catch (IOException ioe)
{
Logger.LogError(ioe);
throw;
}

Document revisions

Version No. Date Changed By Description
0.1 10.07.2008 Nguyen Trung Chinh Copy from http://blogs.msdn.com/brada/articles/361363.aspx
0.1 22.08.2008 Nguyen Trung Chinh Refine from original document, add more applicable rules
0.1 25.08.2008 Nguyen Trung Chinh Fix for 0.1 after reviews of Dennis and Thuan
0.2 08.10.2008 Nguyen Trung Chinh Add section 3.5 and 4
0.3 13.10.2008 Nguyen Trung Chinh Finish section 4
1.0 14.11.2008 Nguyen Trung Chinh Finalize version 1.0