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
Having not made the trip down to Bournemouth for re:develop 1 or 2 it was great to be in attendance for it's
third incarnation.
Making games over a weekend...
competitively... and we chose a dead technology... why the hell not!
Hack Days are awesome. How could they not be? you get to make stuff
with like minded people with no bosses, no client deadlines, no point but the love of it.
It's been a while since I posted. I'd like to say that's because a lot's been going on. In reality I got lazy and now I just happen to have
something to write about that can make it sound like a lot has been going on.