Favorite (Clever) Defensive Programming Best Practices [closed]

In c++, I once liked redefining new so that it provided some extra memory to catch fence-post errors.

Currently, I prefer avoiding defensive programming in favor of Test Driven Development. If you catch errors quickly and externally, you don’t need to muddy-up your code with defensive maneuvers, your code is DRY-er and you wind-up with fewer errors that you have to defend against.

As WikiKnowledge Wrote:

Avoid Defensive Programming, Fail Fast Instead.

By defensive programming I
mean the habit of writing code that
attempts to compensate for some
failure in the data, of writing code
that assumes that callers might
provide data that doesn’t conform to
the contract between caller and
subroutine and that the subroutine
must somehow cope with it.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)