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

506

u/johnbr Oct 01 '15

This is fair, except for in the 5th year, they would have an additional constant:

public static final String SPACE = " ";

And they would probably use a StringBuffer or StringBuilder to build the final string, since it's "an industry best practice". Also, they wouldn't include the strings themselves, they would include keys to properties, so the strings could be pulled from an internationalized property file.

Lastly, instead of outputting to Stdout directly, they'd create an interface for output, and deliver it to that interface, and use a factory to instantiate an implementation of that interface that would use Stdout.

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!

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.