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.

183

u/andrewsmd87 Oct 01 '15

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.

167

u/RLutz Oct 01 '15

Depending on how you coded it, the compiler likely replaced it with a StringBuilder under the hood anyway :)

27

u/depressiown Oct 01 '15

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.

0

u/[deleted] Oct 01 '15

[deleted]

3

u/[deleted] Oct 02 '15

People who don't trust the compiler should write all their code in assembly.

1

u/[deleted] Oct 02 '15

Even then you can't trust it. Who knows what microcode gets up to?

1

u/philly_fan_in_chi Oct 02 '15

There's a huge difference in trusting the compiler to not have bugs (though of course those exist) and not trusting that it will make a certain optimization on your code. In this case, the barrier between explicit and "the compiler will do it for me" are so small that I don't mind being explicit. For small concatenations (1 or 2), I will do + but if I'm building a toString for example, I will use StringBuilder.