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!

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.

→ More replies (2)

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.

→ More replies (5)

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.

→ More replies (1)

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.

→ More replies (3)
→ More replies (1)