{"id":1870,"date":"2014-09-15T00:00:00","date_gmt":"2014-09-15T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/119-sql-code-smells\/"},"modified":"2024-08-30T12:51:51","modified_gmt":"2024-08-30T12:51:51","slug":"119-sql-code-smells","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/resources\/books\/simple-talk-published-books\/119-sql-code-smells\/","title":{"rendered":"119 SQL Code Smells"},"content":{"rendered":"<div class=\"spaced-bottom padded--tight scheme--lightest-grey\">\n<img decoding=\"async\" class=\"media__image\" style=\"float: left; vertical-align: middle;\" src=\"https:\/\/assets.red-gate.com\/external\/SimpleTalk\/database-devops-cog-92.png\" \/> <\/p>\n<p class=\"padded-top--tight\"><strong>DevOps, Continuous Delivery & Database Lifecycle Management<\/strong><br \/>Continuous Integration<\/p>\n<\/div>\n\n<p><em>Written with advice, help or contributions from over 25 SQL Server professionals<\/em> <br \/>\n<a href=\"https:\/\/www.red-gate.com\/products\/sql-development\/sql-prompt\/entrypage\/code-smells?utm_source=simpletalk&amp;utm_medium=publink&amp;utm_content=sqlcodesmells&amp;utm_campaign=sqltest\"><strong>Download the free PDF<\/strong> from Red Gate<\/a><\/p>\n<p>Once you&#8217;ve done a number of SQL code-reviews, you&#8217;ll be able to identify signs in the code that indicate all might not be well. These &#8216;code smells&#8217; are coding styles, some generic, and some particular to SQL Server, that, while not bugs, suggest design problems with the code. In this PDF, Phil&#8217;s put together 119 of those code smells \u00a0so you can see what to avoid and why.<\/p>\n<p>Kent Beck and Massimo Arnoldi seem to have coined the term &#8216;CodeSmell&#8217; in the <a href=\"http:\/\/www.c2.com\/cgi\/wiki?OnceAndOnlyOnce\">&#8216;Once And Only Once&#8217;<\/a> page of www.C2.com, where Kent also said that code &#8216;wants to be simple&#8217;. Kent Beck and Martin Fowler expand on the issue of code challenges in their essay &#8216;Bad Smells in Code&#8217;, published as Chapter 3 of the book &#8216;Refactoring: Improving the Design of Existing Code&#8217; (ISBN 978-0201485677).<\/p>\n<p>Although there are generic code smells, SQL has its own particular habits that will alert the programmer to the need to refactor code. (For grounding in code smells in C#, see <a href=\"https:\/\/www.simple-talk.com\/dotnet\/.net-framework\/exploring-smelly-code\/?utm_source=simpletalk&amp;utm_medium=weblink&amp;utm_content=sqlcodesmells&amp;utm_campaign=sqltest\">&#8216;Exploring Smelly Code&#8217;<\/a> and <a href=\"https:\/\/www.simple-talk.com\/dotnet\/.net-framework\/code-deodorants-for-code-smells\/?utm_source=simpletalk&amp;utm_medium=weblink&amp;utm_content=sqlcodesmells&amp;utm_campaign=sqltest\">&#8216;Code Deodorants for Code Smells&#8217;<\/a> by Nick Harrison.) Plamen Ratchev&#8217;s wonderful article <a href=\"https:\/\/www.simple-talk.com\/sql\/t-sql-programming\/ten-common-sql-programming-mistakes\/?utm_source=simpletalk&amp;utm_medium=weblink&amp;utm_content=sqlcodesmells&amp;utm_campaign=sqltest\">&#8216;Ten Common SQL Programming Mistakes&#8217;<\/a> lists some of the SQL Server code smells along with out-and-out mistakes, but there are more. The use of nested transactions, for example, isn&#8217;t entirely incorrect, even though the database engine ignores all but the outermost, but their use does flag the possibility the programmer thinks that nested transactions are supported.<\/p>\n<p>For a booklet like this, it is best to go with the established opinion of what constitutes a SQL Code Smell in SQL Server. There is little room for creativity. In order to identify only those SQL coding habits that could, in some circumstances, lead to problems, I must rely on the help of experts, and I am very grateful for the help, support and writings of the following people in particular:<\/p>\n<div>\n<ul>\n<li>Dave Howard<\/li>\n<li>Merrill Aldrich<\/li>\n<li>Plamen Ratchev<\/li>\n<li>Dave Levy<\/li>\n<li>Mike Reigler<\/li>\n<li>Anil Das<\/li>\n<li>Adrian Hills<\/li>\n<li>Sam Stange<\/li>\n<li>Ian Stirk<\/li>\n<li>Aaron Bertrand<\/li>\n<li>Neil Hambly<\/li>\n<li>Matt Whitfield<\/li>\n<li>Nick Harrison<\/li>\n<li>Bill Fellows<\/li>\n<li>Jeremiah Peschka<\/li>\n<li>Diane McNurlan<\/li>\n<li>Robert L Davis<\/li>\n<li>Dave Ballantyne<\/li>\n<li>John Stafford<\/li>\n<li>Alex Kusnetsov<\/li>\n<li>Gail Shaw<\/li>\n<li>Jeff Moden<\/li>\n<li>Joe Celko<\/li>\n<li>Robert Young<\/li>\n<\/ul>\n<\/div>\n<p>And special thanks to our technical referees, Grant Fritchey and Jonathan Allen.<\/p>\n<div class=\"scheme--lightest-grey spaced-bottom\">\n  <img decoding=\"async\" class=\"media__image padded--tight\" style=\"float: right; vertical-align: middle;\" src=\"https:\/\/assets.red-gate.com\/external\/SimpleTalk\/database-devops-cog-160.png\"><\/p>\n<div class=\"padded\">\n<p class=\"padded-top--tight\"> <strong>DevOps, Continuous Delivery & Database Lifecycle Management<\/strong><br \/> <a href=\"\/collections\/database-lifecycle-management-patterns-practices-library\/?utm_source=simpletalk&amp;utm_medium=publink&amp;utm_campaign=dlm&amp;utm_content=pandparticle&amp;utm_term=bottombanner\">Go to the Simple Talk library<\/a> to find more articles, or visit <a href=\"https:\/\/www.red-gate.com\/solutions?utm_source=simpletalk&amp;utm_medium=publink&amp;utm_campaign=dlm&amp;utm_content=pandparticle&amp;utm_term=bottombanner&amp;__hstc=81186632.9b3ff88e8dc727f9f1f608ffe1681edd.1467735864337.1482415357732.1482419905332.123&amp;__hssc=81186632.36.1482419905332&amp;__hsfp=3698596899\">www.red-gate.com\/solutions<\/a> for more information on the benefits of extending DevOps practices to SQL Server databases.<\/p>\n<\/div>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Once you&#8217;ve done a number of SQL code-reviews, you&#8217;ll be able to identify signs in the code that indicate all might not be well. These &#8216;code smells&#8217; are coding styles that, while not bugs, suggest design problems with the code. In this PDF, Phil&#8217;s put together 119 of those code smells, some generic, and some particular to SQL Server, so you can see what to avoid and why.&hellip;<\/p>\n","protected":false},"author":154613,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[159158],"tags":[4363,5969,4197,4933,4150,5013,4151,5935],"coauthors":[6813],"class_list":["post-1870","post","type-post","status-publish","format-standard","hentry","category-simple-talk-published-books","tag-books","tag-code-smells","tag-database-design","tag-ebook","tag-sql","tag-sql-books","tag-sql-server","tag-tips-and-tricks"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/154613"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=1870"}],"version-history":[{"count":6,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1870\/revisions"}],"predecessor-version":[{"id":84902,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1870\/revisions\/84902"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1870"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}