views:

400

answers:

5

We are in the process of upgrading our projects from C# 2.0 / VS2005 to C# 3.0 / VS2008. As part of the upgrade, we are adding some items to our coding standards document.

How would (or did) you change your coding standards document when upgrading from C# 2.0 / VS2005 to C# 3.0 / VS2008?

+2  A: 

The updgrade coincided with a new project, so when we moved we started using tools like StyleCop and FxCop which altered our coding standards. Also it handily enforces them too :o)

DeletedAccount
+1  A: 

Nothing should change per se due to the upgrade, though you may need to look at coding standards around new features, such as LINQ expressions, layout, Lambda versus query syntax.

johnc
+7  A: 

You could/should give advice about:

  • When to use query expressions vs dot notation
  • Any restrictions on the use of lambda expressions (e.g. "don't modify captured variables). (This could also apply to anonymous methods in C# 2 of course.)
  • When to write extension methods
  • When to use implicitly typed variables (var)

The last two of these cause some controversy, particularly var.

If your conventions give any design guidelines, I'd suggest that you also advise programmers to consider using delegates for specialisation where previously they might have used inheritance or interfaces. A good example of this is sorting - it's easier (and more readable) to use a projection to specify a sort order than to write an implementation of IComparer<T>.

Jon Skeet
A: 

My standards for new features for 2008:

  • Use var sparingly only with anonymous types.
  • Encourage use of lambda expressions over delegates.
  • Only use extension methods when you don't have control of the source code
+1  A: 

My personal pet peeve is the usage of var wherever "possible".

"Possible" being currently defined as one of the following cases, mostly in order of decreasing neatness:

Obvious, helping DRY:

var obj1 = new Something();
var obj2 = (Something)ObscureFunction();
var obj3 = ObscureStuff() as Something;

Guarded, I don't care as long as it compiles:

var obj4 = ObscureFunction();
foreach(Something s in obj4) { ... }

Complex Generics and almost any LINQ result:

var obj5 = ctx.GetQuery<Something>()..ToList(..)..GroupJoin(..)...ToLookup(...);
David Schmitt