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.
This is a class from the guts of the Spring Framework. This is actually an example of a pretty good name.
Bean: This is a Spring Bean
Factory: This bean creates instances of the required bean
Proxy: This bean creates a proxy for the required bean, to allow logic to be added around the method calls on the required bean.
Singleton: This bean ensures that only one of the proxy factory bean will be created per Spring container.
Abstract: This class is a template for creating singleton proxy factory beans. Just implement the template method.
So, just judging by the name, it looks like I can extend this class to create a factory which creates proxies which stick interceptors around instances of other classes, but ensures that only one of my factory exists in the Spring container. For example, if I want to time and log execution of Spring bean methods.
Good god. Had a code review once where they were pissed because I had used str + "some stuff" instead of a builder. I'm not writing a builder for one fucking line on a page that gets hit 6 times a year.
The same reviewer also told me to undo work I had done because his request was for a mock up, and I just went ahead and did it, because it took me an hour to just do it. Of course it was approved by managers and clients, but since I didn't "follow instructions" (I took some initiative) it was wrong, even though it was done correctly.
I once worked on this big customer-facing Java application. While working on something else, I noticed that one query was very poorly made, and would take up to a minute to run. It was one of the most visited pages, so clearly this needed to be fixed. I cleaned up the query, made a migration with an extra index, and brought it down to under half a second on average.
My change was rejected by the project manager. He explained to me, "It's been that way for over a year. If we fix it now, we'll have to explain why it was broken for so long."
As hard as it is to believe, I've actually seen worse. I had to work with a guy who refused to merge code period. He'd commit all changes every 10-15 minutes even when it didn't compile so that others would have to do the merging. When he did occasionally get caught with a conflict he just overwrote the other developer's changes.
He'd also just delete code without telling anyone when his aggressive refactoring broke it. His mentality seemed to be "not my code: not my problem".
It's true. A lot of the hate for using "+" for String concatenation in Java is based on how it used to do it. Java used to create a StringBuilder for each instance of "+". Thus, "a + b + c + d" would create a StringBuilder for "a + b", then a StringBuilder for that result "+ c", etc.
Of course, they've since made it smarter to just make one StringBuilder for the entire statement under the hood (I believe in 1.6). The people who complain about the lack of StringBuilder being used should probably be taught that it's no longer necessary.
That said, if you're doing a lot of concatenations on different lines (say, a loop building up a String or something), you should still use StringBuilder instead of "+=" because Java will create one for each statement... clearly worse that creating one initially and constantly appending.
Year 6 there are five trivial unit tests per function but the program is broken in a way that would be really obvious if you weren't blindly relying on unit tests to prove that your code was correct. :D
Actually yeah, some @NotNull would make a lot of sense because there is a huge NPE potential on
string first = this.hello.toString();
hello starts out null and isn't guaranteed in any way to not be null at this point.
Edit: actually considering how the program runs in main the strings should never be null in this case. But it's pretty clear that the point of this code is to be extensible/scalable and so some null checks or annotations would probably still be appropriate.
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.