T-SQL Tuesday 151 Round up: Coding Standards

I received a great collection of blog posts in response to my T-SQL Tuesday 151 – asking people to write on T-SQL Coding Standards.

Rob Farley (t|b) is of the opinion that coding values that help with efficiency are better than rigidly enforced standards, although he as a consultant abides by his customer’s needs in this regard. ‘Coding standards are not a bad thing. But I think values are better. Valuing readable code, valuing comments that explain the reasons for coding decisions, and valuing patterns that avoid problems, can raise the quality of the coding culture’.
Tom(b) from Czech republic has a neat list of standards they abide by and try to enforce. I particularly liked what they said on specifying nullability on temp tables/table variables, and on code being idempotent – re runnable.

Kevin Chant(t|b) talks about being consistent, and the importance of short, informative comments. Both points are valid and useful.

Aaron Bertrand(t|b) has an amazing post on ‘controversial standard’ CAST Vs Convert – I really enjoyed reading this one. I always use CONVERT personally, but was not aware of the gotchas with CAST that he points out – such as with yyyy-mm-dd and British date, and of CAST being unsupported in other platforms. CONVERT for the win, and am glad I stuck with it. Aaron’s posts are always very informative, and packed with some snarky humor as well. Don’t miss reading this one!

Glenn Berry(t|b) has a nice post where he talks of some basic coding standards, defends his use of NOLOCK and OPTION RECOMPILE on his DMV queries (I believe this is totally fine and use his queries extensively). In the end , he too is of the same opinion as Rob Farley – that ‘it is much more important to have functional, readable code that performs well and is easier to maintain.’.

Deborah Melkin(t|b) has a post that lists the coding standards she thinks are important. She raises an important point – do you fail a PR if a standard is not met if it is not that critical, or just flag it as something to be fixed later? She also discusses the burden of being the ‘one person’ on the team who has to enforce standards and how hard that can sometimes be.

My good friend Steve Jones(t|b) lists a few standards he thinks are important but emphasizes that it is important there is a standard to begin with. He also has a gentle rebuke for me for hosting this blog party 4 times and wants more new folks to start hosting T-SQL Tuesdays going forward. If you are reading this and are interested in hosting, please contact him on twitter.

Deepthi Goguri(t|b) has an excellent blog post on T-SQL practices she wished she knew earlier..it is packed with various tools and tips any T-SQL programmer can use. I did not know of PoorSQL as a formatting tool, and I also appreciated the tip on updating statistics after importing a large number of rows. Well done, Deepthi!

Ken Fisher(t|b) talks of organizational politics with standards – and the importance of having one set of standards everyone in the organization agrees to. I can totally see how this can be a problem in a large organization.

Gerard Jaryczewski(t|b) is writing his first T-SQL Tuesday post – welcome Gerard!! He talks of the importance of simplicity and consistency as T-SQL Standards, naming conventions and adhering to the naming convention of the domain the language uses.

Hugo Kornelis(t|b), an amazing friend I look to for T-SQL related knowledge and guidance talks about having one standard and sticking to it even if you disagree. He also has an interesting take on prefixing view names, something I freely admit I’ve done and enforced as a standard many times..Hugo believes tables and views have to be interchangeable, so you don’t need to be named differently and there are other ways to find out if something is a view..my take personally has been that view is considered code while a table isn’t..so knowing what is code helps in including it for various purposes of analysis. Regardless I respect Hugo’s stance and will consider it going forward for my standards too.

My good friend Tracy Boggiano(t|b) has some great tips on readability here. She also suggests putting performance ahead of any formatting related standards.

To summarize, the lessons I learned are as below –
1 It is important to have standards
2 Pick one set of standards and stick to them.
3 Respect standards at the client (if you are a consultant) or existing standards wherever you go.
4 Performance and code that functions right is more important compared to how it looks, although readability matters too.
5 Standards include stuff that you shouldn’t use, like CAST instead of CONVERT.
6 Many other standards are buried in the posts above.
7 Sign up with Steve to host a T-SQL Tuesday, if you haven’t already!!

That is it!! Thank you to everyone who participated and sharing your nuggets of wisdom.

T-SQL Tuesday 151 Invite: T-SQL Coding Standards

T-SQL Tuesday is the blog party started by Adam Machanic (b|t) over a decade ago and is now maintained by Steve Jones (b|t) onĀ tsqltuesday.com. On the first Tuesday of each month, a blogger hosts the party and suggests a topic. Anyone interested in the topic gets to blog about it. You have one week to write your blog post. It has to go live on the second Tuesday of the month. The host then compiles a list of posts and creates a blog post, for a summarized read. It is a great way to get thinking on subjects you may not have thought of, read other people’s ideas on the same subject, and also get a few people to visit your blog to read your thoughts. Many bloggers including me have used this to get started.

I am honored to be the host for June 2022’s T-SQL Tuesday.

My invite is about coding standards, or what I now call Linting Rules, for T-SQL. What are the T-SQL coding rules that are appropriate for where you work and what you do? If there are exceptions to those rules, state them too! If this is enough, read the blog party rules below and get started!!

  • Your post must be published on Tuesday June 14, 2022.
  • Your post must contain the T-SQL Tuesday logo (see above) and the image must link back to this blog post.
  • Trackbacks should work, but if not please put a link to your post in the comments section so everyone can see your contribution! (My comments are moderated so please don’t worry if yours doesn’t appear right away, I will make sure it does!)
  • If you are on twitter include the hash tag #tsql2sday – it helps with RT-s and visibility!!

More on why I picked this topic as below –

When I started out as a DBA two decades ago, I had a list of rules that I would carry with me into every job I went..these are things I look for in T-SQL code and try to enforce as standard. Some examples were casing rules, minimized usage of SELECT STAR, equating the right data types in columns, avoiding NOLOCK hint and so on. Standards ensure quality and consistency in code.

Standards differ for each firm, depending on what is appropriate for an environment..it is even possible to have varying standards in the same company, depending on the environment and what is appropriate for a database. This is an excellent article on what are the different components that comprise coding standards, and why we need them. I am also a big proponent of automated code checking for standards – there are lots of tools available for doing this – SQL Prompt, which is a personal favorite of mine, and many others as listed here.

Several tools currently do linting on many relational platforms, not just SQL Server. Almost all of them though, have rules that the author(s) think are best for the worlds they work in, and do not include other conditions which they have not encountered yet. A common example I like to use is unnamed primary keys on temporary tables. There is nothing inherently wrong with having an inline primary key constraint/index on a temporary table – but if you use Query Store, plan forcing on a plan that uses this temp table will not work simply because the constraint gets named differently each time. When I started to look for a linting tool for where I work – I ran into so many rules that were non-existent or not applicable to my environment with outside tools that I decided to write my own using ScriptDOM – a Microsoft-provided library that was created specifically for this purpose.

It would help greatly if we had a collection of rules that people use to pick from and enforce as appropriate for their environments. It will also help me to code some of these into ScriptDOM and put it out on GitHub, if the rule is one that ScriptDOM can find easily. So, re-stating the call for this month – What are the T-SQL coding rules that are appropriate for where you work and what you do? If there are exceptions to those rules, state them too!