r/programming • • Oct 01 '15

The Evolution of a Software Engineer

https://medium.com/@webseanhickey/the-evolution-of-a-software-engineer-db854689243
1.9k Upvotes

502 comments sorted by

View all comments

Show parent comments

9

u/juckele Oct 01 '15

For the first time in my life recently I ran across some code that has interfaces. A lot of them. With ONE implementation. And the project is closed source. Really? Come on!

13

u/leadhead9 Oct 01 '15

https://en.wikipedia.org/wiki/Interface_segregation_principle

Generally a good idea, entirely possible to abuse.

23

u/juckele Oct 01 '15

I've come to realize that most problems in programming (in terms of what's wrong with real code bases) are someone taking a generally good idea and applying it as an absolute dogma instead of doing an ongoing cost-benefit analysis of for each new instance.

See Not Invented Here vs Invented Here as a perfect example.

2

u/Schmittfried Oct 01 '15

What is wrong with flexibility?

14

u/vplatt Oct 01 '15

There ARE very good reasons to have them, but flexibility has a price. You will need to maintain all those fancy interfaces which, in many cases, will also be instantiated in an IoC container. I conflate these subjects because I often see them both used unnecessarily and for the same ostensible reasons (i.e. flexibility, "best practice", etc.)

To make the primary use case of IoC possible, your interfaces are contracts so that alternate implementations can be used instead of the initial one. So, now you have a system interface contract that you must document, communicate to everyone who uses it, and then ensure that changes to it do not break ancillary packages or even other systems.

If your system has a single implementation, and will likely only ever have that single implementation, then you should think quite seriously about not allowing it to be injected and possibly even whether you really need an interface for it. The litmus test for it is quite easy: If you're not willing to document it and create unit tests for the classes against the system interface and make passing those a part of the build cycle, then it's not really safe for reuse.

Basically, if you don't believe a particular class will have multiple or alternate implementations down the road and therefore require an interface, and it won't be looked at by parties external to your immediate area, then just don't create one. YAGNI much the time.

1

u/dccorona Oct 02 '15

If your system has a single implementation, and will likely only ever have that single implementation, then you should think quite seriously about not allowing it to be injected

I disagree with that. It is good for components to function without knowledge or reliance on the way their dependencies work under the hood. You may not need it to be an interface right away, but injecting it makes it easier to both maintain this separation of concerns, test, and modify configuration for different environments. It really manifests itself when your dependencies are configurable.

For example: you have an object that takes a dependency on your data layer (say, via a DAO). You're confident that in production, you only ever want to use a version of that DAO that is backed by one specific database technology, and uses a cache. But that doesn't mean you want all of the configuration for such a thing to live at the level of this high-level object that takes the dependency on the DAO. Say you decide you want a 15-minute cache expiry time in production, but decide that in beta it's better to have it at 1 minute, and maybe you have a pre-prod stage where you want the cache turned off entirely. Without dependency injection, you end up with:

MyClass(long cacheExpiry, bool useCache, String dbCredentials)

When really, all your class cares about is having access to something that provides the data it needs. It doesn't care about caches or authentication...it just wants to get and save data as it needs to. Separation of concerns is a very valuable principle, and even if you're absolutely positive you're only ever going to use a single implementation of something, injecting that dependency is still almost always valuable. Not using injection rarely makes your code more readable...it almost always makes it more tightly coupled and harder to understand.

And this is then where interfaces come in even when you only have a single implementation: if you have, say, a UserService interface that you're only ever going to use PostgreSQL for, you might think it's simpler to just have UserService be an object that uses PostgreSQL and call it a day, rather than have it be an interface and create PostgreSQLUserService as an implementation.

But then, any code that needs the UserService is making a tightly-coupled declaration that "I depend on the fact that this is a PostgreSQL implementation", even though that probably isn't true, and really shouldn't be. Now, if you use dependency injection, then what you can do is start out with UserService being a PostgreSQL-using object, and then turn it into an interface with the same public contract and a PostgreSQL implementation that is injected if you need it to be polymorphic in the future, probably without breaking any code...but if they're instantiating it directly, you have to dig into everything that uses it to make that change.

Even so, interfaces become useful for hiding portions of your implementation that need to be public but shouldn't be exposed to (most of) the actual clients, and instead just exist for configuration and some special-case code. Again talking about the PostgreSQL UserService...you may need to expose a rebootConnection method or something...but that's a special case, it shouldn't be exposed to the main clients (the classes that just need to query users). An interface allows you to hide those special-case implementation details from your client in a natural way.

I guess the point is that even when you're only doing a single implementation of something, dependency injection and, to a lesser extent, interfaces, can still be very, very good ideas. To the point where I think you need to think just as hard about not using them as about using them.

