From HandWiki - Reading time: 3 min
This article may be too technical for most readers to understand. Please help improve it to make it understandable to non-experts, without removing the technical details. (December 2016) (Learn how and when to remove this template message) |
Offensive programming is a software development philosophy that deals with software bugs by having the program fail fast and visibly, rather than attempting to hide or recover from them.[1][2] The goal is to make bugs obvious during development and testing, under the assumption that unexpected internal errors should be fixed by the programmer, not tolerated by the running software.
This approach is considered a branch of defensive programming because it is a strategy for handling errors. However, it contrasts with defensive techniques that might mask a bug by using default values or continuing to run in a degraded state. Instead, offensive programming often uses tools like assertions to halt the program immediately when an invalid state is detected, making the source of the problem easier to identify and fix.
The premise for offensive programming is to distinguish between expectable errors, coming from outside the program's line of defense, however improbable, versus preventable internal errors that shall not happen if all its software components behave as expected.
Contrasting examples:
| Expectable errors | Preventable errors |
|---|---|
| Invalid user input | Invalid function arguments |
| Depletion of OS resources (such as storage, memory) | Value out of defined range (e.g. enum) |
| Hardware failure (such as network, storage) | Undocumented return value or exception |
Offensive programming is concerned with failing, so to disprove the programmer's assumptions. Producing an error message may be a secondary goal.