For this months T-SQL Tuesday contribution I am going to cover T-SQL consistency and comments.
Mala Mahadevan hosts this months T-SQL Tuesday. Mala invites us to share details about our T-SQL coding rules.
You can read the original invitation to decide for yourself. By clicking on this link for the T-SQL Tuesday 151 invitation or on the image below.
Over the years I have worked a lot with automating T-SQL updates across multiple databases. During that time, it has become very clear that both consistency and comments are essential to make the process smooth.
Naming conventions of objects created using T-SQL should be consistent. Along with comments in your code for colleagues or anybody else who picks up your code.
Especially if you are working in a team who want to work in a DevOps related way.
One way to help with keeping your T-SQL consistent is to put your coding standards in a best practices document. Ideally in a Wiki in whichever platform you are using to keep it as close to the code as possible.
For instance, the Wiki that is available in Azure DevOps.
I know adding comments to your code can be tedious. However, they can help others out a lot. Plus, I can tell you from experience that it is a lot easier to work with code you added comments to a year ago than just the code itself.
As far as comments are concerned, try and keep them short and informative. Plus, avoid putting swear words in them. Because you never know where your code might end up.
For example, they could end up being shared as SQL Server Management Studio templates like I covered in a previous post.
Final words about T-SQL consistency and comments
I hope reading this post about T-SQL consistency and comments has given some of you something to think about. Because I think that both points are important.
Another thing you might want to consider is to agree on some best practices for coding as well. To prevent lots of cursors or GOTO statements suddenly appearing in your T-SQL code.
Anyway, if you have any questions or comments about this post feel free to reach out to me.
[…] Kevin Chant values consistency: […]
“Because you never know where your code might end up”: for example, I use many funny real comments when I teach SQL. My last discovery looks like this: there are TWO lines of comments on why we are using “SET NOCOUNT ON” at the beginning of the procedure, and this is by convention repeated in every one of tens of procedures, and then we have something like “IF (@t = (SELECT TOP(1) t FROM ts WHERE 1 = 0 OR @t = 12) RETURN 0”, which has ZERO lines of comment. Writing good comments is an art. Thank you for your work, Kevin.