473,574 Members | 2,347 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CSharp Coding Standards

http://www.auratius.co.za/CSharpCodingStandards.html

Complete CSharp Coding Standards
1. Naming Conventions and Styles
2. Coding Practices
3. Project Settings and Project Structure
4. Framework-Specific Guidelines
Naming Conventions and Styles
Use Pascal casing for type and method names and constants:

public class SomeClass
{
const int DefaultSize = 100;
public SomeMethod( )
{}
}
Use camel casing for local variable names and method arguments:

int number;
void MyMethod(int someNumber)
{}
Prefix interface names with I:

interface IMyInterface
{..}
Prefix private member variables with m_.

Suffix custom attribute classes with Attribute.

Suffix custom exception classes with Exception.

Name methods using verb/object pairs, such as ShowDialog( ).

Methods with return values should have names describing the values
returned, such as GetObjectState( ).

Use descriptive variable names.

Avoid single-character variable names, such as i or t. Use index or
temp instead.

Avoid using Hungarian notation for public or protected members.

Avoid abbreviating words (such as num instead of number).

Always use C# predefined types, rather than the aliases in the System
namespace. For example:

object NOT Object
string NOT String
intNOT Int32
With generics, use capital letters for types. Reserve suffixing Type
for when dealing with the .NET type Type:

//Correct:
public class LinkedList<K,T>
{...}
//Avoid:
public class LinkedList<KeyT ype,DataType>
{...}
Use meaningful namespace names, such as the product name or the
company name.

Avoid fully qualified type names. Use the using statement instead.

Avoid putting a using statement inside a namespace.

Group all framework namespaces together and put custom or third-party
namespaces underneath:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using MyCompany;
using MyControls;
Use delegate inference instead of explicit delegate instantiation:

delegate void SomeDelegate( );
public void SomeMethod( )
{...}
SomeDelegate someDelegate = SomeMethod;
Maintain strict indentation. Do not use tabs or nonstandard
indentation, such as one space. Recommended values are three or four
spaces.

Indent comments at the same level of indentation as the code that you
are documenting.

All comments should pass spellchecking. Misspelled comments indicate
sloppy development.

All member variables should be declared at the top, with one line
separating them from the properties or methods:

public class MyClass
{
int m_Number; string m_Name;

public void SomeMethod1( )
{}
public void SomeMethod2( )
{}
}
Declare a local variable as close as possible to its first use.

A filename should reflect the class it contains.

When using partial types and allocating a part per file, name each
file after the logical part that part plays. For example:

