From the course: Debugging in C#

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Defensive programming

Defensive programming

From the course: Debugging in C#

Start my 1-month free trial

Defensive programming

- [Instructor] Defensive programming is a way of programming, where the application should behave in a consistent and predictable manner, even in the case of unexpected conditions. It's a way of programming where you test for every possible edge case, not just the most common cases or the cases you'd most expect. In defensive programming, we want to make sure our functions have clear requirements, as well as clear input and output types. In the function implementation, we will also want to make sure our input satisfies certain criteria. It's a way of making sure the computation will make sense. Think of it this way, if I ask the question do you write with an apple or an orange? Does this question make sense at all? No, it does not. This is what defensive programming prevents. Let's take a look at an example in code. In this program we have the enum Shape that can have the value Error, Rectangle or Square. We also have a testShape function and depending on what the shape is, we write…

Contents