|
1 |
| -SQL-code-smells |
| 1 | +SQL code smells |
2 | 2 | ===============
|
| 3 | + |
| 4 | +>*This is still a work in progress - we'll add more of the book over the next few days.* |
| 5 | +
|
| 6 | +#Contents |
| 7 | + |
| 8 | +- Introduction |
| 9 | +- Problems with Database |
| 10 | +- Design |
| 11 | +- Problems with Table |
| 12 | +- Design |
| 13 | +- Problems with Data Types |
| 14 | +- Problems with Expressions |
| 15 | +- Difficulties with Query |
| 16 | +- Syntax |
| 17 | +- Problems with Naming |
| 18 | +- Problems with Routines |
| 19 | +- Security Loopholes |
| 20 | +- Acknowledgements |
| 21 | + |
| 22 | +#Introduction |
| 23 | +**Once you’ve done a number of SQL code-reviews, you’ll be able to identify signs in the code that indicate all might not be well. These ‘code smells’ are coding styles that, while not bugs, suggest design problems with the code.** |
| 24 | + |
| 25 | +Kent Beck and Massimo Arnoldi seem to have coined the term ‘CodeSmell’ in the ‘[Once And Only Once](http://www.c2.com/cgi/wiki?OnceAndOnlyOnce)’ page of www.C2.com, where Kent also said that code ‘wants to be simple’. Kent Beck and Martin Fowler expand on the issue of code challenges in their essay ‘Bad Smells in Code’, published as Chapter 3 of the book ‘Refactoring: Improving the Design of Existing Code’ (ISBN 978-0201485677). |
| 26 | + |
| 27 | +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 ‘[Exploring Smelly Code](https://www.simple-talk.com/dotnet/.net-framework/exploring-smelly-code/)’ and ‘[Code Deodorants for Code Smells](https://www.simple-talk.com/dotnet/.net-framework/code-deodorants-for-code-smells/)’ by Nick Harrison.) Plamen Ratchev’s wonderful article ‘[Ten Common SQL Programming Mistakes](https://www.simple-talk.com/sql/t-sql-programming/ten-common-sql-programming-mistakes/)’ lists some of these code smells along with out-and-out mistakes, but there are more. The use of nested transactions, for example, isn’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. |
| 28 | + |
| 29 | +If you are moving towards continuous delivery of database applications, you should automate as much as possible the preliminary SQL code-review. It’s a lot easier to trawl through your code automatically to pick out problems, than to do so manually. Imagine having something like the classic ‘lint’ tools used for C, or better still, a tool similar to [Jonathan ‘Peli’ de Halleux’s](https://www.simple-talk.com/opinion/geek-of-the-week/peli-de-halleux-geek-of-the-week/) Code Metrics plug-in for .NET Reflector, which finds code smells in .NET code. |
| 30 | + |
| 31 | +One can be a bit defensive about SQL code smells. I will cheerfully write very long stored procedures, even though they are frowned upon. I’ll even use dynamic SQL on occasion. You should use code smells only as an aid. It is fine to ‘sign them off’ as being inappropriate in certain circumstances. In fact, whole classes of code smells may be irrelevant for a particular database. The use of proprietary SQL, for example, is only a code smell if there is a chance that the database will be ported to another RDBMS. The use of dynamic SQL is a risk only with certain security models. Ultimately, you should rely on your own judgment. As the saying goes, a code smell is a hint of possible bad practice to a pragmatist, but a sure sign of bad practice to a purist. |
| 32 | + |
| 33 | +In describing all these 119 code-smells in a booklet, I’ve been very constrained on space to describe each code smell. Some code smells would require a whole article to explain them properly. Fortunately, SQL Server Central and Simple-Talk have, between them, published material on almost all these code smells, so if you get interested, please explore these essential archives of information. |
| 34 | + |
| 35 | + -*Phil Factor, _Contributing Editor_* |
| 36 | + |
| 37 | +#Problems with Database |
| 38 | +#Design |
| 39 | +#Problems with Table |
| 40 | +#Design |
| 41 | +#Problems with Data Types |
| 42 | +#Problems with Expressions |
| 43 | +#Difficulties with Query |
| 44 | +#Syntax |
| 45 | +#Problems with Naming |
| 46 | +#Problems with Routines |
| 47 | +#Security Loopholes |
| 48 | +#Acknowledgements |
0 commit comments