In our business we spend a lot of time talking about 'Best Practices' - ways
of doing things that over time have proved to be the most effective (or
accurate, or whatever..). There are a couple problems with Best Practices. One
is that there is no 'book' of them, they are scattered across a hundred books
and web sites and you often need one sentence out of all that information.
Another is that whether something is a Best Practice is often less than clear -
it depends on your attitude towards problem solving, your experience, your
situation. Finally, sometimes Best Practices (let's just use BP from here on)
can be a trap, keeping you from looking at solutions that clearly defy BP but
yet might be an ideal solution to your problem.
Interesting so far?
Between our discussion forums and email I receive, I see a lot of questions
about how to do things. It's interesting to participate in those discussions -
usually the person asking the question just needs to solve the problem and could
care less about BP - but it's often revealed that BP would have avoided the
question in the first place!
Another thing I see a lot is that the question the reader asks points to a
larger issue - something I call Worst Practices (WP). They are trying to solve
the wrong problem - we'll get to some examples shortly. As I thought about that,
it occurred to me that many readers might benefit from a discussion of why some
things are sooooo bad. The interesting thing about WP is I think they map to the
easiest path of solving a problem. I say that because I think if you give a
junior developer or DBA a problem, 8 times out of 10 their solution will be one
of brute force.
Now before I begin, please understand I'm not mocking anyone for asking a
question. One thing you'll notice in our discussion forums is a high degree of
politeness. Our mission here is to help our readers with SQL Server issues and
even issues that are closely related. Those beginner questions provide us with
an incredible opportunity to educate the reader who asked...and all those who
read the thread during and after the discussion.
In the months ahead I'll be addressing many WP items, but for today let's
start with a simple one - using Hungarian Notation for Column Names. If you're
not familiar with Hungarian Notation, take a quick look at this
article on MSDN. Basically it's a technique of naming your variables so that
they include both scope and data type information.
Declare @LoopCounter int
Declare @iLoopCounter int
In this example, the second declare uses Hungarian Notation - I've used the 'i'
prefix to denote that it's an integer value. Once you get used to it, it's a
huge time saver, you don't have to scroll back to the top to find the declare to
see it's data type. The down side of course is that when you need to change it
to a bigint, you have two choices:
Declare @biLoopCounter bigint
Declare @iLoopCounter bigint
You can do a search and replace on the variable name in your procedure to
reflect the data type (BP) or you can just change the data type (WP). Of course,
if you didn't use Hungarian then you just change the data type. I use VB a lot
and Hungarian is pretty common there, after a while it's comfortable and the
occasional extra effort of changing a variable name is more than offset by the
gains in readability. I'll leave it up to you to decide if Hungarian is a BP
Now let's look at something closer to our hearts, a table! Take a hard core
VB programmer who uses Hungarian for their grocery lists and what do you get for
Is that cool or what? Same naming syntax, same benefits of readability - why
would I call this a WP? Make a few tables using this naming convention. Then
build a few views, some stored procedures, maybe a user defined function or two,
plus have three or four developers coding against it for a month. Now decide
that a bit value for IsComplete isn't going to work, you're going to need to
change it to a tinyint so you can have more than two status values. Think the
column name will get changed? Ain't gonna happen! It's just too expensive for
too little return.
Agree with me? Or not?? Click on the discussion tab below and tell me what
you think about WP in general and about this specific example of a WP.
Read the other articles in this series.