Agile FAQs
  About   Slides   Home  

 
Managed Chaos
Naresh Jain’s Random Thoughts on Software Development and Adventure Sports
     
`
 
Discovering...
Industrial Logic

Microblog Feed
    Previous Feeds...
    Recent Thoughts

    Recent Comments
    Categories
    Archives
    June 2009
    M T W T F S S
    « May   Jul »
    1234567
    891011121314
    15161718192021
    22232425262728
    2930  
    RSS Feed
    Add to Technorati Favorites

    Killing Speculative Generality Code Smell

    I’m just reviewing a project’s code. I found a common pattern used in their code base. Every class implements an Interface. Each interface is only implemented by one class. Even more interesting, this interface is not exposed outside. In other words, its not exposed as part of the API.

    Then my question is

    Why do we need the interface? Why can’t we just use the class directly?

    Apparently there is no valid answer. Some told me,

    • Spring forces you to have interfaces.
      • That’s not true.
    • Some told me their mocking framework does not support mocking a class.
      • This is also not true. Most mocking frameworks come with a class extension. Some new frameworks, don’t even distinguish between an interface and a class.

    Anyway, we don’t need one stupid interface for every class we create. YAGNI. When we need it, we’ll create it. This is one form of speculative generality code smell.

    Go ahead, kill it!

    • Share/Bookmark
    • Good point Rick. Certainly I'm not suggesting, not to use interfaces. They can be very powerful tools for abstraction and decoupling.

      Either extremes is bad. No interface or every class inheriting from an interface.
    • I think it is a bad idea to have every class extend an interface, but perhaps the developers are being *very* clever by distinguishing types (in the type theory sense) from classes.

      Well, no I don't think that is what's really happening, and it is certainly not an appropriate use of java.
    • Amen, brother.

      I run into this ant-pattern at work most often with intermediate developers. The journeymen developers don't think to make the interfaces, and the master developers have reached your conclusion.
    • While creating an interface for EVERY class is overkill, I sometimes do like to use internal interfaces. It's a very effective way to decouple classes or invert the reference hierarchy of friend assemblies.

      As an example, I sometimes use interfaces as an alternative to mock frameworks for testing.
    blog comments powered by Disqus
        Licensed under
    Creative Commons License
    Design by vikivix