1

This is a design principle in object-oriented programming:

Program to an interface, not an implementation.

At last now I know for sure what the principle means (thanks to @DanBron in the comments) but as a non-native speaker I fail to understand what the phrase "program to" means in that sentence.

It has been suggested that it means "write to" or "program against" but I still can't risk an interpretation: which example usage of "write to" would be similar? Is "program against" a phrasal verb? What does "against" mean in that case?

1

1 Answer 1

2

Programming is an activity that involves writing a program. However, when you are doing that, you write your program in such a way that it will interact with other systems. This is commonly expressed as programming against X:

I program in C# against an Oracle database.

Programming can be seen as a way to communicate with a computer system. You are telling it what it should do, and you are "talking" to surrounding system parts or comonents.

In that light, one could be said to be programming to a database, a webservice, an external component, a class.

Indeed the advice to prgram to an interface, rather than an implementation, means you should address the exposed interface, or contract, rather than the implementation of that interface or contract.
It also means that you should make that possible for other programmers, by ensuring you provide such an interface or contract!

5
  • It's a similar usage to the phrase "to adhere to a set of guidelines". ie, the guidelines will inform us what we need to do, and we follow them. Commented Aug 5, 2016 at 14:23
  • There is some semantic gap preventing me from understanding what "to" translates to in "adhere to" or "program to", but I was able to understand this answer clearly. Based on it I'd word the principle as "program addressing interfaces, not implementations" or "write code which calls only interfaces". Is this a reasonable interpretation?
    – Piovezan
    Commented Aug 5, 2016 at 17:42
  • @oerkelens Have I missed anything in my interpretation (besides your last paragraph)? In it I mean interfaces as contracts or "the sanctioned parts".
    – Piovezan
    Commented Aug 9, 2016 at 14:40
  • @Piovezan I think your interpretation is reasonable and valid :)
    – oerkelens
    Commented Aug 10, 2016 at 10:37
  • @oerkelens Thank you! And thanks again for the clear explanation :)
    – Piovezan
    Commented Aug 10, 2016 at 10:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .