Defensive Programming in Java

Overview

"Defensive programming" is making sure that programs not only have minimal number of errors, but also that missusing the software in the new code will be detected immediately.

For longer definition see: Defensive programming - Wikipedia.org

Generic introductory resources

Preconditions, Postconditions, Invariants

Part of defensive programming is enforcing pre-conditions & post-conditions for functions, so client code (from user of your code) will not be able to call your functions in the incorrect way. Other related term is called "Design By Contract" (DBC).

Tutorials

Solutions

Books

  • "Java Coding Guidelines: 75 Recommendations for Reliable and Secure Programs" - by Fred Long / Dhruv Mohindra / Robert C. Seacord / Dean F. Sutherland / David Svoboda, 2013 - see chapter "Defensive programming"
  • "The CERT Oracle Secure Coding Standard for Java" - by Fred Long / Dhruv Mohindra / Robert C. Seacord / Dean F. Sutherland / David Svoboda, 2011

See also

  • Best practices
  • Coding standards

Share

follow