InfoQ

News

Static Code Analysis for T-SQL

Posted by Jonathan Allen on Nov 18, 2008 06:34 AM

Community
.NET
Topics
Code Analysis ,
SQL Server ,
Database Design

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 Nov 19, 2008 2:23 PM
  1. Back to top

    Data Dude GDR also supporting static code analysis

    Nov 19, 2008 2:23 PM by Thomas Geiger

    http://blogs.msdn.com/gertd/archive/2008/10/27/the-gdr-rc-is-here.aspx

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.