Monday, January 09, 2006

Homily #1: Late binding is for code-gen only

Today's message: don't use late binding in any code that you (rather than the machine) wrote.

Why would you? No intellisense, no compiler safety net, diminished performance (in some cases). Even when you're sure you've got the right member, did you spell it right (and did you get the casing right, where applicable)?

It's not like it's hard to do either, eg:
  • Generate a strongly-typed dataset, and load the data into that instead, or
  • Use one of any number of code-gen tools to build objects from your data
  • For 'magic values' held in a database (or otherwise), use code-gen to import them into your code as constants / enumerations, and use them from there
It's the OnceAndOnlyOnce principal again: any time you build a 'wrapper' object like this you're centralizing the knowlege and maintanance of the binding contract in one place, and whilst you're not isolating the application from it (typically you'd expose members with names matching the data columns), any breakages are caught by the compiler.

No comments:

Popular Posts