Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 226549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:31:21+00:00 2026-05-11T19:31:21+00:00

Disclaimer: I am a layperson currently learning to program. Never been part of a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:31:21+00:00Added an answer on May 11, 2026 at 7:31 pm

    It all comes down to the contract the interface provides. There are two different scenarios for this: inputs and outputs.

    Inputs–and by that I basically mean parameters to functions–should be checked by the implementation as a general rule.

    Outputs–being return results–should be basically trusted by the caller, at least in my opinion.

    All of this is tempered by this question: what happens if one party breaks the contract? For example, lets say you had an interface:

    class A {
      public:
        const char *get_stuff();
    }
    

    and that contract specifies that a null string will never be returned (it’ll be an empty string at worst) then it’s safe to do this:

    A a = ...
    char buf[1000];
    strcpy(buf, a.get_stuff());
    

    Why? Well, if you’re wrong and the callee returns a null then the program will crash. That’s actually OK. If some object violates its contract then generally speaking the result should be catastrophic.

    The risk you face in being overly defensive is that you write lots of unnecessary code (which can introduce more bugs) or that you might actually mask a serious problem by swallowing an exception that you really shouldn’t.

    Of course circumstances can change this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: I don't actually know anything about nether Oracle nor Java. The issue is
Disclaimer: I'm sure this has been answered before but I cannot find anything on
Disclaimer: this question is driven by my personal curiosity more than an actual need
Disclaimer: Never used php in my life. New to Unix and Jenkins I have
Disclaimer: I am anything but a Javascript expert, so I'm not even sure if
[Disclaimer: I am new to PHP, and I am just learning, so please no
(Disclaimer: I am by no stretch of the imagination a security expert nor a
Disclaimer: I'm fairly new with Gradle. I'm trying to build my project with Gradle
Disclaimer: I am not a C programmer. I have recently seen a friend's project.
Disclaimer : I am reviewing CouchDB for a new project, so I am not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.