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

  • Home
  • SEARCH
  • 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 900795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:26:53+00:00 2026-05-15T15:26:53+00:00

I understand the principles of Coding to Interfaces – to decouple the implementation from

  • 0

I understand the principles of Coding to Interfaces – to decouple the implementation from the interface, and to allow implementations of the interface to be swapped in and out.

Should I code to interfaces for every class I write or is that overkill? I don’t want to double the number of source files in a project unless it’s really worth it.

What factors can I use to decide whether to code by interface or not?

  • 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-15T15:26:54+00:00Added an answer on May 15, 2026 at 3:26 pm

    In general I agree with the other answers: use an interface when you know or anticipate change and/or different implementation, or go for testability.

    But it’s not always easy to know in advance what may change in the future, especially if you are not so experienced. I think the solution for that problem is refactoring: keep it simple (= no interface) as long as it is not needed. When the need arises simply do an “Introduce/Extract interface” refactoring (supported by almost all decent IDEs).

    When you do it extract only those methods that are actually needed by the callers. Don’t be afraid to extract more then one separate interfaces (if not all of the extracted methods are really coherent).

    One driver of the refactoring might be the testing: if you can’t easily test something with classes only consider introducing one/some interface(s). This will also allow you to use mocking which may greatly simplify testing in many cases.

    Edit: based on Tarski’s comment I’ve realized one more important scenario/adjustment to the previous statements:
    If you provide an external API (for other [sub]projects, or really release it “to the wild”) then using interfaces in the API (except for simple value classes) is almost always a good idea.
    It will allow you to change the impl if you like without disturbing the client code. You will have a problem only if you also have to change the interface. Not breaking compatibility will be very tricky (if not impossible).

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

Sidebar

Related Questions

From what I understand of REST principles, URLs should represent a single resource, like
I understand the principles but i have a hard time seeing where the practical
I understand, from MSDN, that ClassInitialize is to mark a method that will do
I am a great fan of code generation (from UML) and coming from the
I understand the principles of asymptotic notation, and I get what it means when
I'm trying to understand the principles of the Repository question. But i've stumbled upon
I understand the basic principles of script/style compression. What I would like to know
I'm trying to sort out an opt in mailing list system. I understand the
I understand the principles behind the split tone effect, and would love to use
I'm maintaining code for a mathematical algorithm that came from a book, with references

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.