Knowledge Share
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Knowledge Share

Knowledge is NOT Power IMPLEMENTATION of knowledge is Power!!!
 
HomePortalGalleryLatest imagesRegisterLog in

 

 Coding Conventions in .net

Go down 
AuthorMessage
Admin
Admin



Posts : 142
Points : 59554454
Reputation : 0
Join date : 2007-12-29
Location : Chennai

Coding Conventions in .net Empty
PostSubject: Coding Conventions in .net   Coding Conventions in .net Icon_minitimeMon Dec 31, 2007 12:06 pm

Method Usage Guidelines
The following rules outline the usage guidelines for methods:
• Choose a name for your event based on the recommended Method Naming Guidelines.
• Do not use Hungarian notation.
• By default, methods are nonvirtual. Maintain this default in situations where it is not necessary to provide virtual methods. For more information about implementing inheritance, see Base Class Usage Guidelines.
Method Overloading Guidelines
Method overloading occurs when a class contains two methods with the same name, but different signatures. This section provides some guidelines for the use of overloaded 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). The following code example illustrates an overloaded String.IndexOf method.
[Visual Basic]
Function String.IndexOf(name As String) As Integer
Function String.IndexOf(name As String, startIndex As Integer) As Integer
[C#]
int String.IndexOf (String name);
int String.IndexOf (String name, int startIndex);
• Use default values correctly. In a family of overloaded methods, the complex method should use parameter names that indicate a change from the default state assumed in the simple method. For example, in the following code, the first method assumes the search will not be case-sensitive. The second method uses the name ignoreCase rather than caseSensitive to indicate how the default behavior is being changed.
[Visual Basic]
' Method #1: ignoreCase = false.
Function Type.GetMethod(name As String) As MethodInfo
' Method #2: Indicates how the default behavior of method #1
' is being changed.
Function Type.GetMethod(name As String, ignoreCase As Boolean) As MethodInfo
[C#]
// Method #1: ignoreCase = false.
MethodInfo Type.GetMethod(String name);
// Method #2: Indicates how the default behavior of method #1 is being // changed.
MethodInfo Type.GetMethod (String name, Boolean ignoreCase);
• Use a consistent ordering and naming pattern for method parameters. It is common to provide a set of overloaded methods with an increasing number of parameters to allow the developer to specify a desired level of information. The more parameters that you specify, the more detail the developer can specify. In the following code example, the overloaded Execute method has a consistent parameter order and naming pattern variation. Each of the Execute method variations uses the same semantics for the shared set of parameters.
[Visual Basic]
Public Class SampleClass
Private defaultForA As String = "default value for a"
Private defaultForB As Integer = "42"
Private defaultForC As Double = "68.90"

Overloads Public Sub Execute()
Execute(defaultForA, defaultForB, defaultForC)
End Sub

Overloads Public Sub Execute(a As String)
Execute(a, defaultForB, defaultForC)
End Sub

Overloads Public Sub Execute(a As String, b As Integer)
Execute(a, b, defaultForC)
End Sub

Overloads Public Sub Execute(a As String, b As Integer, c As Double)
Console.WriteLine(a)
Console.WriteLine(b)
Console.WriteLine(c)
Console.WriteLine()
End Sub
End Class
[C#]
public class SampleClass
{
readonly string defaultForA = "default value for a";
readonly int defaultForB = "42";
readonly double defaultForC = "68.90";

public void Execute()
{
Execute(defaultForA, defaultForB, defaultForC);
}

public void Execute (string a)
{
Execute(a, defaultForB, defaultForC);
}

public void Execute (string a, int b)
{
Execute (a, b, defaultForC);
}

public void Execute (string a, int b, double c)
{
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine();
}
}
Note that the only method in the group that should be virtual is the one that has the most parameters and only when you need extensibility.
• If you must provide the ability to override a method, make only the most complete overload virtual and define the other operations in terms of it. The following example illustrates this pattern.
[Visual Basic]
Public Class SampleClass
Private myString As String

Public Sub New(str As String)
Me.myString = str
End Sub

Overloads Public Function IndexOf(s As String) As Integer
Return IndexOf(s, 0)
End Function

Overloads Public Function IndexOf(s As String, startIndex As
Integer) As Integer
Return IndexOf(s, startIndex, myString.Length - startIndex)
End Function

Overloads Public Overridable Function IndexOf(s As String,
startIndex As Integer, count As Integer) As Integer
Return myString.IndexOf(s, startIndex, count)
End Function
End Class
[C#]
public class SampleClass
{
private string myString;

public MyClass(string str)
{
this.myString = str;
}

public int IndexOf(string s)
{
return IndexOf (s, 0);
}

public int IndexOf(string s, int startIndex)
{
return IndexOf(s, startIndex, myString.Length - startIndex );
}

public virtual int IndexOf(string s, int startIndex, int count)
{
return myString.IndexOf(s, startIndex, count);
}
}
Methods With Variable Numbers of Arguments
You might want to expose a method that takes a variable number of arguments. A classic example is the printf method in the C programming language. For managed class libraries, use the params (ParamArray in Visual Basic) keyword for this construct. For example, use the following code instead of several overloaded methods.
[Visual Basic]
Sub Format(formatString As String, ParamArray args() As Object)
[C#]
void Format(string formatString, params object [] args)
You should not use the VarArgs or ellipsis (...) calling convention exclusively because the Common Language Specification does not support it.
For extremely performance-sensitive code, you might want to provide special code paths for a small number of elements. You should only do this if you are going to special case the entire code path (not just create an array and call the more general method). In such cases, the following pattern is recommended as a balance between performance and the cost of specially cased code.
[Visual Basic]
Sub Format(formatString As String, arg1 As Object)
Sub Format(formatString As String, arg1 As Object, arg2 As Object)

Sub Format(formatString As String, ParamArray args() As Object)
[C#]
void Format(string formatString, object arg1)
void Format(string formatString, object arg1, object arg2)

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

Constructor Usage Guidelines
The following rules outline the usage guidelines for constructors:
• Provide a default private constructor if there are only static methods and properties on a class. In the following example, the private constructor prevents the class from being created.
[Visual Basic]
NotInheritable Public Class Environment
' Private constructor prevents the class from being created.
Private Sub New()
' Code for the constructor goes here.
End Sub
End Class
[C#]
public sealed class Environment
{
// Private constructor prevents the class from being created.
private Environment()
{
// Code for the constructor goes here.
}
}
• 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 performing further 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 private constructor. If you do not specify a constructor, many programming language (such as C#) implicitly add a default public constructor. If the class is abstract, it adds a protected constructor.
Be aware that if you add a nondefault constructor to a class in a later version release, the implicit default constructor will be removed which can break client code. Therefore, the best practice is to always explicitly specify the constructor even if it is a public default constructor.
• Provide a protected (Protected in Visual Basic) constructor that can be used by types in a derived class.
• You should not provide constructor without parameters for a value type struct. Note that many compilers do not allow a struct to have a constructor without parameters. If you do not 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 no difference in semantics between using an empty constructor followed by property set accessors, and using a constructor with multiple arguments. The following three code examples are equivalent:
[Visual Basic]
' Example #1.
Dim SampleClass As New Class()
SampleClass.A = "a"
SampleClass.B = "b"

' Example #2.
Dim SampleClass As New Class("a")
SampleClass.B = "b"

' Example #3.
Dim SampleClass As New Class("a", "b")
[C#]
// Example #1.
Class SampleClass = new Class();
SampleClass.A = "a";
SampleClass.B = "b";

// Example #2.
Class SampleClass = new Class("a");
SampleClass.B = "b";

// Example #3.
Class SampleClass = new Class ("a", "b");
Use a consistent ordering and naming pattern for constructor parameters. A common pattern for constructor parameters is to provide an increasing number of parameters to allow the developer to specify a desired level of information. The more parameters that you specify, the more detail the developer can specify. In the following code example, there is a consistent order and naming of the parameters for all the SampleClass constructors.
[Visual Basic]
Public Class SampleClass
Private Const defaultForA As String = "default value for a"
Private Const defaultForB As String = "default value for b"
Private Const defaultForC As String = "default value for c"

Public Sub New()
MyClass.New(defaultForA, defaultForB, defaultForC)
Console.WriteLine("New()")
End Sub

Public Sub New(a As String)
MyClass.New(a, defaultForB, defaultForC)
End Sub

Public Sub New(a As String, b As String)
MyClass.New(a, b, defaultForC)
End Sub
Public Sub New(a As String, b As String, c As String)
Me.a = a
Me.b = b
Me.c = c
End Sub
End Class
[C#]
public class SampleClass
{
private const string defaultForA = "default value for a";
private const string defaultForB = "default value for b";
private const string defaultForC = "default value for c";

public MyClass():this(defaultForA, defaultForB, defaultForC) {}
public MyClass (string a) : this(a, defaultForB, defaultForC) {}
public MyClass (string a, string b) : this(a, b, defaultForC) {}
public MyClass (string a, string b, string c)
}
Back to top Go down
https://knowledgeshare.forumotion.com
 
Coding Conventions in .net
Back to top 
Page 1 of 1
 Similar topics
-
» Coding Conventions in .net
» Coding Conventions in .net
» Coding Conventions in .net
» Coding Conventions in .net
» Coding Conventions in .net

Permissions in this forum:You cannot reply to topics in this forum
Knowledge Share :: .Net-
Jump to: