Friday, August 1, 2008

Programming Standard for Our Team

1. Exception Handling
1) Deprive all custom exceptions from System.ApplicationException, and putting all custom exceptions into WD.AIATools.Common.Exceptions namespace;
2) Define three overloaded version for constructor, such as:

public class ActionParamsException : ApplicationException

{

    public ActionParamsException()

        : base()

    {    }

 

    public ActionParamsException(string message)

        : base(message)

    {    }

 

    public ActionParamsException(string message, Exception ex)

        : base(message, ex)

    {    }

}

3) Provide a helper method which includes the logging operation for your custom exception in ThrowHelper class:

    public static ActionParamsException ThrowActionParamsException(string message, Exception ex)

    {

        LogService.Error(message, ex);

        return new ActionParamsException(message, ex);

    }

4) In Data Access Layer, try to catch the exception which is thrown by .NET Framework, and re-throw the custom exception by invoking the helper method with ThrowHelper. For example:

        try

        {

            Foo();

        }

        catch (SqlException ex)

        {

            throw; //Avoid

            ThrowHelper.ThrowCustomException(“message”, ex); //OK

        }

        catch (NotSupportedException ex)

        {

            ThrowHelper.ThrowCustomException(“message”, ex); //OK

        }

        catch (Exception ex)

        {

            ThrowHelper.ThrowCustomException(“message”, ex); //OK

        }

        finally //if necessary

        {

            //do something

        }

5) Don’t catch the exception in Business Logic Layer otherwise it is necessary.
6) In Presentation Layer, it includes two cases:
a) Show the error information on the current page through by catching the exception;
b) Link to the error page which includes the error information with Exception.Message;
7) Don’t catch the exception inside for or foreach statement.

 

2. Robust Check Policy
1) Avoid throwing the NullReferenceException. Check whether the object is null before using it:

if (someObject != null)

{

    //Do something;

}

else //if necessary

{

    //throw the custom exception or else

}

2) Never return the null value especially if the object is collection:

public IList GetSomeList()

{

    //Something to do;

    if (NoItemInCollection)

    {

        return new List(); //OK

        return null; //Avoid;

    }

}

3) Always explicitly initialize an array of reference types using a for loop:

public class MyClass{}

 

const int ArraySize = 10;

MyClass[] array = new MyClass[ArraySize];

for (int index = 0; index < ArraySize; ++index)

{

    Array[index] = new MyClass();

}

4) Prefer to use foreach statement instead of for statement;
5) Try to check what the instance’s type is before casting the reference type by using “is” operator:

if (someObject is Foo)

{

    Foo object = (Foo)someObject;

}

//Or

Foo object = someObject as Foo;

6) Remember to dispose the resource when using the unmanaged resource; Always use a using statement in this situation.

3. Code Practices
1) Avoid putting multiple classes in a single file;
2) Never hard-code a numeric value; always declare a constant instead;
3) Avoid providing explicit values for enums unless they are integer powers of 2:

//Correct

public enum Color

{

    Red, Green, Blue

}

Note: Putting all enum types into the specific file.
4) Use String.Empty instead of “”;
5) When building a long string, use StringBuilder, not String;
6) Always have a default case in a switch statement;

No comments: