concept defensive programming in category .net

appears as: defensive programming, defensive programming
AOP in .NET

This is an excerpt from Manning's book AOP in .NET.

2.2.4. Introducing defensive programming

Because this core loyalty business logic has no control over the data that gets passed in as parameters, we may have to check for worst-case scenarios. For a simple example, what if a null reference is passed in for the RentalAgreement parameter? That’s not a problem that our logic can cope with, so an exception should be thrown. We hope the code calling our API can cope with that exception. If not, then at least we’re alerting the UI developers and/or QA that something has gone wrong. This philosophy is known as defensive programming. Like defensive driving, it’s meant to reduce the risk of dangerous (or in the case of programming, invalid) scenarios by anticipating invalid contexts or mistakes in other parts of the application.

In the following listing, you’ll put defensive programming in place to check for the invalid scenario of a null RentalAgreement being passed in.

Listing 2.5. Accrue with defensive programming