Coding Standards

From Logic Wiki
Jump to: navigation, search
Abbreviations.PNG

Coding Standards

Having everyone write code in a similar way makes the codebase more consistent and readable for everyone. Therefore we have various coding guidelines for things that are considered important to keep consistent.

In general there are 3 standards exist in Notation/Casing

Hungarian Notation

it_is_a_variable

Camel Casing

itIsAVariable

Pascal Casing

ItIsAVariable


C# Naming Conventions

lLocalVariable

mMemberVariable

xiInputParameters

xoOutputParameters

xbParametersByReference

parameterInLambdaExpressions

CONSTANTS

IInterface

Iteration variables: i (i.e. not ii)

eEnumType

EnumCases

Others ... ?

SQL Naming Conventions

preTableName : "pre" represents a prefix like "app" "sys"

ID : Table's primary key ID is always named as ID

TableNameID : Foreign Keys

Table1Table2Cross : cross reference table name

qq comments

qq is a temporary marker indicating something that should be resolved before being committed. In theory these shouldn't be left in completed code, although in the past (very) many have slipped through. The correct syntax is qq:INITIALS. The reason we use "qq" is to make it easy to search for (it won't appear in the middle of any words!), and initials are appended to help find out who added it (and to make it easier to find all your qqs before you commit). When you do want to commit a qq-like comment, better versions of it are:

TODO

use this if there's something that the next person editing this code should consider doing, but which could happily be left unchanged forever if no-one happens to look at this code again.

//todo: fix dialog windows
//TODO: work on SQLCE guide
// Todo new screenshot

BUG

BUG:xxx - use this if there's something that needs to be fixed at some point. Raise a bug in http://s3k.me/bug, and replace xxx with its number.

Regexes

Some guidelines on using regexes to bear in mind for code that uses them is at WritingRegularExpressions.

Usage of "var"

Yes, I like that: Anything which has an explicit type defined, especially if it will save space and improve readability.

Example 1:

using (AuditComponent lAuditComponent = new AuditComponent())

Here, we save space (even more useful for certain RW types). Seeing as the assignment specifies a type, we don’t lose any readability/clarity either. .

Example 2:

var lHotel = (HotelDataSet.Hotel)lDataRow;

Anything where a cast makes the type obvious is fine here too as in the above example.

Do I not like that? Yes. I don’t like that:

In case it isn’t clear, these are cases where you shouldn’t be using var:

Example 1:

ConditionType lConditionType = ConditionTypeAccessor.GetConditionTypeFromCache((int)xiConditionToCheck["ConditionTypeId"]);

You could certainly guess the type returned by GetConditionTypeFromCache; it would however still be a guess so in this case, you should not be using var.

Example 2:

int lNights = 0;

Primitive types should not be var’d. There isn’t really an obvious benefit from varring a bool or an int so we shouldn’t; it is arguably also less clear what the type of the variable is. Resharper will just have to remain upset about these!


Quick Sheet

public class HelloWorld{}
public void sayHelloWorld(){}
int totalCount - 0
void SayHello(string nameToSayHello)

HelloWorldController.cs

Do Not Use

  • string addr
  • n_hungarianNotation
  • appbrevations longer then 5 chars
  • underscore prefix for local variables
  • Hardcoded numbers or strings -> Use constants or resource files
  • 1000 lines or more ine a file
  • /* ... */
  • //*************

Use

  • string address
  • Meaningful descriptive words for naming variables
  • Can, Is, Has prefixes for boolean variables
  • All members variables must use Underscore prefix
  • Root namespace as product, company or developer's name
  • TAB (size 4) as indentation
  • 1 blank line to seperate logical groups of code
  • #region to group
  • Declare readonly / static readonly variables instead of constants for complex types
  • Explicitly declare all identifiers -> Protected void SomeMethod(...
  • Convert strings to lower or upper before comparing
  • String.Empty instead of ""
  • String.Builder instead of string
  • //
  • finally block for releasing used resources

Grouping and Sequence

  1. Member Variables
  2. Constructors and finalizers
  3. Nested Enums, Struct, Classes
  4. Properties
  5. Methods

Based on Access modifier

  1. Public
  2. Protected
  3. Internal
  4. Private


Link

http://dotnetdaily.net/wp-content/uploads/downloads/2011/07/coding-standards-csharp.pdf