//In MyClass.cs
public partial class MyClass
{...}
//In MyClass.Designe r.cs
public partial class MyClass
{...}
Always place an open curly brace ({) in a new line.

With anonymous methods, mimic the code layout of a regular method,
aligned with the anonymous delegate declaration (this complies with
placing an open curly brace in a new line):

delegate void SomeDelegate(st ring someString);
//Correct:
public void InvokeMethod( )
{
SomeDelegate someDelegate = delegate(string name)
{
MessageBox.Show (name);
};
someDelegate("J uval");
}
//Avoid
public void InvokeMethod( )
{
SomeDelegate someDelegate = delegate(string name)
{MessageBox.Sho w(name);};
someDelegate("J uval");
}
Use empty parentheses on parameter-less anonymous methods. Omit the
parentheses only if the anonymous method could have been used on any
delegate:

delegate void SomeDelegate( );
//Correct
SomeDelegate someDelegate1 = delegate( )
{
MessageBox.Show ("Hello");
};
//Avoid
SomeDelegate someDelegate1 = delegate
{
MessageBox.Show ("Hello");
};

Coding Practices
Avoid putting multiple classes in a single file.

A single file should contribute types to only a single namespace.
Avoid having multiple namespaces in the same file.

Avoid files with more than 500 lines (excluding machine-generated
code).

Avoid methods with more than 25 lines.

Avoid methods with more than five arguments. Use structures for
passing multiple arguments.

Lines should not exceed 80 characters.

Do not manually edit any machine-generated code.

If modifying machine-generated code, modify the format and style to
match this coding standard.

Use partial classes whenever possible to factor out the maintained
portions.

Avoid comments that explain the obvious. Code should be self-
explanatory. Good code with readable variable and method names should
not require comments.

Document only operational assumptions, algorithm insights, and so on.

Avoid method-level documentation.

Use extensive external documentation for API documentation.

Use method-level comments only as tool tips for other developers.

With the exception of zero and one, never hardcode a numeric value;
always declare a constant instead.

Use the const directive only on natural constants, such as the number
of days of the week.

Avoid using const on read-only variables. For that, use the readonly
directive:

public class MyClass
{
public const int DaysInWeek = 7;
public readonlyint Number;
public MyClass(int someValue)
{
Number = someValue;
}
}
Assert every assumption. On average, every fifth line is an assertion:

using System.Diagnost ics;

object GetObject( )
{...}

object someObject = GetObject( );
Debug.Assert(so meObject != null);
Every line of code should be walked through in a "white box" testing
manner.

Catch only exceptions for which you have explicit handling.

In a catch statement that throws an exception, always throw the
original exception (or another exception constructed from the original
exception) to maintain the stack location of the original error:

catch(Exception exception)
{
MessageBox.Show (exception.Mess age);
throw; //Same as throw exception;
}
Avoid error code as method return values.

Avoid defining custom exception classes.

When defining custom exceptions:

Derive the custom exception from Exception.

Provide custom serialization.

Avoid multiple Main( ) methods in a single assembly.

Make only the most necessary types public; mark others as internal.

Avoid friend assemblies, as they increase interassembly coupling.

Avoid code that relies on an assembly running from a particular
location.

Minimize code in application assemblies (i.e., EXE client assemblies).
Use class libraries instead to contain business logic.

Avoid providing explicit values for enums:

//Correct
public enum Color
{
Red,Green,Blue
}
//Avoid
public enum Color
{
Red = 1,Green = 2,Blue = 3
}
Avoid specifying a type for an enum:

//Avoid
public enum Color : long
{
Red,Green,Blue
}
Always use a curly brace scope in an if statement, even if it contains
a single statement.

Avoid using the trinary conditional operator.

Avoid function calls in Boolean conditional statements. Assign into
local variables and check on them:

bool IsEverythingOK( )
{...}
//Avoid:
if(IsEverything OK( ))
{...}
//Correct:
bool ok = IsEverythingOK( );
if(ok)
{...}
Always use zero-based arrays.

Always explicitly initialize an array of reference types:

public class MyClass
{}
const int ArrraySize = 100;
MyClass[] array = new MyClass[ArrraySize];
for(int index = 0; index < array.Length; index++)
{
array[index] = new MyClass( );
}
Do not provide public or protected member variables. Use properties
instead.

Avoid using the new inheritance qualifier. Use override instead.

Always mark public and protected methods as virtual in a non-sealed
class.

Never use unsafe code, except when using interop.

Avoid explicit casting. Use the as operator to defensively cast to a
type:

Dog dog = new GermanShepherd( );
GermanShepherd shepherd = dog asGermanShepher d;
if(shepherd != null)
{...}
Always check a delegate for null before invoking it.

Do not provide public event member variables. Use event accessors
instead.

Avoid defining event-handling delegates. Use GenericEventHan dler
instead.

Avoid raising events explicitly. Use EventsHelper to publish events
defensively.

Always use interfaces.

Classes and interfaces should have at least a 2:1 ratio of methods to
properties.

Avoid interfaces with one member.

Strive to have three to five members per interface.

Do not have more than 20 members per interface. The practical limit is
probably 12.

Avoid events as interface members.

When using abstract classes, offer an interface as well.

Expose interfaces on class hierarchies.

Prefer using explicit interface implementation.

Never assume a type supports an interface. Defensively query for that
interface:

SomeType obj1;
IMyInterface obj2;

/* Some code to initialize obj1, then: */
obj2 = obj1 as IMyInterface;
if(obj2 != null)
{
obj2.Method1( );
}
else
{
//Handle error in expected interface
}
Never hardcode strings that will be presented to end users. Use
resources instead.

Never hardcode strings that might change based on deployment, such as
connection strings.

Use String.Empty instead of "":

//Avoid
string name = "";

//Correct
string name = String.Empty;
When building a long string, use StringBuilder, not string.

Avoid providing methods on structures.

Parameterized constructors are encouraged.

You can overload operators.

Always provide a static constructor when providing static member
variables.

Do not use late-binding invocation when early binding is possible.

Use application logging and tracing.

Never use goto, except in a switch statement fall-through.

Always have a default case in a switch statement that asserts:

int number = SomeMethod( );
switch(number)
{
case 1:
Trace.WriteLine ("Case 1:");
break;
case 2:
Trace.WriteLine ("Case 2:");
break;
default:
Debug.Assert(fa lse);
break;
}
Do not use the this reference unless invoking another constructor from
within a constructor:

//Example of proper use of 'this'
public class MyClass
{
public MyClass(string message)
{}
public MyClass( ) : this("Hello")
{}
}
Do not use the base word to access base class members unless you wish
to resolve a conflict with a subclass member of the same name or when
invoking a base class constructor:

//Example of proper use of 'base'
public class Dog
{
public Dog(string name)
{}
virtual public void Bark(int howLong)
{}
}
public class GermanShepherd : Dog
{
public GermanShepherd( string name) : base(name)
{}
override public void Bark(int howLong)
{
base.Bark(howLo ng);
}
}
Do not use GC.AddMemoryPre ssure( ).

Do not rely on HandleCollector .

Implement Dispose( ) and Finalize( ) methods based on the template in
Examples.

Always run code unchecked by default (for the sake of performance),
but explicitly in checked mode for overflow- or underflow-prone
operations:

int CalcPower(int number,int power)
{
int result = 1;
for(int count = 1;count <= power;count++)
{
checked
{
result *= number;
}
}
return result;
}
Avoid explicit code exclusion of method calls (#if...#endif). Use
conditional methods instead:

public class MyClass
{
[Conditional("My SpecialConditio n")]
public void MyMethod( )
{}
}
Avoid casting to and from System.Object in code that uses generics.
Use constraints or the as operator instead:

class SomeClass
{}
//Avoid:
class MyClass<T>
{
void SomeMethod(T t)
{
object temp = t;
SomeClass obj = (SomeClass)temp ;
}
}
//Correct:
class MyClass<Twhere T : SomeClass
{
void SomeMethod(T t)
{
SomeClass obj = t;
}
}
Do not define constraints in generic interfaces. Interface-level
constraints can often be replaced by strong typing:

public class Customer
{...}
//Avoid:
public interface IList<Twhere T : Customer
{...}
//Correct:
public interface ICustomerList : IList<Customer>
{...}
Do not define method-specific constraints in interfaces.

If a class or a method offers both generic and non-generic flavors,
always prefer using the generics flavor.

When implementing a generic interface that derived from an equivalent
non-generic interface (such as IEnumerable<T>) , use explicit interface
implementation on all methods, and implement the non-generic methods
by delegating to the generic ones:

class MyCollection<T: IEnumerable<T>
{
IEnumerator<TIE numerable<T>.Ge tEnumerator()
{...}
IEnumerator IEnumerable.Get Enumerator()
{
IEnumerable<Ten umerable = this;
return enumerable.GetE numerator();
}
}

Project Settings and Project Structure
Always build your projects with Warning Level 4 (see Figure E-1).

Treat warnings as errors in the Release build (note that this is not
the default of Visual Studio). Although it is optional, this standard
recommends treating warnings as errors in Debug builds as well.

Avoid suppressing specific compiler warnings.

Always explicitly state your supported runtime versions in the
application configuration file:

<?xml version="1.0"?>
<configuratio n>
<startup>
<supportedRunti me version="v2.0.5 500.0"/>
<supportedRunti me version="v1.1.5 000.0"/>
</startup>
</configuration>

Figure E-1. The project Build pane


Avoid explicit custom version redirection and binding to CLR
assemblies.

Avoid explicit preprocessor definitions (#define). Use the project
settings for defining conditional compilation constants.

Do not put any logic inside AssemblyInfo.cs .

Do not put any assembly attributes in any file other than
AssemblyInfo.cs .

Populate all fields in AssemblyInfo.cs , such as company name,
description, and copyright notice.

All assembly references should use relative paths.

Disallow cyclic references between assemblies.

Avoid multi-module assemblies.

Avoid tampering with exception handling using the Exception window
(Debug - Exceptions).

Strive to use uniform version numbers on all assemblies and clients in
the same logical application (typically, a solution). Use the
SolutionInfo.cs technique from Examples to automate.

Name your Visual Studio 2005 application configuration file
App.config, and include it in the project.

Modify the Visual Studio 2005 default project structure to your
project's standard layout, and apply a uniform structure for project
folders and files.

A release build should contain debug symbols (see Figure E-2).
Figure E-2. The Advanced Build Settings dialog

Always sign your assemblies, including the client applications.

Use password-protected keys.

Framework-Specific Guidelines
Multithreading
Use synchronization domains. Avoid manual synchronization , because
that often leads to deadlocks and race conditions.

Never call outside your synchronization domain.

Manage asynchronous call completion on a callback method. Do not wait,
poll, or block for completion.

Always name your threads:

Thread currentThread = Thread.CurrentT hread;
string threadName = "Main UI Thread";
currentThread.N ame = threadName;
The name is traced in the debugger Threads window, making debug
sessions more productive.

Do not call Suspend( ) or Resume( ) on a thread.

Do not call Thread.Sleep( ), except in the following conditions:

Thread.Sleep(0) is an acceptable optimization technique to force a
context switch.

Thread.Sleep( ) is acceptable in testing or simulation code.

Do not call THRead.SpinWait ( ).

Do not call Thread.Abort( ) to terminate threads. Use a
synchronization object instead to signal the thread to terminate.

Avoid explicitly setting the thread priority to control execution. You
can set the thread priority based on task semantics (such as
ThreadPriority. BelowNormal for a screensaver).

Do not read the value of the ThreadState property. Use
Thread.IsAlive( ) to determine whether the thread is dead or alive.

Do not rely on setting the thread type to background thread for
application shutdown. Use a watchdog or other monitoring entity to
deterministical ly kill threads.

Do not use the thread local storage unless thread affinity is
guaranteed.

Do not call Thread.MemoryBa rrier( ).

Never call Thread.Join( ) without checking that you are not joining
your own thread:

void WaitForThreadTo Die(Thread thread)
{
Debug.Assert(Th read.CurrentThr ead.ManagedThre adId !=
thread.ManagedT hreadId);
thread.Join( );
}
Always use the lock( ) statement rather than explicit Monitor
manipulation.

Always encapsulate the lock( ) statement inside the object it
protects:

public class MyClass
{
public void DoSomething( )
{
lock(this)
{...}
}
}
You can use synchronized methods instead of writing the lock( )
statement yourself.

Avoid fragmented locking.

Avoid using a Monitor to wait or pulse objects. Use manual or auto-
reset events instead.

Do not use volatile variables. Lock your object or fields instead to
guarantee deterministic and thread-safe access. Do not use
THRead.Volatile Read( ), Thread.Volatile Write( ), or the volatile
modifier.

Avoid increasing the maximum number of threads in the thread pool.

Never stack lock( ) statements, because that does not provide atomic
locking:

MyClass obj1 = new MyClass( );
MyClass obj2 = new MyClass( );
MyClass obj3 = new MyClass( );

//Do not stack lock statements
lock(obj1)
lock(obj2)
lock(obj3)
{
obj1.DoSomethin g( );
obj2.DoSomethin g( );
obj3.DoSomethin g( );
}
Use WaitHandle.Wait All( ) instead.

Serialization
Prefer the binary formatter.

Mark serialization event-handling methods as private.

Use the generic IGenericFormatt er interface.

Always mark non-sealed classes as serializable.

When implementing IDeserializatio nCallback on a non-sealed class, make
sure to do so in a way that allows subclasses to call the base class
implementation of OnDeserializati on( ).

Always mark unserializable member variables as non-serializable.

Always mark delegates on a serialized class as non-serializable
fields:

[Serializable]
public class MyClass
{
[field:NonSerial ized]
public event EventHandler MyEvent;
}
Remoting
Prefer administrative configuration to programmatic configuration.

Always implement IDisposable on single-call objects.

Always prefer a TCP channel and a binary format when using remoting,
unless a firewall is present.

Always provide a null lease for a singleton object:

public class MySingleton : MarshalByRefObj ect
{
public override object InitializeLifet imeService( )
{
return null;
}
}
Always provide a sponsor for a client-activated object. The sponsor
should return the initial lease time.

Always unregister the sponsor on client application shutdown.

Always put remote objects in class libraries.

Avoid using SoapSuds.exe.

Avoid hosting in IIS.

Avoid using uni-directional channels.

Always load a remoting configuration file in Main( ), even if the file
is empty and the application does not use remoting:

static void Main( )
{
RemotingConfigu rationEx.Config ure( );
/* Rest of Main( ) */
}
Avoid using Activator.GetOb ject( ) and Activator.Creat eInstance( ) for
remote object activation. Use new instead.

Always register port 0 on the client side, to allow callbacks.

Always elevate type filtering to Full on both client and host, to
allow callbacks.

Security
Always demand your own strong name on assemblies and components that
are private to the application, but are public (so that only you can
use them):

public class PublicKeys
{
public const string MyCompany =
"12345678948000 009400000006020 00000240000"+

"52534131000400 000100010007D1F A57C4AED9F0"+

"A32E84AA0FAEFD 0DE9E8FD6AEC8F8 7FB03766C83"+

"4C99921EB23BE7 9AD9D5DCC1DD9AD 23613210290"+

"0B723CF980957F C4E177108FC6077 74F29E8320E"+

"92EA05ECE4E821 C0A5EFE8F1645C4 C0C93C1AB99"+

"285D622CAA652C 1DFAD63D745D6F2 DE5F17E5EAF"+

"0FC4963D261C8A 12436518206DC09 3344D5AD293";
}

[StrongNameIdent ityPermission(S ecurityAction.L inkDemand,
PublicKey = PublicKeys.MyCo mpany)]
public class MyClass
{...}
Apply encryption and security protection on application configuration
files.

When importing an interop method, assert unmanaged code permission and
demand appropriate permission instead:

[DllImport("user 32",EntryPoint= "MessageBox A")]
private static extern int Show(IntPtr handle,string text,string
caption,

int msgType);
[SecurityPermiss ion(SecurityAct ion.Assert,Unma nagedCode = true)]
[UIPermission(Se curityAction.De mand,
Window =
UIPermissionWin dow.SafeTopLeve lWindows)]
public static void Show(string text,string caption)
{
Show(IntPtr.Zer o,text,caption, 0);
}
Do not suppress unmanaged code access via the
SuppressUnmanag edCodeSecurity attribute.

Do not use the /unsafe switch of TlbImp.exe. Wrap the RCW in managed
code so that you can assert and demand permissions declaratively on
the wrapper.

On server machines, deploy a code access security policy that grants
only Microsoft, ECMA, and self (identified by a strong name) full
trust. Code originating from anywhere else is implicitly granted
nothing.

On client machines, deploy a security policy that grants client
application only the permissions to execute, to call back the server,
and to potentially display user interface. When not using ClickOnce,
client application should be identified by a strong name in the code
groups.

To counter a luring attack, always refuse at the assembly level all
permissions not required to perform the task at hand:

[assembly:UIPerm ission(Security Action.RequestR efuse,
Window=UIPermis sionWindow.AllW indows)]
Always set the principal policy in every Main( ) method to Windows:

public class MyClass
{
static void Main( )
{
AppDomain currentDomain = AppDomain.Curre ntDomain;

currentDomain.S etPrincipalPoli cy(PrincipalPol icy.WindowsPrin cipal);
}
//other methods
}
Never assert a permission without demanding a different permission in
its place.

For more information goto

http://www.auratius.co.za

Regards

Auratius

Mar 29 '07 #1
19 3955
And your point? MS have a good set of standards / guidelines that
aren't very different, so I hop this didn't take you too long... I'd
also arge some of these... (not exhaustive...)

i.e. SomeClass<K, Tshould probably be SomeClass<TKey, TValue>

declaring members at the top... why? it adds no value in a modern
editor; keep it near (adjacent to) the property that manages it...

m_ : it's a marmite thing

brace layout : don't start that war!

your enum example is counter to the statement

"always mark public and protected methods as virtual"... woah that is
risky. If your class doesn't explicitely anticipate inheritance on a
method, it could be relying on the implementation. I'm not saying
don't use virtuals, but "always" is the road to madness

"do not provide public event member variables" - no such thing exists.
An "event" is explicitely not a field (variable); however "do not
provide public delegate fields that expose events" would be
reasonable.

EventsHelper - is that bespoke? never heard of it; neither has
Reflector

2:1 ratio methods to properties; rubbish; it should have what the
scenario requires

String.Empty vs. ""; no practial benefit given compiler support,
interning etc. I find "" clearer, so I use it

"Do not use the this reference unless" ... a few more usages
present... e.g. as "sender", testing object references, etc

lock(this) - is frowned upon in some circles; you don't know who else
can lock on your reference; lock on a private readonly object instead
(or a private collection if that is what you are synchronising)

Monitor.Pulse - why not? Works very cleanly without requiring an OS
object (AFAIK)

Marc
Mar 29 '07 #2

"Marc Gravell" <ma**********@g mail.comwrote in message
news:uY******** ******@TK2MSFTN GP05.phx.gbl...
String.Empty vs. ""; no practial benefit given compiler support, interning
etc. I find "" clearer, so I use it
I didnt read his recommandations but only your comments.

My 2 cents on String.Empty: as it's a static member of String, you probably
already know that each time you use it you point on the same instance of it.
Performance.

I did a quick test and compile a consoleapp where I compare strings with "".
The compiler doesn't substitute "" with String.Empy. So, I suppose, at
runtime, "" is *each time* converted to a new string object. Bad
performance.

Maybe I'm wrong, any CLR specialist could add his comments on this?

Mar 29 '07 #3

"Nobody" <no****@nowhere .orgwrote in message
news:uE******** ******@TK2MSFTN GP06.phx.gbl...

Maybe I'm wrong, any CLR specialist could add his comments on this?
Okay, after 5 minutes test and playing with ildasm.exe it can say that "" is
not replaced by String.Empty.

---8<---
IL_0001: ldstr "a"
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldstr ""
IL_000d: call bool [mscorlib]System.String:: op_Equality(str ing,
string)

IL_002a: ldc.i4.0
IL_002b: ceq
IL_002d: stloc.1
IL_002e: ldloc.1
IL_002f: brtrue.s IL_0037
IL_0031: ldstr "d"
IL_0036: stloc.0
IL_0037: ldloc.0
IL_0038: ldsfld string [mscorlib]System.String:: Empty
IL_003d: call bool [mscorlib]System.String:: op_Equality(str ing,
string)

---8<---
Mar 29 '07 #4
"Nobody" <no****@nowhere .orgschrieb im Newsbeitrag
news:uE******** ******@TK2MSFTN GP06.phx.gbl...

<snip>
>
I did a quick test and compile a consoleapp where I compare strings with
"". The compiler doesn't substitute "" with String.Empy. So, I suppose, at
runtime, "" is *each time* converted to a new string object. Bad
performance.
<snip>
Surely not, all string literal with the same value only constitute only one
stringinstance.

As long, as string values aren't constructed by operators or similar, there
will never be two equal string instances.

Christof
Mar 29 '07 #5
Hi,

"Marc Gravell" <ma**********@g mail.comwrote in message
news:uY******** ******@TK2MSFTN GP05.phx.gbl...
And your point? MS have a good set of standards / guidelines that aren't
very different, so I hop this didn't take you too long... I'd also arge
some of these... (not exhaustive...)
I'm impress that you took the time to read it :)
i.e. SomeClass<K, Tshould probably be SomeClass<TKey, TValue>

declaring members at the top... why? it adds no value in a modern editor;
keep it near (adjacent to) the property that manages it...
Agreed
m_ : it's a marmite thing

>
lock(this) - is frowned upon in some circles; you don't know who else can
lock on your reference; lock on a private readonly object instead (or a
private collection if that is what you are synchronising)
Jon Sket has a VERY GOOD explanation of why you should NEVER use lock(this)
Mar 29 '07 #6
Hi,

"Nobody" <no****@nowhere .orgwrote in message
news:uE******** ******@TK2MSFTN GP06.phx.gbl...
>
"Marc Gravell" <ma**********@g mail.comwrote in message
news:uY******** ******@TK2MSFTN GP05.phx.gbl...
>String.Empty vs. ""; no practial benefit given compiler support,
interning etc. I find "" clearer, so I use it

I didnt read his recommandations but only your comments.

My 2 cents on String.Empty: as it's a static member of String, you
probably already know that each time you use it you point on the same
instance of it. Performance.

I did a quick test and compile a consoleapp where I compare strings with
"". The compiler doesn't substitute "" with String.Empy. So, I suppose, at
runtime, "" is *each time* converted to a new string object. Bad
performance.
The last statement is incorrect, all string literals used in your code are
stored only once and that same reference is used all over the code.

It's the compiler decision if create a new reference to "" or use
String.Empty instead, in any case I see no difference in performance in
either case.
Mar 29 '07 #7

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions .comwrote in
message news:up******** ******@TK2MSFTN GP02.phx.gbl...
The last statement is incorrect, all string literals used in your code are
stored only once and that same reference is used all over the code.

It's the compiler decision if create a new reference to "" or use
String.Empty instead, in any case I see no difference in performance in
either case.

OKay, thanks Christof/Ignacio for your comments, i'm glad that i've learned
something today. My test was too simplistic, only 3 lines of code, didn't
tried with multiple "" in code.

Why the hell did they implement String.Empty then?

---8<---
public static readonly string Empty;
static String()
{
Empty = "";
WhitespaceChars = new char[] {
'\t', '\n', '\v', '\f', '\r', ' ', '\x0085', '\x00a0', '\u1680',
'\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005', '\u2006',
'\u2007', '\u2008', '\u2009', '\u200a', '\u200b', '\u2028',
'\u2029', '\u3000', '\ufeff'
};
}

I can understand that Environment.New Line can be different on different
platform, but an empty string is kinda always the same...
Mar 29 '07 #8
Am 29 Mar 2007 03:44:26 -0700 schrieb au******@gmail. com:
http://www.auratius.co.za/CSharpCodingStandards.html
Hello,

sorry, but much of this is a matter of taste. Like driving on the left side
or right side of the street. Different countries came to different
solutions.

Unfortunately, you did not give any reasoning for your "standards" . For
example, *why* do you discourage the use of single character variable
names? Pls give arguments for your recommendations . Without that, your
"Standards" are no better or worse than any of the many other already
existing recomendations.

In *my* coding standards, the according passage reads: "Variables with
longer lifespan should have longer, more descriptive names. Variables with
short lifespan should have shorter names. Longer names express what the
variable stores ("Person", "UserId"). Shorter names express what a variable
is used for (e.g. i, j as loop counters)".

And: I personally strongly recommend the use of the shortest meaningful
variable names - but no shorter.

OK - see the difference? And btw, "temp" is in no way any better than "t".

Some of your recommendations are really questionable, and are frowned upon
by most professionals.
>All member variables should be declared at the top, with one line
separating them from the properties or methods:

public class MyClass
{
int m_Number; string m_Name;

public void SomeMethod1( )
{}
public void SomeMethod2( )
{}
}
First of all, besides of personal preferences there are good reasons *not*
to write it like this. When we open a file to look at a class, we are
mainly interested in the public parts of the class - that is what we can
use. Private parts are implementation business and should not be visible at
all. From that follows that private methods and private variables should be
at the very end.

In *my* coding standards, I recommend to use the partial class feature to
separate public parts from implementation details. And I strongly recommend
to delegate work to the implementation parts. Sourcecode of public
functions should be very short.
About 80% Some of your recommendations are not even questionable, they are
plain dead wrong. I don't want to go into it deeper, but if someone wants I
can give more thorough analysis of many of the points. Simply one example:
>Always mark public and protected methods as virtual in a non-sealed
class.
Sorry, but the other way around is correct: "Never make a public function
virtual", or "all virtual functions should be non-public (which leaves
protected, of course.)

Why? Because the publicly exposed interface of a class is defined by its
public members (functions, here.) Whether a class allows to "plug in"
another implementation for a certain service it offers, and how this is
technically done, is an implementation detail. Such a plug-in *can* be
realised by making a function virtual: we "plug-in" another implementation
by deriving from the class and overriduing the function. BUT - and thats
the important part - the *interface to the class* must remain the same. For
example, checking of preconditions should be in the public function. If
this were virtual, one could simpley derive and omit the checks..... no
good idea. (BTW, there are other techniques to plug-in functionality:
delegates are an example).
OK, one more.......
>Assert every assumption. On average, every fifth line is an assertion:

object someObject = GetObject( );
Debug.Assert(s omeObject != null);
Sorry, completely wrong.

- if GetObject's contract says that it must return an object, then we do
not need to test it after the call. (Of cource, GetObject will throw an
exception should it find that it cannot fulfil its contract).

- but if Getobject's contract says that it can return null, then we
eventualy must write

object someObject = GetObject( );
if ( someObject != null )
{
...
}

etc.
My 2 cents....
Paule

Mar 29 '07 #9
On Mar 29, 5:44 am, aurat...@gmail. com wrote:
>
[snip]
Always have a default case in a switch statement that asserts:

int number = SomeMethod( );
switch(number)
{
case 1:
Trace.WriteLine ("Case 1:");
break;
case 2:
Trace.WriteLine ("Case 2:");
break;
default:
Debug.Assert(fa lse);
break;

}
I like this pattern as well.

void Foo()
{
int number = SomeMethod();
switch (number)
{
case 1: /* blaa blaa */ return Something;
case 2: /* blaa blaa */ return SomethingElse;
}
return DefaultValue;
}
>
Do not use the this reference unless invoking another constructor from
within a constructor:
I care that much as long as the this keyword is used consistently or
not at all. Using it only half the time is pretty annoying.
Never call Thread.Join( ) without checking that you are not joining
your own thread:
That's too restriction. In fact, I can't think of situation where any
of my code could call Join on its own thread.
Mar 29 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2122
by: Jonathan Grobe | last post by:
From: Nigel Perry <nigel@cosc.canterbury.ac.nz> Newsgroups: news.announce.newgroups,news.groups Subject: RFD: comp.std.csharp Date: Tue, 08 Jul 2003 00:26:01 +0000 REQUEST FOR DISCUSSION (RFD) unmoderated group comp.std.csharp This is a formal Request For Discussion (RFD) for the creation of the worldwide unmoderated newsgroup...
3
2041
by: ganesan | last post by:
Hi Guys, Could any one knows the best coding standards styles(with variable declarations for c#) . and if any links or site with the best coding standards for .NET send me those links regards Ganesan
4
2282
by: dotNetDave | last post by:
About three weeks ago I released the first .NET coding standards book titled "VSDN Tips & Tricks .NET Coding Standards". Here is what the famous author/ speaker Deborah Kurata says about it: "David McCarter once again demonstrates his knack for pulling best practices into one cohesive unit with his new book "VSDN Tips and Tricks: .NET Coding...
5
5395
by: db2sysc | last post by:
ALl. Is it possible to get MS ACCESS CODING STANDARDS? TIA
7
2254
by: Eric Gunnerson | last post by:
If you would like to see this group created, please reply to this post, making sure you leave news.groups on the newsgroups line. "Nigel Perry" <nigel@cosc.canterbury.ac.nz> wrote in message news:1057623961.29376@isc.org... > REQUEST FOR DISCUSSION (RFD) > unmoderated group comp.std.csharp > > This...
144
6782
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force...
7
4935
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already developed significant content for the C programming language that is available at: https://www.securecoding.cert.org/ by clicking on the "CERT C...
3
1769
by: editormt | last post by:
A recent poll asked if programming standards are used by development organisations... and if they are controlled. None: 20% Yes, but without control: 49% Yes, with control: 31% Participants: 369 Source: Methods & Tools (http://www.methodsandtools.com)
0
1660
by: pat | last post by:
CodeCheck Coding Standard's Support As a free service to our customers we offer support in developing "rule-files" for automating corporate coding standards. If you have a coding standard that you wish to automate please send the standard to us in PDF format so we can assist in developing the automation with codecheck. If you are...
0
7828
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7742
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8089
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8260
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8127
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6493
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3765
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2257
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.