Click here to monitor SSC
Av rating:
Total votes: 15
Total comments: 3


Defensive Database Programming
22 February 2010

The goal of Defensive Programming is to produce resilient code that responds gracefully to the unexpected. To the SQL Server programmer, this means T-SQL code that behaves consistently and predictably in cases of unexpected usage, doesn't break under concurrent loads, and survives predictable changes to database schemas and settings. Inside this book, you will find dozens of practical, defensive programming techniques that will improve the quality of your T-SQL code and increase its resilience and robustness.

Defensive Database Programming By Alex Kuznetsov

Download the free PDF. 

The book is available to buy from Amazon.com and Amazon.co.uk.

Why read this book?

Resilient T-SQL code is code that is designed to last, and to be safely reused by others. The goal of defensive database programming, the goal of this book, is to help you to produce resilient T-SQL code that robustly and gracefully handles cases of unintended use, and is resilient to common changes to the database environment.

Too often as developers, we stop work as soon as our code passes a few basic tests to confirm that it produces the 'right result' in a given use case. We do not stop to consider what other possible ways in which the code might be used in the future, or how our code will respond to common changes to the database environment, such as a change in the database language setting, or a change to the nullability of a table column, and so on.

In the short-term, this approach is attractive; we get things done faster. However, if our code is designed to be used for more than just a few months, then it is very likely that such changes can and will occur, and the inevitable result is broken code or, even worse, code that silently starts to behave differently, or produce different results. When this happens, the integrity of our data is threatened, as is the validity of the reports on which critical business decisions are often based. At this point, months or years later, and long after the original developer has left, begins the painstaking process of troubleshooting and fixing the problem.

Would it not be easier to prevent all this troubleshooting from happening? Would it not be better to spend a little more time and effort during original development, to save considerably more time on troubleshooting, bug fixing, retesting, and redeploying?

This is what defensive programming is all about: we learn what can go wrong with our code, and we proactively apply this knowledge during development. This book is filled with practical, realistic examples of the sorts of problems that beset database programs, including:

  • Changes in database objects, such as tables, constraints, columns, and stored procedures.
  • Changes to concurrency and isolation levels.
  • Upgrades to new versions of SQL Server.
  • Changes in requirements.
  • Code reuse.
  • Problems causing loss of data integrity.
  • Problems with error handling in T-SQL.

In each case, it demonstrates approaches that will help you understand and enforce (or eliminate) the assumptions on which your solution is based, and to improve its robustness. Ultimately, the book teaches you how to think and develop defensively, and how to proactively identify and eliminate potential vulnerabilities in T-SQL code.

Nikos Vaggalis at I Programmer called it 'an indespensable guide'. See his review here.



This article has been viewed 8053 times.
Alex Kuznetsov

Author profile: Alex Kuznetsov

Alex Kuznetsov has been working with object oriented languages and databases for more than a decade. He has worked with Sybase, SQL Server, Oracle and DB2. He regularly blogs on sqlblog.com, mostly about database unit testing, defensive programming, and query optimization. Alex has published multiple articles on simple-talk.com and devx.com and wrote a book entitled "Defensive Database Programming with SQL Server". Currently he works with DRW Trading Group in Chicago, where he leads a team of developers, practicing agile development, defensive programming, and database unit testing every day. In his leisure time Alex prepares for and runs ultramarathons.

Search for other articles by Alex Kuznetsov

Rate this article:   Avg rating: from a total of 15 votes.


Poor

OK

Good

Great

Must read
 
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: Looking forward to reading!
Posted by: Johnm (view profile)
Posted on: Thursday, April 22, 2010 at 8:58 PM
Message: Alex, it was a pleasure to meet you at the Chicago SQL Saturday. I wish we could have chatted more. I eagerly look forward to reading your book!

Subject: Defensive Database Programming
Posted by: RGerald (view profile)
Posted on: Friday, June 25, 2010 at 12:51 PM
Message: When will the physical book be available? I've been waiting to buy a copy.

Rand

Subject: Chi-SSWUG
Posted by: alalani (view profile)
Posted on: Friday, March 11, 2011 at 9:17 AM
Message: Alex,
It was nice meeting you yesterday at this user group. Your presentation about defensive and resilient programming totally made sense. Some were eye-openers such as difference between SET and SELECT. Also thanks for giving me your book. I was looking for link "defensive_code.zip" and could not find it. Please send me the current link to download the codes in the book.
Thanks
Ameena

 



recommended site pinvoke

PInvoke.net is a user-driven wiki which provides .NET developers with native method signatures, so they don't have to spend time writing them from scratch.




Exchange 2010 SP1 - A Practical Approach
 This update of Jaap Wessellius' popular guide to Exchange 2010 covers everything you need to know to... Read more...

Captain Biplane: Interplanetary Airman
 Simple-Talk caters for the lighter side with its publication of 'Captain Biplane: Interplanetary... Read more...

Don't Just Roll the Dice - eBook Download
 Neil Davidson has created a short handbook with the theory, practical advice and case studies, to... Read more...

Defensive Database Programming
 Inside this book, you will find dozens of practical, defensive programming techniques that will improve... Read more...

Mastering SQL Server Profiler
 Brad McGehee's "from the ground up" guide to SQL Server's most powerful performance diagnostic tool... Read more...

Exchange 2010 - A Practical Approach
 Jaap's Practical Guide to Exchange Server 2010 draws upon all that experience to deliver an easy-to-use... Read more...

Brad's Sure Guide to SQL Server Maintenance Plans
 Brad's Sure Guide to Maintenance Plans shows you how to use the Maintenance Plan Wizard and Designer to... Read more...

Inside the SQL Server Query Optimizer
 This book will take you from the fundamentals of Statistics, Cost Estimation, Index Selection, and the... Read more...

SQL Server Hardware
 SQL Server Hardware will provide the fundamental knowledge and resources you need to make intelligent... Read more...

Performance Tuning with SQL Server Dynamic Management Views
 This is the book that will de-mystify the process of using Dynamic Management Views to collect the... Read more...

Over 400,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.

Join Simple Talk