SQLServerCentral Editorial

Decorations

,

This week's editorial is a guest post from Louis Davidson.

I was whistling contentedly as I hung up the Christmas Decorations. It may have been ‘Jingle Bells’, or it could have been ‘Santa didn’t buy me a bow-wow’: Christmas songs are creepily similar.  As I hung up the fairy lights on the Christmas tree, I was struck by the thought ‘Decorations are not decorations at all, but a communication’. I stood back to inspect critically what I’d done and hurriedly drew the curtains together.

As DBAs, we are called on to write a lot of code. Code to do stuff such as schedule jobs, backup databases, create databases and objects like tables and stored procedures, create and give security to principles, query data, and many other tasks. While none of these tasks is exactly like the other, they all share a common problem. When we get back from celebrating Christmas and/or the New Year, having eaten and drunk our weight in Egg Nog, we need to remember what the code does.  This is where decorations come in: they are there to communicate. Just as you can get an idea what a person is like from their Christmas decorations, you can tell a lot about a person’s code by looking at how they decorate their code in three particular ways.

Formatting

SQL code is amongst the worst code to parse without proper formatting. The following line of code is actually multiple statements that would work :

However, it is far better to write:

The code is still gibberish, but now it is readable gibberish that we can see approximately what is going on. But we are not finished decorating this code.

Naming

Now we need to give better names to objects. My names are clearly terrible. So let’s apply a bit of better naming:

Ah, now it is a bit more clear that we are doing something with a tree and ornaments. From the names, I can see that we are deleting ornaments from a tree, apparently based on size… But why? One last bit of decoration to add

Comments

Now the reader of this code can easily tell what this code does, why it does it, and use accordingly. Nothing we did changes the functionality whatsoever. With a little work, we could figure out the format using a tool like SQL Prompt, and we could figure out from the data that O was ornament, and T was tree, and what all of the code actually does.

But you will thank yourself for decorating your code when you come back from a well-deserved break… And even better, you will never need to take them down!

Happy Celebrations to you and yours. See you next year!

Rate

3.25 (4)

You rated this post out of 5. Change rating

Share

Share

Rate

3.25 (4)

You rated this post out of 5. Change rating