HRA                           Kumon Human Resource Appraisal System        Application Coding Standards for C#Copyright 20...
1.0 Introduction     This document describes the programming rules and recommendations for developing     Kumon Human Reso...
1.1 Document Conventions    Coloring & Emphasis          Blue            Indicates a C# keyword or .NET type           Bol...
1.2 Document Terminology    Definitions          Access              C# keywords public, protected, internal and private d...
2.0 Programming Guideline     This standard pertains to the use of the C# language. With few exceptions, it does not     d...
2.1 Naming Conventions   1    Always use US-English for naming identifiers.        Example:        realize, center, millim...
2.1 Naming Conventions   7    Always use method names using verb-object pair.        Example:        ShowDialog(), Calcula...
2.1 Naming Conventions  12    Try to name a general Exception as “ex” since the “e” parameter is routinely used for the Ev...
2.2 Coding Style   1    Avoid putting multiple classes in a single file.   2    Avoid methods with more than 7 or 8 argume...
2.2 Coding Style  10    Always segregate interface implementation by using #region statements.        Always use meaningfu...
2.2 Coding Style  15    Always use a space before and after comparison operators.        Example:        if (x == y)      ...
2.2 Coding Style  20    Always throw informative exceptions only in exceptional situations.  21    Always throw the origin...
2.2 Coding Style  24    Always use the ternary conditional operator only for trivial conditions. Avoid complex ternary ope...
2.2 Coding Style  27    Avoid using compound conditional expressions. Try to use Boolean variables to split parts into mul...
2.2 Coding Style  29    Always compare string value using “Equals” method and String.Length = 0 to check for empty string....
2.3 Documenting Style   1    Always use US-English in correct grammar and contain appropriate punctuation marks for commen...
2.4 Framework Specific Guideline   1    Never write code in aspx files. All code should be in the code-beside partial clas...
Upcoming SlideShare
Loading in...5
×

WinStore.vn | Application coding standards for C#

426

Published on

