Talking about Defensive Programming during times of Test Driven Development, Clean Code, Domain Driven Development and all other kinds of buzzwords seems a bit unusual. From his long-year experience David McCarter shares a collection of rules of thumb to write better, rock-solid code. The book is part of his conference series titled Improving Code Quality... One Developer at A Time.
Back in 2017 I met David at the C# Corner conference in Delhi, India. While I gave a session on .NET Core on Linux, he had several sessions on how to be successful as a software developer and how to improve programming skills by sharing his experience as a programmer for over two decades.
His series Improving Code Quality... One Developer at A Time was already in full swing and the review on his book on Defensive Programming is a milestone on this journey.
The targeted audience of the title are .NET developers with little to a few years of experience. The major focus is on how to write code that avoids causing exceptions. And in case that an exception happens on how to deal with it depending on the application layer it occurs. Lastly, David writes about techniques to log exceptions in .NET Core.
Stop exceptions before they happen
Of course, the best way to deal with an exception is not to cause an exception. And that's how defensive programming adds value to your code base. Use the existing features of .NET and .NET Core to check the state of objects, properties and parameters before using them further.
Typically, one would always check an object or property for null
. If you're working with file system operations check whether a file really exists before accessing it. Treat all incoming parameters and data as incomplete or damaging to your program execution. If the state of an object or property does not match the expected situation stop any further execution and return back to the caller. Stay on the positive path...
Lastly, David gives a few tips on how to deal with exceptions that are still occurring inclusive the ones that our code might actually throw given incorrect state evaluation. The .NET Framework has several options on where to catch such exception, inclusive the option to implement an "catch-all" exception handler that wouldn't miss a single beat.
The book has 42 pages in total and is relatively short in regards to the topic overall. Given the length of Rock Your Code - Defensive Programming it is a good fit for a couple of lunch breaks, one or two commute units or a dedicated Friday afternoon learning session. Some information might be too obvious to an experienced .NET developer but surely a good resource for junior developers to get on the right track of writing better source code from the very beginning.
You can get a copy of Rock Your Code - Defensive Programming on Amazon at an exceptional low price.
This book brings writing better code to the next level.
The bespoken book is one of the newer titles of David McCarter; available on Amazon. You might also be interested in his advice on how to Rock Your Technical Interview.
Disclaimer: I'm a technical editor of the book.Image credit: John Pratt