Disclaimer: I am a layperson currently learning to program. Never been part of a project, nor written anything longer than ~500 lines.
My question is: does defensive programming violate the Don't Repeat Yourself principle? Assuming my definition of defensive programming is correct (having the calling function validate input instead of the opposite), wouldn't that be detrimental to your code?
For instance, is this bad:
int foo(int bar)
{
if (bar != /*condition*/)
{
//code, assert, return, etc.
}
}
int main()
{
int input = 10;
foo(input); //doesn't the extra logic
foo(input); //and potentially extra calls
foo(input); //work against you?
}
compared to this:
int main()
{
if (input == /*condition*/)
{
foo(input);
foo(input);
foo(input);
}
}
Again, as a layperson, I don't know how much simple logic statements count against you as far as performance goes, but surely defensive programming is not good for the program or the soul.