28th April 2009
You really should know and use these... seriously...
Simple things apparently annoy me a great deal...
.NET Developers should be aware of, and use, .NET constructs that have been around for ages:
- String.Empty - avoid creation of empty string references ("") use this built in single reference and the String.IsNullOrEmpty() method too.. the framework is there to help you!
- String.Format - avoid concatenation using + or &. Use string.format with the objects you intend to concatenate, it will also do the ToString for you and has formatting abilities.
- StringBuilder - strings are immutable. String builders avoid the creation of new strings each time you do a & or + concatenation.
- Using - Wrapping IDisposable instances in Using statements will automatically call dispose immediately on End Using. Additionally, implement IDisposable on heavy classes so that your own code can be consumed this way.
- Try Catch Finally - No catch should be empty, either decorate the exception with an appropriate wrapper before throwing again or remedy whatever the problem was because just plain eating exceptions makes debugging particularly crappy. Also use Finally to ensure that even when exceptions are thrown references are disposed of appropriately.
- 'As' (TryCast in VB) - Should be used where appropriate to cast and return null if the cast fails (rather than throw a cast exception).
- TryParse - Should be used instead of Try Catch blocks i.e. Integer.TryParse will return false if the parse failed, rather than throwing an exception.
- Nullable Types should be used for primitive types that can also be null i.e. int? or Nullable(Of Integer). This struct has a 'hasValue' property that can be accessed rather than building your own constructs such as -1 means no value, or 0 means no value etc.
- Generics - Do not use classes such as ArrayList or SortedList as they are object based and have the overhead of casting to and from object as well as forcing the user to do these casts. Compile time checking is also hindered. Instead use Generic Collections so that typing can be enforced i.e. Generic.SortedList<int, MyClass> or Generic.SortedList(Of Integer, MyClass).
Additionally look to use generics to create generalised code (avoid similar operations being redone in multiple specialised classes). Generics is not just applicable to classes. You can, and should if necessary, create Generic Methods e.g. ConfigurationSettings.GetValue<int>("keyName") allows casting and other checking to be encapsulated away from consuming code.
Obviously it's up to you in the context of each situation you encounter, but to never use these at all is surely madness.
Other Recent Posts
You may not know this but this blog has been xml based since its inception (in fact there's a longstanding, not yet achieved, task to 'replace' it
with a 'better' persistant storage mechanism -- clearly I must agree then, that the perfect is the enemy of the good). But anyway...
don't worry. I'm not about to do anotherblogaboutxml.
While reading a book the other day (C# in depth if you're interested... I'd recommend it to all you .net lovers out there..
yes you.. both of you) I suddenly realised something. I was holding a book.
Part of my role at Headscape has included looking at our development processes/practices.
There's a blog in this (and it's coming soon), but as a brief teaser to that:
So we're here... just, but with far less man points than we started... to be fair I think I've lost out to Ryan
on this one
Year in review and resolution posts may seem a bit passe, cliche and anything else that ends in an 'e' that sounds like
an 'a'... but let's get one out of our (my) system anyway.