4

I have been programming for decades but I have not been used to object oriented programming. But for recenet years, I had a great opportunity to learn OOP, its principles, and a lot of patterns that are great.

Since I've learned OOP, I tried to apply them to a couple of projects and found those projects successful. Unfortunately I didn't follow extreme programming that suggests writing test first, mainly because their time frame were tight. What I did for those projects were

  1. Identify all necessary classes and create them with proper properties and methods
  2. whenever there is dependency between classes, write interface between them
  3. see if there is any patterns for certain relationships between classes to replace

By successful, I meant that it was quick development effort, the classes can be reused better, and flexible enough so that another programmer does not have to change something else to fix another part.

But I wonder if this is a good practice. Of course, I know I need to put writing unit tests first in my work process. But other than that, is there any problem with this approach - creating lots of interfaces - in long term?

6
  • Are you asking if there could be any problems if you do not write tests before developing your code?
    – Giorgio
    Commented Sep 8, 2012 at 17:57
  • @Giorgio No. I understand that part. I am asking if my interface-heavvy programming is a good practice. Commented Sep 8, 2012 at 17:59
  • Without looking at your code, that's a tough question to answer. It's also somewhat language dependent: What's necessary and good in Java might be overkill in Ruby. However, I disagree with your premise that Test-First is slow: in my experience, writing tests first saves you time.
    – philosodad
    Commented Sep 8, 2012 at 18:15
  • @philosodad You are right. But in our prototype-first process, it is just not easy to write tests first without much specifications - requirements in hand. Or Is there any way? Thanks for your comment. Commented Sep 8, 2012 at 18:24
  • Unit tests typically only test the functionality of a single object. Other objects should be mocked... you just want to know that the other (possibly future) interface will be called with the right values, or that if that future interface/object returns a value, your current object will behave as expected. I work in Agile shops and we use this sort of TDD to drive requirements and design. For Java, I recommend the book Test Driven (manning.com/koskela)
    – philosodad
    Commented Sep 8, 2012 at 18:38

1 Answer 1

2

Yes .. Using interfaces in Object Oriented Programming would be good practice. Benefits of using interfaces in your model :

  1. Interfaces provides asynchronous development without defining Class implementations.
  2. Multiple class implementations of a particular behavior/operation.
  3. Interfaces provides a defined set of standards that must be followed by implementation classes.
  4. If you are writing a API a utility package you must expose the operations using interfaces so that a user of that API can easily integrate.

These features pointed above are automatically included with your model in case of using interfaces but in case of direct class implementations you need to take care of these manually.

However there are some disadvantages also :

  1. New development members needs to follow the standards defined by interfaces if they are developing new implementations. It is bit complicated and takes time to develop code by new members.
  2. In case of changing the interface standards we need to change into all the class implementations as well.
5
  • Thanks for your encouragement. But can you see any downside? Commented Sep 8, 2012 at 18:26
  • @david I have included some disadvantages. Commented Sep 8, 2012 at 18:41
  • It is important to note that interfaces should be used only when required or for specific purposes like testing/mocking. Making everything an interface is one reason why COM programming became very difficult.
    – akton
    Commented Sep 9, 2012 at 13:44
  • @akton I don't agree with you.. can you provide any example where using interfaces makes trouble in programming. Commented Sep 9, 2012 at 14:06
  • @SatishPandey As I said above, Windows COM was built around the idea of objects that implemented multiple interfaces for different functions and interfaces were the only way to access functionality. Interface immutability created sometimes 7 or 8 versions of the same interface making it hard to keep up for both object implementers and callers.
    – akton
    Commented Sep 9, 2012 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.