Home .NET C#: code writing requirements and guidelines

C#: code writing requirements and guidelines

by admin

Nottoo long ago, in the comments to topic AlexS suggested the idea of using the Habra code agreement for .Net theme examples written in C#.
Idid a little research on the available offers from these sources :
submain.com/blog/FreeCVBNETCodingGuidelinesEbookDownload.aspx
idesign.net/idesign/DesktopDefault.aspx
and compiled a draft that describes the most basic rules for code written in C#.
Offer :
– Discuss this draft;
– Make any necessary changes to it;
– Approve it as a standard for writing C# code on Habra.
Furthermore, I suggest creating a document that could be offered as habrahabr comunity guidelines for all other C# programmers.
Under the footer you will find the text of the draft.I invite everyone to discuss it and give it a proper revision.

1. Requirements

1.1 Pascal casing

Describes the names of :
– all type definitions, including custom classes, enums, events, delegates, and structures;
– enumeration values;
– readonlyfields and constants;
– interfaces;
– methods;
– namespaces;
– properties;
– publicfields;

namespace SampleNamespace
{
enum SampleEnum
{
FirstValue,
SecondValue
}
struct SampleStruct
{
public int FirstField;
public int SecondField;
}
interface ISampleInterface
{
void SampleMethod();
}
public class SampleClass: SampleInterface
{
const int SampleConstValue = 0xffffff;
readonly int SampleReadonlyField;
public int SampleProperty
{
get ;
set ;
}
public int SamplePublicField;
SampleClass()
{
SampleReadonlyField = 1;
}
delegate void SampleDelegate();
event SampleDelegate SampleEvent;
public void SampleMethod()
{
}
}
} * This source code was highlighted with Source Code Highlighter

1.2 Camel casing

Describes the names of :
– local variables;
– method arguments;
– protectedfields.

protected int sampleProtectedField;
int SampleMethod( int sampleArgument)
{
int sampleLocalVariable;
} * This source code was highlighted with Source Code Highlighter

1.3 Suffixes and Prefixes

The following suffixes and prefixes are used :
– Custom exception classnames always end with the suffix "Exception";
– interfacenames always start with the prefix "I";
– user attribute names always end with the suffix "Attribute";
– event handler delegatenames always end with the EventHandler suffix;the names of the EventArgssuccessor classes always end with the EventArgs suffix.

public class SampleException: System. Exception
{
public SampleException()
{
}
}
interface ISample
{
void SampleMethod();
}
[System. AttributeUsage (System. AttributeTargets All, Inherited = false , AllowMultiple = true )]
sealed class SampleAttribute: System. Attribute
{
public SampleAttribute()
{
}
} public delegate void AnswerCreatedEventHandler( object sender, AnswerCreatedEventArgs e);
public class AnswerCreatedEventArgs: EventArgs
{
public int CreatedId;
public int ParentId;
public string CreatorName;
} * This source code was highlighted with Source Code Highlighter
* This source code was highlighted with Source Code Highlighter

1.4 Abbreviations

When using abbreviations in names, abbreviations with two characters must be capitalized, in other abbreviations only the first character must be capitalized.

namespace Sample.IO
{
}
class HttpUtil
{
} * This source code was highlighted with Source Code Highlighter

2. Recommendations

2.1 Method Naming

Use the verb-object construction to name methods

ShowUserInfo()

As a special case, for methods that return a value, use the "Get" verb-object pair for the verb and the description of the return value for the object.

GetUserId()

2.2 Variables, fields and properties

– When naming variables, avoid using acronyms like I and t , use index and temp Do not use Hungarian notation or use it only for closed members. Do not abbreviate words, use number , not num
– It is recommended that prefixes describing the element type be specified for control names. For example: txtSample, lblSample, cmdSample or btnSample. The same recommendation applies to local variables of complex types : ThisIsLongTypeName tltnSample = new ThisIsLongTypeName();
– Do not use public or protected fields, use properties instead;
– use automatic properties;
– always specify the access modifier private, even ifyou are allowed to omit it;
– always initialize variables, even when there is automatic initialization.

2.3 Additional recommendations

– use blank line between logical sections in source file, class, method;
– use an intermediate variable to pass the bool-value of a function result to a conditional expression if ;

bool boolVariable = GetBoolValue();
if (boolVariable)
{
} * This source code was highlighted with Source Code Highlighter

2.4 Code volume

– avoid files with more than 500 lines of code;
– avoid methods with more than 200 lines of code;
– avoid methods with more than 5 arguments, use structures to pass a large number of parameters;
– one line of code should not be more than 120 characters long.

3. Controversial points for discussion

3.1 Closed Fields

First variant
Closed field names always start with the prefix "m_" the rest of the name is described using Pascal Casing.

private int m_SamplePrivateField;

Second variant
Closed field names always start with the prefix "m" the rest of the name is described using Pascal Casing.

private int mSamplePrivateField;

The third option (so far the most likely to be final)
Closed field names always start with the prefix "_" the rest of the name is described with Camel Casing.

private int _samplePrivateField;

3.2 Additional requirements

– Always place opening and closing curly braces on a new line;
– always use curly braces for an if expression, even when the expression contains only one line of code;

You may also like