+ All Categories
Home > Documents > Coding Standard C Sharp

Coding Standard C Sharp

Date post: 29-May-2018
Category:
Upload: koooool3473
View: 226 times
Download: 0 times
Share this document with a friend

of 19

Transcript
  • 8/9/2019 Coding Standard C Sharp

    1/19

    Coding Standard - C# : Coding & Commenting Standards

    CCODINGODING SSTANDARDTANDARD --

    C#C#

    Internal Document Page 1 of 19 39072851.doc

  • 8/9/2019 Coding Standard C Sharp

    2/19

    Coding Standard - C# : Coding & Commenting Standards

    Table of Contents

    1 INTRODUCTION ............................................................................................................... ..........3

    2 FILE NAMING ..............................................................................................................................4

    3 CODING STANDARDS GENERAL ..........................................................................................5

    3.1 ALLFUNCTIONSMUSTHAVERETURNTYPE.........................................................................................53.2 USING.....................................................................................................................................5

    4 NAMING CONVENTIONS ...................................................................................................... .....6

    4.1 NAMESPACE..............................................................................................................................64.2 CLASS ....................................................................................................................................64.3 INTERFACE................................................................................................................................74.4 ENUMERATION ................................................................................................................ ..........74.5 STATIC FIELD ...........................................................................................................................74.6 PARAMETER..............................................................................................................................74.7 METHOD NAME..........................................................................................................................8

    4.8 PROPERTY NAME.......................................................................................................................84.9 EVENT NAME.......................................................................................................................... ..8

    5 USAGE GUIDELINES ........................................................................................................ .......10

    5.1 PROPERTY USAGE ...................................................................................................................105.2 EVENT USAGE ........................................................................................................................105.3 METHOD USAGE......................................................................................................................115.4 CONSTRUCTOR USAGE ............................................................................................................ .125.5 FIELD USAGE..........................................................................................................................125.6 PARAMETER USAGE ................................................................................................................ .135.7 ARRAY USAGE.........................................................................................................................135.8 OPERATOR OVERLOADING ...................................................................................................... ...135.9 TYPE CASTING........................................................................................................................15

    6 EXCEPTIONS AND VALIDATIONS ...................................................................................... ....16

    6.1 EXCEPTIONS............................................................................................................................166.2 VALIDATIONS.......................................................................................................................... .17

    7 DOCUMENTATION AND COMMENTING .................................................................................18

    7.1 TAGS.............................................................................................................................. ......18

    8 UNIT TESTS ..............................................................................................................................19

    8.1 TEST MODULE.........................................................................................................................198.2 TESTCLASS............................................................................................................................198.3 TESTS...................................................................................................................................198.4 TEST COVERAGE......................................................................................................................19

    Internal Document Page 2 of 19 39072851.doc

  • 8/9/2019 Coding Standard C Sharp

    3/19

    Coding Standard - C# : Coding & Commenting Standards

    1 Introduction

    This document describes the standards that every developer in Tally should follow while

    coding in C#.

    This document addresses the following standards

    File naming

    Coding Standards General

    Coding Standards - Platform

    Coding conventions are very important and it is expected that all developers will knowthese standards and follow them.

    Internal Document Page 3 of 19 39072851.doc

  • 8/9/2019 Coding Standard C Sharp

    4/19

    Coding Standard - C# : Coding & Commenting Standards

    2 File Naming

    Source file naming should conform to the following:

    Use Pascal casing for filenames

    Name should be meaningful

    Try to keep the file name short, while making sure that it conveys the purpose

    All source files will fall into one of these types

    File Extension / Type Purpose

    .cs Class implementation

    Internal Document Page 4 of 19 39072851.doc

  • 8/9/2019 Coding Standard C Sharp

    5/19

    Coding Standard - C# : Coding & Commenting Standards

    3 Coding Standards General

    The section explains the coding standards to be used by all developers while coding.

    3.1 All functions must have return type

    All functions need to have an explicit return type. All functions should return onlyReference types or bool as return values.

    E.g.

    ProcessDoc(String name); is wrong

    bool ProcessDoc(String name); is right

    3.2 Using

    3.2.1 Name space references

    All the namespaces referenced in the code must be included in to the assembly byusing the using keyword.

    E.g.

    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

    Internal Document Page 5 of 19 39072851.doc

  • 8/9/2019 Coding Standard C Sharp

    6/19

    Coding Standard - C# : Coding & Commenting Standards

    4 Naming Conventions

    4.1 Namespace

    You should use Pascal case for namespaces, and separate logical components withperiods, as in TallyShoper9.Lib.Log. The general rule for naming namespaces is to usethe company name followed by the technology name and optionally the feature anddesign as follows.

    ProductName.Group.[.Feature][.Design]

    For example:

    TallyShoper9.Comp.ExportServer;

    Prefixing namespace names with TallyShoper9 name will avoid conflicts withnamespaces. For TallyShoper9.Lib and Tally9Erp.Lib will form two different libraries andwill not create any conflicts.

    Second level in the name space should identify the group of the namespaces. Forexample, Lib for library and Comp for Components.

    Classes in nested namespaces should have dependency on types in the containgnamespace. For example classes in namespace TallyShoper9.Lib.File.FileAccess willdepend on classes in TallyShoper9.Lib.File, but inverse of the same should not beallowed.

    Do not use the same name for a namespace and a class. For example, do not provideboth a Debug namespace and a Debug class.

    4.2 Class

    Use a noun or noun phrase to name a class

    UsePascal case. Use abbreviations sparingly

    Do not use a type prefix, such as C for class, on a class name. For example, use theclass name FileStream rather than CFileStream.

    Do not use the underscore character (_).

    Occasionally, it is necessary to provide a class name that begins with the letter I, eventhough the class is not an interface. This is appropriate as long as I is the first letter of an

    Internal Document Page 6 of 19 39072851.doc

    http://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspx
  • 8/9/2019 Coding Standard C Sharp

    7/19

    Coding Standard - C# : Coding & Commenting Standards

    entire word that is a part of the class name. For example, the class name IdentityStore isappropriate.

    Where appropriate, use a compound word to name a derived class. The second part ofthe derived class's name should be the name of the base class. For example,ApplicationException is an appropriate name for a class derived from a class namedException,

    4.3 Interface

    Name interfaces with nouns or noun phrases, or adjectives that describe behavior. Forexample, IComponent , ICustomAttributeProvider, IPersistable

    UsePascal case

    Use abbreviations sparingly

    Prefix interface names with the letter I, to indicate that the type is an interface.

    Use similar names when you define a class/interface pair where the class is a standard

    implementation of the interface. The names should differ only by the letter I prefix on theinterface name.

    Do not use the underscore character (_).

    4.4 Enumeration

    UsePascal casefor Enum types and value names.

    Use abbreviations sparingly.

    Do not use an Enum suffix on Enum type names.

    4.5 Static Field Use nouns, noun phrases, or abbreviations of nouns to name static fields.

    UsePascal case.

    Do not use a Hungarian notation prefix on static field names ex: strUserName is wronguse userName instead

    It is recommended that you use static properties instead of public static fields wheneverpossible

    4.6 Parameter

    It is important to carefully follow these parameter naming guidelines because visualdesign tools that provide context sensitive help and class browsing functionality displaymethod parameter names to users in the designer.

    Usecamel case for parameter names.

    Use descriptive parameter names. Parameter names should be descriptive enough thatthe name of the parameter and its type can be used to determine its meaning in mostscenarios. For example, visual design tools that provide context sensitive help displaymethod parameters to the developer as they type. The parameter names should be

    Internal Document Page 7 of 19 39072851.doc

    http://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspx
  • 8/9/2019 Coding Standard C Sharp

    8/19

    Coding Standard - C# : Coding & Commenting Standards

    descriptive enough in this scenario to allow the developer to supply the correctparameters.

    Use names that describe a parameter's meaning rather than names that describe aparameter's type. Development tools should provide meaningful information about aparameter's type. Therefore, a parameter's name can be put to better use by describingmeaning. Use type-based parameter names sparingly and only where it is appropriate.

    Do not prefix parameter names with Hungarian type notation.

    Type GetType(string typeName)

    string Format(string format, object[] args)

    4.7 Method Name

    Use verbs or verb phrases to name methods.

    UsePascal case.

    4.8 Property Name

    Use a noun or noun phrase to name properties

    UsePascal case.

    Do not use Hungarian notation

    Consider creating a property with the same name as its underlying type. For example, ifyou declare a property named Color, the type of the property should likewise beColor.

    4.9 Event Name

    UsePascal case.

    Do not use Hungarian notation.

    Use an EventHandler suffix on event handler names.

    Specify two parameters named sender and e. The sender parameter represents theobject that raised the event. The sender parameter is always of type object, even if it ispossible to use a more specific type. The state associated with the event is encapsulatedin an instance of an event class named e. Use an appropriate and specific event class for

    the e parameter type.

    Name an event argument class with the EventArgs suffix.

    Consider naming events with a verb. For example, correctly named event names includeClicked, Painting, and DroppedDown.

    Use a gerund (the "ing" form of a verb) to create an event name that expresses theconcept of pre-event, and a past-tense verb to represent post-event. For example, aClose event that can be canceled should have a Closing event and a Closed event. Donot use the BeforeXxx/AfterXxx naming pattern.

    Internal Document Page 8 of 19 39072851.doc

    http://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.drawing.color(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.drawing.color(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.drawing.color(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.drawing.color(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspx
  • 8/9/2019 Coding Standard C Sharp

    9/19

    Coding Standard - C# : Coding & Commenting Standards

    Do not use a prefix or suffix on the event declaration on the type. For example, use Closeinstead of OnClose.

    In general, you should provide a protected method called OnXxx on types with eventsthat can be overridden in a derived class. This method should only have the eventparameter e, because the sender is always the instance of the type.

    Internal Document Page 9 of 19 39072851.doc

  • 8/9/2019 Coding Standard C Sharp

    10/19

    Coding Standard - C# : Coding & Commenting Standards

    5 Usage guidelines

    5.1 Property Usage Determine whether a property or a method is more appropriate for your needs.

    Choose a name for your property based on the recommended Property NamingGuidelines.

    When accessing a property using the set accessor, preserve the value of the propertybefore you change it. This will ensure that data is not lost if the set accessor throws anexception.

    Allow properties to be set in any order. Properties should be stateless with respect toother properties. For example, a TextBox control might have two related properties:DataSource and DataField. DataSource specifies the table name, and DataFieldspecifies the column name. Once both properties are specified, the control canautomatically bind data from the table into the Text property of the control.

    Components should raise property-changed events. This is useful if the developer wantsto perform an operation when ever the value of a property is changed

    Use a property when the member is a logical data member. In the following memberdeclarations, Name is a property because it is a logical member of the class.

    You should use a read-only property when the user cannot change the property's logicaldata member.

    Do not use write-only properties.

    5.2 Event Usage

    When you refer to events in documentation, use the phrase, "an event was raised"instead of "an event was fired" or "an event was triggered."

    Use a return type of void for event handlers, as shown in the following C# code example.

    Event classes should extend the System.EventArgs Class, as shown in the followingexample.

    Use a protected (Protected in Visual Basic) virtual method to raise each event. Thistechnique is not appropriate for sealed classes, because classes cannot be derived from

    them. The purpose of the method is to provide a way for a derived class to handle theevent using an override. This is more natural than using delegates in situations where thedeveloper is creating a derived class. The name of the method takes the formOnEventName, where EventName is the name of the event being raised. For example:

    public class Button

    {

    ButtonClickHandler onClickHandler;

    Internal Document Page 10 of 19 39072851.doc

    http://msdn.microsoft.com/en-us/library/fzcth91k(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/fzcth91k(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/fzcth91k(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.eventargs(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/fzcth91k(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/fzcth91k(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.eventargs(VS.71).aspx
  • 8/9/2019 Coding Standard C Sharp

    11/19

    Coding Standard - C# : Coding & Commenting Standards

    protected virtual void OnClick(ClickEventArgs e)

    {

    // Call the delegate if non-null.

    if (onClickHandler != null)onClickHandler(this, e);

    }

    }

    Use or extend the System.ComponentModel.CancelEventArgs Class to allow thedeveloper to control the events of an object. For example, the TreeView control raises aBeforeLabelEdit when the user is about to edit a node label. The following code exampleillustrates how a developer can use this event to prevent a node from being edited.

    Note that in this case, no error is generated to the user. The label is read-only.

    Cancel events are not appropriate in cases where the developer would cancel the

    operation and return an exception. In these cases, you should raise an exception insideof the event handler in order to cancel. For example, the user might want to writevalidation logic in an edit control as shown.

    5.3 Method Usage

    By default, methods are nonvirtual. Maintain this default in situations where it is notnecessary to provide virtual methods.

    Use method overloading to provide different methods that do semantically the same

    thing. Use method overloading instead of allowing default arguments. Default arguments do not

    version well and therefore are not allowed in the Common Language Specification(CLS).

    Use a consistent ordering and naming pattern for method parameters. It is common toprovide a set of overloaded methods with an increasing number of parameters to allowthe developer to specify a desired level of information. The more parameters that youspecify, the more detail the developer can specify.

    Note that the only method in the group that should be virtual is the one that has the mostparameters and only when you need extensibility.

    If you must provide the ability to override a method, make only the most completeoverload virtual and define the other operations in terms of it.

    You might want to expose a method that takes a variable number of arguments. A classicexample is the printf method in the C programming language. For managed classlibraries, use the params (ParamArray in Visual Basic) keyword for this construct. Forexample, use the following code instead of several overloaded methods.

    void Format(string formatString, params object [] args)

    Internal Document Page 11 of 19 39072851.doc

    http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.treeview(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/aa735713(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/aa735713(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.treeview(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/aa735713(VS.71).aspx
  • 8/9/2019 Coding Standard C Sharp

    12/19

    Coding Standard - C# : Coding & Commenting Standards

    5.4 Constructor Usage

    Provide a default private constructor if there are only static methods and properties on aclass. The private constructor prevents the class from being created.

    Minimize the amount of work done in the constructor. Constructors should not do more

    than capture the constructor parameter or parameters. This delays the cost of performingfurther operations until the user uses a specific feature of the instance.

    Provide a constructor for every class. If a type is not meant to be created, use a privateconstructor Why:. If you do not specify a constructor, C# implicitly add a default publicconstructor. If the class is abstract, it adds a protected constructor. if you add a nondefault constructor to a class in a later version release, the implicit default constructor willbe removed which can break client code. Therefore, the best practice is to alwaysexplicitly specify the constructor even if it is a public default constructor.

    Provide a protected constructor that can be used by types in a derived class.

    You should not provide constructor without parameters for a value type struct. Note thatmany compilers do not allow a struct to have a constructor without parameters. If you donot supply a constructor, the runtime initializes all the fields of the struct to zero. This

    makes array and static field creation faster.

    Use parameters in constructors as shortcuts for setting properties. There should be nodifference in semantics between using an empty constructor followed by property setaccessors, and using a constructor with multiple arguments.

    Use a consistent ordering and naming pattern for constructor parameters. A commonpattern for constructor parameters is to provide an increasing number of parameters toallow the developer to specify a desired level of information. The more parameters thatyou specify, the more detail the developer can specify.

    5.5 Field Usage

    Do not use instance fields that are public or protected (Public or Protected in VisualBasic). If you avoid exposing fields directly to the developer, classes can be versionedmore easily because a field cannot be changed to a property while maintaining binarycompatibility. Consider providing get and set property accessors for fields instead ofmaking them public. The presence of executable code in get and set property accessorsallows later improvements, such as creation of an object on demand, upon usage of theproperty, or upon a property change notification.

    Expose a field to a derived class by using a protected property that returns the value ofthe field

    Use the const keyword to declare constant fields that will not change. Languagecompilers save the values of const fields directly in calling code.

    Spell out all words used in a field name. Use abbreviations only if developers generallyunderstand them. Do not use uppercase letters for field names

    Do not use Hungarian notation for field names. Good names describe semantics, nottype.

    Do not apply a prefix to field names or static field names. Specifically, do not apply aprefix to a field name to distinguish between static and nonstatic fields. For example,applying a g_ or s_ prefix is incorrect.

    Internal Document Page 12 of 19 39072851.doc

  • 8/9/2019 Coding Standard C Sharp

    13/19

    Coding Standard - C# : Coding & Commenting Standards

    5.6 Parameter Usage

    Check for valid parameter arguments. Perform argument validation for every public orprotected method and property set accessor. Throw meaningful exceptions to thedeveloper for invalid parameter arguments. Use the System.ArgumentException Class,or a class derived from System.ArgumentException.

    Note that the actual checking does not necessarily have to happen in the public orprotected method itself. It could happen at a lower level in private routines. The mainpoint is that the entire surface area that is exposed to the developer checks for validarguments.

    Make sure you fully understand the implications of passing parameters by value or byreference. Passing a parameter by value copies the value being passed and has noeffect on the original value.

    Passing a parameter by reference passes the storage location for the value. As a result,changes can be made to the value of the parameter. The following method examplepasses a parameter by value.

    An output parameter represents the same storage location as the variable specifed as

    the argument in the method invocation. As a result, changes can be made only to theoutput parameter.

    5.7 Array Usage

    In functions do not return an internal instance of an array. This allows calling code tochange the array. Instead return a clone of the array

    Eg: return (char[])arrayName.Clone();

    Functions returning arrays should not return null reference; it should always return zeroelement arrays.

    5.8 Operator Overloading

    Define operators on value types that are logical built-in language types, such asstructure.

    Provide operator-overloading methods only in the class in which the methods aredefined. The C# compiler enforces this guideline.

    Use the names and signature conventions described in the Common LanguageSpecification (CLS). The C# compiler does this for you automatically.

    Use operator overloading in cases where it is immediately obvious what the result of theoperation will be. For example, it makes sense to be able to subtract one Time value fromanother Time value and get a TimeSpan. However, it is not appropriate to use the oroperator to create the union of two database queries, or to use shift to write to a stream.

    Overload operators in a symmetric manner. For example, if you overload the equalityoperator (==), you should also overload the not equal operator(!=).

    Provide alternate signatures. Most languages do not support operator overloading. Forthis reason, it is a CLS requirement for all types that overload operators to include asecondary method with an appropriate domain-specific name that provides the equivalent

    Internal Document Page 13 of 19 39072851.doc

    http://msdn.microsoft.com/en-us/library/system.argumentexception(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.argumentexception(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/aa735713(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/aa735713(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/system.argumentexception(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/aa735713(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/aa735713(VS.71).aspx
  • 8/9/2019 Coding Standard C Sharp

    14/19

    Coding Standard - C# : Coding & Commenting Standards

    functionality. It is a Common Language Specification (CLS) requirement to provide thissecondary method. The following example is CLS-compliant.

    public struct DateTime

    {

    publicstatic TimeSpan operator -(DateTime t1, DateTime t2){}

    publicstatic TimeSpan Subtract(DateTime t1, DateTime t2) {}

    }

    The following table contains a list of operator symbols and the corresponding alternativemethods and operator names.

    C++ operatorsymbol Name of alternative method Name of operator

    Not defined ToXxx or FromXxx op_Implicit

    Not defined ToXxx or FromXxx op_Explicit

    + (binary) Add op_Addition

    - (binary) Subtract op_Subtraction

    * (binary) Multiply op_Multiply

    / Divide op_Division

    % Mod op_Modulus

    ^ Xor op_ExclusiveOr

    & (binary) BitwiseAnd op_BitwiseAnd

    | BitwiseOr op_BitwiseOr

    && And op_LogicalAnd

    || Or op_LogicalOr

    = Assign op_Assign

    > RightShift op_RightShift

    Not defined LeftShift op_SignedRightShift

    Not defined RightShift op_UnsignedRightShift

    == Equals op_Equality

    > Compare op_GreaterThan

    < Compare op_LessThan

    != Compare op_Inequality

    >= Compare op_GreaterThanOrEqual

  • 8/9/2019 Coding Standard C Sharp

    15/19

    Coding Standard - C# : Coding & Commenting Standards

    %= Mod op_ModulusAssignment

    += Add op_AdditionAssignment

    &= BitwiseAnd op_BitwiseAndAssignment

    |= BitwiseOr op_BitwiseOrAssignment

    , None assigned op_Comma

    /= Divide op_DivisionAssignment

    -- Decrement op_Decrement

    ++ Increment op_Increment

    - (unary) Negate op_UnaryNegation

    + (unary) Plus op_UnaryPlus

    ~ OnesComplement op_OnesComplement

    5.9 Type Casting

    Do not allow implicit casts that will result in a loss of precision. For example, there shouldnot be an implicit cast from Double to Int32, but there might be one from Int32 to Int64.

    Do not throw exceptions from implicit casts because it is very difficult for the developer tounderstand what is happening.

    Provide casts that operate on an entire object. The value that is cast should represent theentire object, not a member of an object. For example, it is not appropriate for a Button tocast to a string by returning its caption.

    Do not generate a semantically different value. For example, it is appropriate to convert aDateTime or TimeSpan into an Int32. The Int32 still represents the time or duration. Itdoes not, however, make sense to convert a file name string such as "c:\mybitmap.gif"into a Bitmap object.

    Do not cast values from different domains. Casts operate within a particular domain ofvalues. For example, numbers and strings are different domains. It makes sense that anInt32 can cast to a Double. However, it does not make sense for an Int32 to cast to aString, because they are in different domains.

    Internal Document Page 15 of 19 39072851.doc

  • 8/9/2019 Coding Standard C Sharp

    16/19

    Coding Standard - C# : Coding & Commenting Standards

    6 Exceptions and Validations

    6.1 Exceptions6.1.1 Exception Handling

    All exceptions are captured at the procedure level and will not catch any general exceptions. All

    the unknown exception and unhandled exceptions will be caught at the application/thread level.

    For example

    classExample

    {

    publicstaticvoid Main()

    {

    try

    {

    //Try something

    DoSomeThing();

    }

    catch (Exception e)

    {

    //catch all ubknown exception types and handle the same

    //log the exception and rollback

    }

    finally

    {

    //finalize

    }}

    publicvoid DoSomeThing()

    {

    int i;

    int j;

    i = 0;

    j = 10;

    try

    {

    i = j / i;

    }

    catch (DivideByZeroException e) //catch only known exception

    types and process

    {

    Console.Write("divided by zero error occurs");

    }

    }

    }

    Internal Document Page 16 of 19 39072851.doc

  • 8/9/2019 Coding Standard C Sharp

    17/19

    Coding Standard - C# : Coding & Commenting Standards

    6.1.2 Throwing Exceptions

    If it is necessary to throw an exception then the same must be done using anappropriate exception type.

    6.2 Validations

    All function calls must be validated for a valid return value. All lib functions will return only

    reference types and it will return null when the processing is not done. The calling function will

    have to validate the result before use. If the function has to return a value type then out parameter

    will be used and the function will return a bool value.

    Function call: //Try something

    resultString = DoSomeThing(2);

    if (resultString == null)

    {

    Console.WriteLine("error occurred"); Console.ReadLine();

    }

    Function definitions: staticString DoSomeThing(int j)

    {

    int i;

    i = 0;

    try

    {

    i = j / i; return (i.ToString());

    }

    catch (DivideByZeroException e) //catch only known

    exception types and process

    {

    Console.Write("divided by zero error occurs");

    return (null);

    }

    }

    Internal Document Page 17 of 19 39072851.doc

  • 8/9/2019 Coding Standard C Sharp

    18/19

    Coding Standard - C# : Coding & Commenting Standards

    7 Documentation and commenting

    All code segments must be well commented. All functions and logical steps should have

    documentation tags defined in the doxygen format. The following tags should be usedwhen documenting the code.

    7.1 Tags

    Identifies inline text that should be rendered as a piece of code. Similar to usingtext.

    Set one or more lines of source code or program output. Note that this commandbehaves like \code ... \endcode for C# code, but it behaves like the HTML equivalent... for other languages.

    Marks a block of text as an example, ignored by doxygen.

    Identifies the exception a method can throw.

    Starts a list, supported types are bullet or number and table. A listconsists of a number of tags. A list of type table, is a two column table which canhave a header.

    List item. Can only be used inside a context.

    Part of a command, describes an item.

    Starts the header of a list of type "table".

    Identifies a paragraph of text.

    Marks a piece of text as the documentation for parameter"paramName". Similar to using \param.

    Refers to a parameter with name "paramName".Similar to using \a.

    Identifies the detailed description.

    Marks a piece of text as the return value of a function or method. Similar to

    using \return. Refers to a member. Similar to \ref.

    Starts a "See also" section referring to "member". Similar tousing \sa member.

    Identifies the brief description. Similar to using \brief.

    Part of a command.

    Marks a piece of text as the documentation for typeparameter "paramName". Similar to using \param.

    Refers to a parameter with name "paramName".Similar to using \a.

    For detailed discussion of doxygen refer to Doxygen Manual on OM.

    Internal Document Page 18 of 19 39072851.doc

    http://www.stack.nl/~dimitri/doxygen/commands.html#cmdparamhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdahttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdahttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdreturnhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdrefhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdrefhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdsahttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdbriefhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdparamhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdparamhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdahttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdahttp://om/Process%20and%20Templates/DocumentationProcess/Doxygen.doc#_Toc152326196http://www.stack.nl/~dimitri/doxygen/commands.html#cmdparamhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdahttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdreturnhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdrefhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdsahttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdbriefhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdparamhttp://www.stack.nl/~dimitri/doxygen/commands.html#cmdahttp://om/Process%20and%20Templates/DocumentationProcess/Doxygen.doc#_Toc152326196
  • 8/9/2019 Coding Standard C Sharp

    19/19

    Coding Standard - C# : Coding & Commenting Standards

    8 Unit Tests

    Coding should be started only after the tests that the code should pass are ready. Each

    code block should have a test written. This ensures that running all the tests will makesure that all the known issues are tested. This is useful for unit testing.

    8.1 Test Module

    All the tests must be in a separate file under Tests folder of the source path.

    The test namespace must be under TallyShoper9.Tests. Eg: if you are writing Tests forVendor module then the tests should be under TallyShoper9.Tests.Vendor

    File name should be the module file name followed by test. Eg: Vendor class is in theVendor.cs file, all the tests specific to Vendor class must be in VendorTest.cs file in theTest folder.

    8.2 Test class

    The tests must be written with same logical division as the module. Eg: if there is Vendormodule devided as Vendor, VendorTypes classes then the tests must also be defined asVendorTest, VendoerTypesTest classes

    You have to use [TestFixture] before the definition of the class to make it as test class.

    8.3 Tests

    The test must be intended to test one or more logical units of the application.

    The function that will contain the test will have to be marked as test by using [Test] beforethe definition of the function.

    Assert class should be used for comparision of the results as applicable.

    8.4 Test Coverage

    All the code lines must be visited at least once.

    To test the coverage of the code line testing use NCoverage tool. This will display all thelines of code that is not visited at least once.

    Make your own judgment on the tests to be performed.

    Eg: We can test a division function by passing 8, 2 as values which will return 4. This willbe a passed test and all the lines of code is visited. It does not mean that a test with parameters8, 0 will also pass. You have to think all the possible scenarios and use cases to test.

    Internal Document Page 19 of 19 39072851.doc


Recommended