Vous êtes sur la page 1sur 7

Sapphire Coding Standards

VERSION 1.0

Sapphire Coding Standards


Table of Contents
Introduction ..................................................................................................................................................3
General..........................................................................................................................................................3
Tabs ........................................................................................................................................................................3
TODO Items ............................................................................................................................................................3
Statements .............................................................................................................................................................3
Logic Evaluation Order ...........................................................................................................................................3
Use Constants .........................................................................................................................................................3
Parameter Types.....................................................................................................................................................4
Object Design .........................................................................................................................................................4
Unused Code ..........................................................................................................................................................4
C# ..................................................................................................................................................................4
Comments ..............................................................................................................................................................4
Declaration .............................................................................................................................................................4
Initialization ............................................................................................................................................................4
Naming ...................................................................................................................................................................5
Properties............................................................................................................................................................5
Fields ...................................................................................................................................................................5
Constants and ReadOnly Variables.....................................................................................................................5
Classes and Interfaces ........................................................................................................................................5
Method Signatures .............................................................................................................................................6
Conditionals and Loops ..........................................................................................................................................6
Nullable Operators .................................................................................................................................................7
Try/Catch Blocks .....................................................................................................................................................7

Page | 2

Sapphire Coding Standards

Introduction
This documents purpose is to establish standards and guidelines for applications written in C#. Adhering to
coding standards ensures a consistent code base for product life cycle management. Current and future
developers will have an easier time determining application structure and code meaning.

General
Tabs
Visual Studio should be set up to use spaces for tabs. Each tab should be 4 spaces.

TODO Items
Use the TODO keyword in comments on sections of code that need additional work done.

Statements
Each statement should start on its own line. Statements can be broken over multiple lines for readability.

Logic Evaluation Order


Place Boolean evaluations first in logic evaluation expressions.
Correct:
if (isPending && checkAccess(feature))
{
//
}

Use Constants
Define constants or static readonly variables instead of magic numbers and strings.

Page | 3

Sapphire Coding Standards


Parameter Types
Use the most specific parameter type available. For example, use a generic List<T> instead of ArrayList or
PropertyChangedEventArgs instead of EventArgs.

Object Design
Create loosely coupled components. Provide interfaces that components can depend on rather than concrete
classes. Classes should have a single purpose.

Unused Code
Prefer removing code to commenting it out. You can retrieve it from SCM if it is needed later.

C#
Comments
For reference, see The Fine Art of Commenting
(http://www.icsharpcode.net/technotes/commenting20020413.pdf).
For documentation use XML comments preceded by three slashes.
Correct:
/// <summary>
/// Displays the About dialog
/// </summary>

Declaration
Use descriptive variable names. Do not use Hungarian Notation. Declare one variable per line.

Initialization
Initialize a variable as close to its declaration as possible.
Correct:
var time = DateTime.Now;

Page | 4

Sapphire Coding Standards


Incorrect:
var time = null;
//...
time = DateTime.Now;

Naming
Properties
Properties and public members should be Pascal Case. Braces for properties should be on their own line
unless using Automatic Properties.
Correct:
public string AccountNumber { get; set; }

Or:
private string _accountNumber;
public string AccountNumber
{
get
{
return _accountNumber;
}
set
{
_accountNumber = value;
}
}

Fields
Fields and private members should be Camel Case with a leading underscore. Eg. _accountNumber

Constants and ReadOnly Variables


Const and ReadOnly fields should be Pascal Case.

Classes and Interfaces


Class and interface names should be Pascal Case. Interface names need not be prefixed with I. Braces
around a class definition should be on their own line.

Page | 5

Sapphire Coding Standards


public class ExampleClass
{
//...
}

Namespaces
Namespaces should be Pascal Case. They should start with the organization name, followed by a period, then
followed by the application or library name, then additional labels separated by periods.
Correct:
CynergySystems.Commons
MyCompany.MyApp.Controls
MyCompany.Services.Tests

Method Signatures
Method names should be Pascal Case. Parameter names should be Camel Case. If a method requires many
parameters, break them into multiple lines.

Conditionals and Loops


Braces for all conditionals and loop statements should each have its own line. Counter variables may be a single
letter, non-descriptive name such as i, j, k.
Correct:
if (PropertyName == lastName)
{
//...
}
else if (PropertyName == firstName)
{
//...
}
if (Container.Items.Count > 5)
{
for (int i = Container.Items.Count - 1; i > 0; --i)
{
//...
}
}

Ternary Operator Example


var nextImage = (ImageColumnOne == null) ? ImageColumnTwo : ImageColumnThree;

Page | 6

Sapphire Coding Standards


Nullable Operators
Prefer the ? operator to System.Nullable<T>.
Example
int? c = null;
//...
int d = c ?? -1;

Try/Catch Blocks
Each brace in a try block should be on its own line. Catch may be on its own line or on the same line as the
ending try brace.
Correct:
try
{
//...
}
catch (StyleNotFoundException)
{
// ...
}
try
{
//...
} catch (StyleNotFoundException ex)
{
// ...
}

Page | 7

Vous aimerez peut-être aussi