Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

May 16, 2014

What is Defensive Coding?

Filed under: C#,Testing,VB.NET @ 10:40 am

From Wikipedia (as of 4/14/14):

… an approach to improve software and source code, in terms of:

General quality – Reducing the number of software bugs and problems.

• Making the source code comprehensible – the source code should be readable and understandable so it is approved in a code audit.

• Making the software behave in a predictable manner despite unexpected inputs or user actions.

Let’s consider each of these bullet points…

General quality

Coding defensively means to actively code to reduce bugs. One of the key techniques for improving quality is through automated code testing.

Don’t know that you have time in your project schedule for automated code testing? That’s a topic for another blog post. Or check out my “Defensive Coding” course referenced at the bottom of this post for a demonstration of some simple automated code testing techniques and a discussion of the “no time for testing” issue.


It is not just computers that need to read and understand your code … people need to read and understand it as well.

If another developer doesn’t understand your intent, they may make incorrect assumptions about that code and make inappropriate code changes … causing your code to fail.

Plus if the code is easy to read and understand, it will be easier and less time consuming to modify as the application is maintained or enhanced over time.

The key to making source code more readable and understandable is by building “Clean Code”. The concept of “Clean Code” was first presented by Robert Martin in his book: “Clean Code: A Handbook of Agile Software Craftsmanship”.

The cleaner your code is, the easier it is to understand, maintain, and test.


Predictable code should handle unexpected inputs or user actions by anticipating them and responding accordingly.

This includes techniques such as guard clauses, validation, and error handlers.

Putting these three concepts into a picture summarizes the goals of defensive coding:


For more information on Defensive Coding, see my Pluralsight course: “Defensive Coding in C#”.




Check out my Pluralsight courses!


  1.   Eyal — May 19, 2014 @ 1:19 pm    Reply

    I liked the the picture that summerize defensive coding.
    I may use it one time in some presentation if you don’t mind 🙂

    nice article.


  2.   DeborahK — May 20, 2014 @ 9:53 am    Reply

    Glad you like it. Sure, feel free to use it. Just be sure to provide an acknowledgment of the source. 🙂

  3.   Prasad P. Khandekar — May 21, 2014 @ 3:01 am    Reply


    It will be really nice if you also elaborate these points with some good examples.

    Since I am from java camp I can talk on java projects, most of the frameworks I have been using does not seem to follow these guidelines very well, e.g the famous spring framework or hibernate is not really Comprehensible, though it’s readable.

    “If another developer doesn’t understand your intent”, how to address this aspect.

    What makes code clean?


RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2022 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs