InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Static Code Analysis for T-SQL

Posted by Jonathan Allen on Nov 18, 2008

Sections
Operations & Infrastructure,
Process & Practices,
Development,
Architecture & Design
Topics
.NET ,
Database Design ,
SQL Server ,
Code Analysis

Static code analysis, long neglected on the Windows platform, has been becoming more and more import in the last few years. The new emphasis on static analysis started with FX Cop, an internal Microsoft tool that was so successful that they released it to the public. In Visual Studio 2005, FX Cop was integrated into the IDE as part of Visual Studio Team System.

Second generation tools with the ability to complement or even eliminate the need for some forms of unit testing are on the way. These include .NET 4's Code Contracts and the independent venture NStatic.

Managed code is not the only area that's getting attention. Ubitsoft is applying the same techniques to T-SQL. As databases grow, the amount of business logic encapsulated in T-SQL becomes quite significant, sometimes even overwhelming 'normal' code in importance. To help manage this, Ubitsoft created SQL Enlight. We spoke with their lead developer, Iliyan Stoyanov.

This is a fairly new product, can you give me some back ground on how SQL Enlight came to be?

We designed SQL Enlight as T-SQL management and refactoring tool, but because we needed quite more time than acceptable to implement at once all the ideas we had in mind, we decided first to release only the T-SQL reformatting functionality and later gradually to include new features.

At what point did you decide to add support for Transact-SQL Script Analysis?

The analysis feature was one of our goals from the beginning of the project, but we decided to delay its release till we were over with extending our T-SQL parser with support the new SQL Sever 2005 T-SQL syntax.

How do you decide what rules to add to your analysis?

The analysis rules which we implement basically come from tips and practices that we find on the Internet, or from the requests for analysis rules that SQL Enlight users submit to us.

Your technical support mentioned you were working on a version that will perform an analysis on the entire database. Can you give me a little more information about it?

That's right we are working on a new release 1.6 which will be our milestone for version 1.x of SQL Enlight. The new release will include two important features - ability to create custom analysis rules and support for running analysis on databases. We are also planning to include command line tool and MsBuild task.

The current version of SQL Enlight supports these analysis rules.

Design

  • Equality and inequality comparisons involving a NULL constant
  • Non-ANSI outer join syntax.
  • Non-ANSI inner join syntax.
  • Depreciated syntax string_alias = expression.
  • Use TRY..CATCH construct or check the @@ERROR variable after executing a data manipulation statement (like INSERT/UPDATE/DELETE).
  • SELECT * in stored procedures, views and table valued functions.
  • Use SCOPE_IDENTITY() instead @@IDENTITY.
  • Support for constants in ORDER BY clause have been depreciated.
  • TOP clause used in a query without an ORDER BY clause.
  • Always use a column list in INSERT statements.
  • Deprecated usage of table hints without WITH keyword.
  • Index type (CLUSTERED or NONCLUSTERED) not specified.
  • Avoid using GOTO statement to improve readability.
  • Consider using parentheses to improve readability and avoid mistakes because of logical operator precedence.

Naming

  • Avoid 'fn_' prefix when naming functions.
  • Avoid 'sp_' prefix when naming stored procedures.

Performance

  • Variable @variable declared but never used.
  • Variable @variable used but not previously assigned.
  • Variable @variable assigned but value never used.
  • Pattern starting with "%" in LIKE predicate.
  • Consider using a table variable instead a temporary table.
  • Avoid returning results in triggers.
  • Use of very small variable length type (size 1 or 2).
  • SET NOCOUNT ON option in stored procedures and triggers.
  • Avoid using inequality operators (<>,!=) in the WHERE clause.
  • Local cursor not closed.
  • Local cursor not explicitly deallocated.
  • Local cursor reference not explicitly deallocated.
  • Avoid wrapping filtering columns within a function in the WHERE clause.
  • Deterministic function calls can be extracted from the WHERE clause to avoid unnecessary table scan.
  • Input parameter never used.
  • Output parameter never assigned.
  • Avoid using NOT IN predicate in the WHERE clause.
  • Don't use the GROUP BY clause without an aggregate function.
Data Dude GDR also supporting static code analysis by Thomas Geiger Posted
  1. Back to top

    Data Dude GDR also supporting static code analysis

    by Thomas Geiger

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.