June 15, 2021 by Kenneth Fisher
I remember when I was first learning to code there was always the argument of how long a name should be. For example the table msdb.dbo.log_shipping_monitor_history_detail. That particular table name is 35 characters long, and it’s not the longest table name I’ve ever seen. Can you imagine typing that a dozen times in a query? How much easier would it be if the name was significantly shorter?
On the other hand, can you imagine if it was just named lsmhd? Or just tab001? And yes, I’ve seen object names similar to both. Much easier to type, knowing what it is on the other hand, not so easy.
Things like aliases and synonyms have made this less of an issue but it’s still an important consideration. Long and easy to understand, or short and easy to type. As I’m sure is obvious by now, I thought I’d throw in my two cents. And to be fair, this is entirely my opinion on this, you are welcome to have your own.
- Object names should be as long as necessary to be completely readable and understandable. That means if your table name needs to be 100 or even 200 characters long for someone to be able to walk up and say “Hey, That’s where I can find xyz!” then that’s what you do.
- Within a query every object should be qualified. i.e. at least a two part name depending on the type of object and if it’s in the current database. This means that column names should include the table, table names should include at least the schema, possibly the database etc.
- Should be used if the original name is longer than you feel like typing throughout the rest of the query.
- Don’t have to be readable. Short is more important here. So lsmhd would actually work for an alias for the table above.
- Should be used in such a way that in the end your query is more readable, rather than less.
That last line is everything. Your naming conventions, for all types of objects and aliases, should make things easier to understand. For every time you type your code out, you (or someone else) will be reading it dozens of times (at least). Guess which I think is more important.