From the course: Debugging in C++ with Visual Studio Code
Unlock this course with a free trial
Join today to access over 24,900 courses taught by industry experts.
Defensive programming - Visual Studio Code Tutorial
From the course: Debugging in C++ with Visual Studio Code
Defensive programming
- [Instructor] Let's talk about a coding philosophy that's less about fixing problems and more about preventing them, because the best bugs are the ones that never happen. Defensive programming is all about assuming things can and will go wrong. By anticipating potential errors, you can write code that handles unexpected situations when they occur. For example, every input in your program, whether from the user, a file, or an external system, should be checked for validity. The objective is for your program to work with clean and expected data. Assertions allow you to define and enforce conditions that must hold true during execution. They act as checkpoints where violations can be caught before they cause bigger problems. Instead of crashing or producing cryptic errors, defensive code is designed to fail gracefully. This means providing clear error messages and keeping the system stable whenever possible. Defensive programming encourages code that explains itself. Clear variable…