# Defensive Programming

- Speaker: [Lior Sion](/wiki/speakers/lior-sion.md)
- Event: [Reversim Summit 2013](/wiki/years/2013.md)
- Video: https://www.youtube.com/watch?v=NcXEBc3dX5A
- Transcript: [/transcripts/2013/08-NcXEBc3dX5A.md](/transcripts/2013/08-NcXEBc3dX5A.md)
- Language: Hebrew · Type: talk · Level: intermediate
- Topics: [Software Engineering](/wiki/topics/software-engineering.md), [Technical Debt](/wiki/topics/technical-debt.md), [Soft Skills](/wiki/topics/soft-skills.md)

> Written by an LLM from machine transcripts. It can contain errors. Check the transcript before you quote it.

## Summary

The speaker explores the psychological roots of defensive programming, arguing that overly defensive code often stems from fear, lack of familiarity with the codebase, and time pressure. Through real-world examples, the speaker demonstrates how common practices like excessive input validation, indiscriminate exception catching, and premature locking actually create fragile, hard-to-maintain software. The audience learns to recognize these anti-patterns, understand the human emotions driving them, and foster a culture of quality, ongoing refactoring, and code familiarity rather than fear.

## Key points

- [1:08](https://www.youtube.com/watch?v=NcXEBc3dX5A&t=68s) Defensive programming is largely a psychological phenomenon rather than a strict set of rules.
- [2:35](https://www.youtube.com/watch?v=NcXEBc3dX5A&t=155s) Common defensive anti-patterns include redundant input validation at every layer, catching all exceptions indiscriminately, and overusing locks for concurrency issues.
- [4:41](https://www.youtube.com/watch?v=NcXEBc3dX5A&t=281s) Writing repetitive validation and locks makes code bloated, hard to test, and difficult to change in the future.
- [11:08](https://www.youtube.com/watch?v=NcXEBc3dX5A&t=668s) The primary drivers behind defensive coding are time pressure, complex legacy code, lack of testing, and most importantly, fear.
- [17:48](https://www.youtube.com/watch?v=NcXEBc3dX5A&t=1068s) Continuous, ongoing refactoring prevents the accumulation of technical debt and keeps the codebase healthy.
- [22:10](https://www.youtube.com/watch?v=NcXEBc3dX5A&t=1330s) Design patterns should be understood and applied thoughtfully rather than copied blindly without grasping the underlying problem.
- [25:23](https://www.youtube.com/watch?v=NcXEBc3dX5A&t=1523s) A blame-free culture is essential because punishing mistakes induces fear, which directly leads to defensive programming.

## Takeaways

- Treat the codebase as a familiar friend by taking the time to read and understand code before changing it.
- Address the root causes of fear and lack of knowledge in the team rather than reacting with complex defensive code.
- Perform small, ongoing refactorings instead of letting technical debt accumulate until massive rewrites are required.

## Questions this talk answers

- How can I recognize and eliminate defensive programming anti-patterns in my team's codebase?
- When is it appropriate to apply design patterns versus writing straightforward custom code?
- How do we balance the need for high code quality and refactoring with tight delivery deadlines?

## Technologies

Ruby, C++

## Tags

best practices, code quality, defensive programming, refactoring, software psychology, technical debt