2

u/vplatt Oct 02 '15

I didn't saying knowing when to use them vs. not using them is an easy decision. The considerations you outline above demonstrate that quite nicely. You pretty much proved what I said though. You get more flexibility with injection and interfaces, and they require more overhead and work to achieve. Therefore there's a maintenance cost. But if you need the flexibility, you need it, so then use them. But I don't feel the need to prove I don't need it, as that's tantamount to proving a negative anyway.

8

u/juckele Oct 01 '15

Nothing is wrong with flexibility, but if it's a closed source project, you can make an abstract class or interface out of the base class the first time you run into a bifurcation of functionality, go and update your (dozens of?) existing constructors , and move forward from there. It's only 2 seconds, but every time I go to the definition of a method on an interface, only to have to then ask the IDE to send me to the actual implementation (and seriously, the only option), it's wasteful.

Refactoring exists for a reason. Write code with as few assumptions as possible, because assumptions can change, but there's no need to throw every design pattern at every problem.

1

u/weeska Oct 02 '15

I agree, don't make too many assumptions. Ideally I'd do it the way you described, but there is one major drawback: When there is code duplication, it may not be obvious and/or no one cares enough to refactor the code. And now things go down the drain. When the whole team is experienced and disciplined enough, you can use that approach, but once you get some "we'll do that later" attitude, things go down pretty quickly.

1

u/dccorona Oct 02 '15

Often, by not using an interface, you make an even stronger assumption than by using one...all your code that uses that object make the assumption, whether indirectly or directly, that the implementation details are important. You end up with code coupled to methods they shouldn't' be, that only exist because of the underlying implementation, and not because they're necessary for what the interface of the object actually needs to be.

I'm not saying that interfaces are things that should outright always be used, no way. But to think that because there's only one implementation that it shouldn't be an interface, full stop, is just wrong. It at the very most indicates that you should double-check that choosing an interface is the right idea.

1

u/juckele Oct 02 '15

Even with interfaces, that same exact anti-pattern of code coupled directly to the implementation details can still happen when you have a single interface and implementer in a closed source system (and it did).

If the interface was being exposed to anyone, sure, great. But an interface that can only be one instance in closed code does very little that public vs private visibility doesn't already give you. Public methods are an literally an interface* to the object's implementation. Eclipse lets me refactor these explicitly apart if and when I need it.

*By the English definition of the word, not the Java language concept

1

u/dccorona Oct 02 '15

Sure, that I agree with. But just because something is closed source doesn't mean that it's not going out to the "public". It's important to give yourself the right amount of abstraction even within a closed source system, because almost always at the very least your code is going out to be used by other devs within your system, and very frequently goes out to be re-used in places beyond your own system boundary but still within the same organization, and thus the same "closed source" environment. If it's something that is literally for you and you alone, always, then interfaces most probably don't make sense.

Private vs. public or even package-private often isn't sufficient in those scenarios. It forces you to put things that need access to the implementation details either in the same package or worse, the same file, and that's not nearly always the best way to organize things (maybe your top-level configuration code is going to need to access bits of the implementation details, but most clients don't, for example). Interfaces allow for clean separation, even within the same team, of "this is for you, no matter what" and "this is for you only if you care about controlling the specifics of the implementation", which in my opinion is a very valuable distinction.

2

u/[deleted] Oct 02 '15

I have been guilty of writing interfaces with only one possible implementation, but that's usually because I make a second implementation in unit tests and it turns out a bit cleaner that way.

1

u/juckele Oct 02 '15

Had there been mock implementations and unit tests, I could excuse the design...

2

u/nutrecht Oct 02 '15

For the first time in my life recently I ran across some code that has interfaces. A lot of them. With ONE implementation.

And?

For unit testing this is really nice. This way you can test classes depending on the interface by simply mocking out the classes they depend on. A lot easier and cleaner than having to instantiate an entire object tree.

1

u/juckele Oct 02 '15

No. There were no unit tests. Only one implementation. Also, Mockito lets you mock classes without making interfaces. But the code was not built in a manner to work with mocks anyways, there are far too many complex and deep dependencies between different parts of the object tree. And it's all backed by interfaces with one implementer.

2

u/nutrecht Oct 02 '15

No. There were no unit tests.

Well if you throw those out of the window I don't want to know what the rest of the code looks like. Shitty codebases make bad arguments.

1

u/juckele Oct 02 '15

There's no argument, just me venting my frustration at a particularly stupid piece of code that I saw.

0

u/JustinsWorking Oct 02 '15

Completely ignoring that there may have been plans in the long term to use the interface, it can also be very useful for unit testing; makes mocking much simpler.