Chris Donnan : Programming - Brooklyn Style
software, trading, family, fun
Posted c#, coding on Saturday, November 5th, 2005.
I was doing some pair programming with a dev from my current client’s UK office the other day. I had an immediate set of reactions when coding for the 1st time with a person I have never programmed with before. We had a quickie debate on casting style - c style vs ‘as’ style casting. I am generally an ‘as-ist’.
Coding style is something to talk about for sure. In the past - I have been able to just basically mandate standards. On my new project - I have the intersting challenge of convincing people that ‘my’ coding standards are useful. There are a few things that have come under debate lately.
class Foo
{
private int _aThingId = 0;
// vs.
private int aThingId = 0;
...
...
}
2nd - I was arguing for the performance of casting. I could not recall an article to support my viewpoint that using ‘as’ style casts was prefereable for speed and also in order to remove those nasty ‘ice’s (invalid cast exceptions). ‘as’ is especially more performant when comparing vs ‘is’ compare THEN c style cast. Anyhow an article supporting the speed of ‘as’ vs c style casting is right here
class Foo
{
private void DoSomething( object foo )
{
Foo f = foo as Foo; //(null check later for invalid cast - no exception)
// vs. (potential invalid cast exception)
Foo f = (Foo)foo;
// or
Foo f;
if( f is Foo )
f = (Foo)foo;
}
...
...
}
3rd - using ‘this’ all over the place as an alternative to _ or simply in addition to _ in a class. I claim ‘never use this’ except in the ctor.
class Foo
{
private int val = int.MinValue;
/// this is OK by me and all others
public Foo( int val )
{
this.val = val;
}
/// this is NOT OK by me and many others
public void DoSomething()
{
if( int.MinValue == this.val )
{
//Do something useful
}
}
...
...
}
“3.2.32. Rule 3@505: Only use the this. construction to avoid a name clash
Do not use the this. construction to dereference members. The use of this is only allowed as a reference to
the current class instance or to prevent name clashing between method parameters and class fields.”
I am sure there will be many more coding standards debates. I am sure that I will win some and lose some. I tend to be code anal. I hope that I can lighten up enough to play nice
Either way - I am sure that we will be turning out lots of higher quality code for the project…..
More to come…
-CD
Comment on this post below
You must be logged in to post a comment.
You can leave a response, or trackback from your own site.















