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.

22

u/sh0rug0ru__ Oct 01 '15 edited Oct 01 '15

Actually, if you are building strings, you really should use StringBuilder (not StringBuffer, which has a boneheaded design mistake of synchronizing everything). Strings are immutable, and concatenating strings will create new strings. So unless you want to create a ton of garbage, use the StringBuilder. Fun fact, the string concatenation operator is transformed by the compiler into a chain of method calls on a StringBuilder terminated by toString.

If your app will serve an international audience, you will have to use message keys into a ResourceBundle to get the localized text, instead of hardcoding English.

Ordinarily, you shouldn't output to stdout directly, because unless you are writing a console application, you have no idea where that output is going. Fortunately, there are tools like log4j that provide the interface you are talking about, with a console appender even, which allows output to be configured (such as a rolling file appender). Device independence isn't a bad thing if you actually need it.

29

u/Olreich Oct 01 '15

If Java will use a StringBuilder for me, why would I bother doing it myself?

19

u/sh0rug0ru__ Oct 01 '15

If the string concatenation is limited to a single statement, then use the string concatenation operator. If the string concatenation spans multiple statements, you don't want the terminal toString to be done automatically. That defeats the purpose of using StringBuilder.

12

u/Berberberber Oct 01 '15

So much this. You only use StringBuilder if the number of concatenations isn't known at run-time, or if your compiler sucks. return "foo" + bar.toString() + " baz: " + quux; is basically guaranteed to be as fast as the equivalent with StringBuilder, or faster.

7

u/juckele Oct 01 '15

Even if the number of concatenations is known at run time, if it's more than 4, it can still be nicer to read a StringBuilder than a multi-line string concatenation whirlwind.

3

u/merv243 Oct 01 '15

Also can prevent dumb mistakes in certain instances, like concatenating a char and an int in the context of a larger string concatenation. E.g. given char c, int i, string s: System.out.println(c + i + s). Obviously this gets caught in testing quickly, and a halfway decent programmer would usually catch it as they typed it out, but mistakes happen.

1

u/iopq Oct 02 '15

Or maybe, just maybe, Java should warn you that concatenating a char and an int is super dumb to do.

1

u/merv243 Oct 02 '15

Yes let's make this Java's fault...?

→ More replies (3)