Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
NET_Coding_Guidelines.doc
Скачиваний:
3
Добавлен:
12.11.2019
Размер:
175.62 Кб
Скачать

4.Code formatting

4.1Files and folders

Place every top-level type in the separate file (whether type public or note). Folder structure should correspond to the namespaces.

For example, class Itransition.Framework.Collections.RandomList should be placed in the “…/Itransition/Framework/Collections/RandomList.cs” file.

4.2Indentation, spaces and braces

The following rules outline the formatting guidelines for indentation and braces:

  • Indentation should be 4 space characters (tabs usage is prohibited).

  • Braces should not reside on the same line as related construct.

  • Case labels should use indentation.

  • All flow control primitives (if, else, while, for, do, switch) shall be followed by braces, even if they have single operation.

  • Methods, properties, fields, events and nested types should be separated by single empty line. Related fields can be placed on the sequential lines however.

  • When instruction spares several lines, second and subsequent ones should be indented with 4 space characters.

  • If necessary, method logic can contain empty lines if it increases understandability (however, small methods strongly encouraged).

For example:

public class Point : EventArgs

{

double x;

double y;

public Point(double x, double y)

{

this.x = x;

this.y = y;

}

public double X

{

get

{

return x;

}

}

public int Y

{

get

{

double y;

}

}

public PointType GetType()

{

double radius = x * x + y * y;

if(Math.Abs(radius – 1.0) < 0.000001)

{

return PointType.Periodic;

}

if(radius < 1.0)

{

return PointType.Converging;

}

else

{

return PointType.Divergent;

}

}

}

4.3Separation of operators and punctuators

Space character should be placed before and after the following operators and punctuators (if operator or punctuator is placed before line break, the space after it unnecessary):

+ - * / % & | ^ = < >

&& || << >> == != <= >= += -= *=

/= %= &= != ^= <<= >>=

Space before opening round bracket is optional.

Comma should be followed by space (but space is disallowed before comma).

Operators and punctuators can end line, but should not start line.

4.4Line length

It’s recommended not to create source code lines longer than 80 characters.

5.General style recommendations

5.1Structures and classes

Do not declare structures (except for unmanaged code interoperability).

5.2Ref and out parameters

Do not use ref and out parameters (except for unmanaged code interoperability).

5.3Operator overloading

Do not use operator overloading.

5.4Collections

Always try to use System.Collections.Generic classes instead of System.Collections ones as much as possible.

5.5Partial types

Do not put declaration of the single type in several files (do not use partial types until it is forced by IDE).

5.6Anonymous methods

Use anonymous methods cautiously. Generally, apply the following rules:

  • Use anonymous method if you need up to five code lines;

  • Use anonymous method if you need capture one or two external variables.

5.7C# 3.0 features

5.7.1Lambda expressions

  • Prefer methods over lambda expressions when the same code is used repeatedly.

  • Prefer lambda expressions where anonymous delegates would have been appropriate in C# 2.0.

5.7.2Extension methods

  • Try to avoid extension methods usage.

  • Put extension methods in their own static class.

  • Consider grouping extension methods that extend a particular class into a single static class, and name that class <ClassName>Extensions.

  • Keep extension method classes in their own namespace to mitigate potential name collisions (if you run into a name collision you're forced back to using a static method call).

5.7.3Anonymous types

Do not use anonymous types (except for very short-lived data in LINQ).

5.7.4Implicitly types local variables

  • Prefer explicitly-typed local variables.

  • Do not use var with intrinsic types.

  • Use var with LINQ statements where the result is not an intrinsic type.

5.7.5Object initializers

Prefer object initializers to one-property-per-statement object/element initialization.

5.7.6Automatic properties

Prefer automatic properties over class field when property acts like a simple wrapper.

6.References

The following materials are referenced within the document:

  1. .NET Framework General Reference - Naming Guidelines

  2. C# Language Specification

7.Glossary

The glossary contains terms and abbreviations that you should be familiar with when reading the document:

Pascal Case

The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. You can use Pascal case for identifiers of three or more characters. For example:

BackColor, TransformationMatrix, DodgeTypeJawCrusher

Camel Case

The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. For example:

backColor, transformationMatrix, dodgeTypeJawCrusher

Uppercase

All letters in the identifier are capitalized. For example:

IO, UI

2, Melnikayte Str.,

Tel:

+375 17 2226108

www.itransition.com

Minsk, 220004, Belarus

Fax:

+375 17 2267170

info@itransition.com

Copyright © 2007 Itransition. Proprietary and confidential. For internal use only.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]