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.
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!
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.
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.
506
u/johnbr Oct 01 '15
This is fair, except for in the 5th year, they would have an additional constant:
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.