All you need to know about coding standards for SQL queries

SQL   |   
Published September 24, 2019   |   

Coding standards are a set of guidelines, best practices, programming styles and conventions that developers adhere to when writing a piece of code for a project. All the big software companies have them. And advise their developers to embed these coding standards in deliverables. Especially before deployment in a production environment.

Why do we need a coding standard?

It is highly recommended to implement coding standards during the building of SQL codes. Since at times they can become very difficult to interpret by someone who was not part of the initial development. For developers, coding standards are important for the following reasons.

  • They make it easy to debug during development and troubleshoot issues that arise in production
  • It’s easier to interpret the logic while revisiting the code for any logical changes
  • Knowledge transfer of the code becomes frictionless. A new resource can understand the code flow with better readability
  • Allows for plenty of time savings in all the activities, since there’s a single standard for the whole team
  • And it’s always a best practice to learn the industry standards

On the other hand, following a coding standard is beneficial for your clients as well. Here’s how,

  • Better code durability and readability
  • Less turnaround time in case of future changes
  • Lesser requirement for clarification queries

In this article, we shall delve into how this can be implemented. These processes are fairly simple and very easy to adhere to. Overall, this would just take an additional 10% of effort during the development of the code. But this saves a lot of effort in the subsequent analysis. Which is why it’s for us as well as clients. Especially when an unforeseen issue arises.

How do coding standards really affect the code?

Let’s take an example to SQL code illustrate this,

As-is (for illustrative purpose only. Database names are hidden deliberately)

arun 1

Here’s the same code, but with coding standardsarun 2

arun 3

 

arun 4

 

 

 

 

 

 

 

 

 

 

 

 

Here are a few points to keep in mind:

  • Firstly, didn’t the code implemented with coding standards as above look better than the As-Is case?
  • Were you able to figure out the logic implemented more easily?
  • Were you able to easily spot the joins, where clauses, group by’s etc., more easily?
  • Can the trouble shooting be done more quickly?

And that’s why coding standards matter!

A closer look

Screenshot 144

 

 

 

 

 

 

 

 

 

 

 

Make your life simple by using a Notepad++ plugin

There is even a Notepad++ plugin available, which makes it much easier to implement coding standards.

arun 5

The steps are as follows:

  • Download the file dll file from the internet.
  • Reach out to IT team (or get admin access) and move the dll file that you placed under the folder to C:\Program Files\Notepad++\Plugins ( or wherever notepad++ is installed)
  • Open Notepad++, go to Settingsàimportàimport plugin(s) on the top of the window
  • In the open window, please type the highlighted options and select the dll file
  • Close the Notepad++ and reopen.
  • Go to PluginsàSQLinFormàFormat Selected SQL to format your sql query. This will reduce your effort in formatting it to the guidelines as explained above.

The take-away

Isn’t it simple? The coding standard comments mentioned in the text boxes above, along with the plugin option, can applied to any SQL code that we develop. The formatting options in the plugin are limited when compared to the pro version but it will still make our life easier. For most of the cases, the above standards should be able to cover it. Based on newer scenarios and our experience, one can update the standards in the future. These standards surely will help us bring in more efficiency and readability to the work that we do.