http://winstore.vn

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
426
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "WinStore.vn | Application coding standards for C#"

  1. 1. HRA Kumon Human Resource Appraisal System Application Coding Standards for C#Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  2. 2. 1.0 Introduction This document describes the programming rules and recommendations for developing Kumon Human Resource Appraisal System (HRA) using C# language and .NET Framework Common Type System (CTS). Objectives of implementing this standard:  Avoidance of common errors/bugs  Performance improvement, by dissuading wasteful practices  Maintainability, by promoting some proven design principles  Standardization, by requiring a certain unity of programming styleCopyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  3. 3. 1.1 Document Conventions Coloring & Emphasis Blue Indicates a C# keyword or .NET type Bold Highlights an important keyword Keywords Always This rule must be enforced Never This action must not happen Do Not This action must not happen Avoid This action should be prevented, with some exceptions allowed for special cases Try This rule should be attempted whenever possible and deemed suitable Example Precedes text used to illustrate a rule or recommendationCopyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  4. 4. 1.2 Document Terminology Definitions Access C# keywords public, protected, internal and private declare Modifier the allowed code accessibility of types and their members. Classes and most other members use private while interfaces and enums are default to public. Camel Case A word with first letter lowercase, and the first letter of each subsequent word-part capitalized. Example: customerName Pascal Case A word with first letter uppercase, and the first letter of each subsequent word-part capitalized. Example: CustomerName Common The .NET Framework Common Type System (CTS) defines how types Type System are declared, used and managed. All native C# types are based upon the CTS to ensure support for cross-language integration. Identifier A programmer-defined token used to uniquely name a declared object or object instance. Example: public class MyClassName {...}Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  5. 5. 2.0 Programming Guideline This standard pertains to the use of the C# language. With few exceptions, it does not discuss the use of the .NET class libraries. This guideline covers topics such as:  Naming Conventions  Coding Style  Documenting Style  Framework Specific GuidelineCopyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  6. 6. 2.1 Naming Conventions 1 Always use US-English for naming identifiers. Example: realize, center, millimeter, behavior, color 2 Always use Pascal casing for type, method names and constants. Example: private class MyClass {...} protected void GetSalesOrder() {...} 3 Always use Camel casing for local variable names and method arguments. Example: int orderNumber string customerName 4 Always use C# predefined types rather than the aliases in the System namespace. Example: //Bad //Good System.String -> string System.Int16 -> short System.Int32 -> int 5 Never use all capital or lower case names. Example: CUSTOMERNAME, customeraddress 6 Never use names that begin with numeric characters. Example: 100Customer, 5AddressZipCodeCopyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  7. 7. 2.1 Naming Conventions 7 Always use method names using verb-object pair. Example: ShowDialog(), CalculatePrice() 8 Always use an –ing and –ed form to express pre-events and post-events. Example: //Bad //Good BeforePriceValidation() -> PriceValidating() AfterPriceValidation() -> PriceValidated() 9 Try to prefix boolean variables and properties with “Can”, “Is” or “Has”. Example: IsValid, IsActive, HasCalculated 10 Avoid using abbreviations unless the full name is very long. Avoid using abbreviations longer than 5 characters. Try to use uppercase for two-letter abbreviations. Try to use abbreviations that are widely known and accepted. Example: //Original //With abbreviation DisplayMaximumProductPrice() -> DisplayMaxProductPrice() 11 Try to name the EventArgs of an event as “e” and the server as “sender” especially when working with the basic EventHandler. Example: public delegate void EventHandler(object sender, EventArgs e);Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  8. 8. 2.1 Naming Conventions 12 Try to name a general Exception as “ex” since the “e” parameter is routinely used for the EventArgs. Example: catch (Exception ex) { } 13 Avoid using Hungarian Notations. Example: //Bad strName, iAge, dblPrice 14 Avoid adding redundant or meaningless prefixes and suffixes to identifiers. Example: //Bad public enum ColorsEnum {...} public class CVehicle {...} public struct RectangleStruct {...} 15 Append computational qualifiers to variable names like Average, Count, Sum, Min and Max where appropriate. Example: int maxQuantity double minPrice 16 Avoid using letters that can be mistaken for digits, and vice versa. Example: Letter “o”, “O” and “l”Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  9. 9. 2.2 Coding Style 1 Avoid putting multiple classes in a single file. 2 Avoid methods with more than 7 or 8 arguments. Use structures for passing multiple arguments. 3 Try to use const directive only on natural constants such as the number of days in a week. Example: public const int DaysInWeek = 7; 4 Avoid using const on read-only variables. Use readonly directive instead. Example: public class MyClass { public readonly int Number; } 5 A file name should reflect the class that it contains. 6 Try to declare a local variable as close as possible to its first use. 7 Always place curly braces ({ and }) in a new line. 8 Always use curly braces ({ and }) for conditional statements. Example: //Bad if (IsValid) DoSomething(); //Good if (IsValid) { DoSomething(); } 9 Always maintain strict indentions for all lines of codes. Always use a Tab (Size 4).Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  10. 10. 2.2 Coding Style 10 Always segregate interface implementation by using #region statements. Always use meaningful description for #region. 11 Always use white space (CR/LF, Tabs, etc) to separate and organize codes. 12 Always use a space before an opening parenthesis “(“ in a flow control statement. Example: if (isValid) { while (isValid) { } } 13 Avoid using a space before an opening parenthesis “(“ in a function call. Example: //Bad CreateFoo (); //Good CreateFoo(); 14 Avoid using spaces inside brackets. Example: //Bad x = array[ index ]; //Good x = array[index];Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  11. 11. 2.2 Coding Style 15 Always use a space before and after comparison operators. Example: if (x == y) { } 16 Always use a space to separate the sections of a “for” statement. Example: for (int i = 0; i < countNumber; i++) { } 17 Always use a space after a comma in any statement. Example: CalculatePrice(a, b, c) 18 Always declare each variable independently, not in the same line of statement. Example: //Bad string customerName, customerAddress; //Good string customerName; string customerAddress; 19 Avoid throwing exceptions in situations that are normal or expected. Use return values or status enumerations instead.Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  12. 12. 2.2 Coding Style 20 Always throw informative exceptions only in exceptional situations. 21 Always throw the original exception to maintain the stack location of the original error. Example: catch(Exception ex) { MessageBox.Show(ex.Message); throw; } 22 Always initialize variables at the point of declaration. Example: //Bad string customerName; int maxQuantity = null; //Good string customerName = String.Empty; int maxQuantity = 0; 23 Always use the simplest data type required, unless you know that you will need more bit to store values. Example: //Bad long quantityNumber; //Good int quantityNumber;Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  13. 13. 2.2 Coding Style 24 Always use the ternary conditional operator only for trivial conditions. Avoid complex ternary operations. Example: //Bad int result = (isValid && maxQuantity > 100 && minQuantity < 0) ? 9 : 4; //Good int result = isValid ? 9 : 4; 25 Avoid evaluation Boolean conditions against true or false. Example: //Bad if (isValid == true) { } //Good if (isValid) { } 26 Avoid assignment within conditional statements. Example: //Bad if ((i = 2) == 2) { }Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  14. 14. 2.2 Coding Style 27 Avoid using compound conditional expressions. Try to use Boolean variables to split parts into multiple manageable expressions. Example: //Bad if (((value > highScore) && (value != highscore)) && (value < maxScore)) { } //Good isHighScore = (value >= highScore); isTiedHigh = (value == highScore); isValid = (value < maxScore); if ((isHighScore && !isTiedHigh) && isValid) { } 28 Try to use switch-case statement instead of if-else if all comparison values are known. Example: switch(lightColor) { case “green”: case “yellow”: case “red”: }Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  15. 15. 2.2 Coding Style 29 Always compare string value using “Equals” method and String.Length = 0 to check for empty string. Example: //Bad if (customerName == “John”) { } //Good if (customerName.Equals(“John”)) { } 30 Always use String.Empty instead of “” for string. //Bad string customerName = “”; //Good string customerName = String.Empty; 31 Always check for null when displaying or comparing values obtained from database. 32 Never omit access modifiers. Always declare all identifiers explicitly with the appropriate access modifiers instead of allowing the default. Example: //Bad void WriteLine(string line); //Good Private void WriteLine(string line);Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  16. 16. 2.3 Documenting Style 1 Always use US-English in correct grammar and contain appropriate punctuation marks for comments. 2 Always use // for comments. Never use /* ... */. 3 Avoid space between // and your comments. Example: //Bad // This is a sample comment //Good //This is a sample comment 4 Avoid using “flowerbox” comment blocks. Example: // *********************************************** // This is a sample comment // *********************************************** 5 Avoid placing comments on the same line as the code. Always place comments on separate lines. Example: //Bad CalculatePrice(); //Method to calculate item price //Good //Method to calculate item price CalculatePrice(); 6 Always include Task-List keyword flags for comment-filtering. Example: //TODO: Write database command for this function //UNDONE: Removed code line 199 due to calculation errorCopyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c
  17. 17. 2.4 Framework Specific Guideline 1 Never write code in aspx files. All code should be in the code-beside partial class. 2 Never include any Business Logic algorithms in aspx files. All Business Logic algorithm code should be written in Business Logic Framework files. 3 New source code File/Folder naming should follow the existing pattern and use the same prefixes. 4 Avoid setting the Auto-Postback property of server controls in ASP.NET to true. 5 Avoid using session variable to store post-back values if values can be posted from one form to another form. 6 Always check a session variable for null before attempting to access it. 7 Always use type-safe data sets or data tables. Avoid raw ADO.NET.Copyright 2011, NTT Communications. All Rights Reserved Doc no: 0f759e89-0a76-4a8e-a438-44b28315860c

×