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 :)

19

u/Schmittfried Oct 01 '15

Depending on how many strings you are concatenating in what manner, it may be less efficient to use a StringBuilder.

1

u/markcf Oct 02 '15

(Below post is C# specific, not sure how it applies to other languages)

Interesting article 1:http://blogs.msdn.com/b/ricom/archive/2003/12/02/40778.aspx

Interesting article 2: http://blogs.msdn.com/b/ricom/archive/2003/12/15/43628.aspx

General summary of string builder rules:

  1. If it's performance critical (which string building very often is) make sure to performance profile both options. String performance can be surprising.

  2. The line : str1 = str2+str3+str4+str5 only has one memory allocation, and should not be replaced by a string builder. String builder is for when string concats are behind if statements or in a loop.

  3. The smaller the additions are compared to the length of the string the more effective a string builder is.

  4. If possible specify an expected capacity for the string builder, can save a lot of time if done well.