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
For the last few weeks I've been pulling together the concept of 'The Barn'. Ostensibly it's a company blog, but to me it's a bit
nicer than that.
Having made the trip twice before I was looking forward to Barcamp Bournemouth. It's probably my favourite small event. Partly because
it's so close, partly because it's a great venue but mainly because there's always something interesting and new (at least new to me)
going on.
Ok so if you follow me on twitter you may know that towards the end of last year I took part in
'Alphalabs'. Organised by onedotzero this was a
competition aimed at encouraging developers and artists to work together on the Lumia 800 platform.
Apparently doodling can be good for you. Although when I do it, it's not so good for
Ed Merritt.
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.