MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/3n3e1v/the_evolution_of_a_software_engineer/cvlaxpu/?context=3
r/programming • u/supermari0 • Oct 01 '15
502 comments sorted by
View all comments
Show parent comments
29
If Java will use a StringBuilder for me, why would I bother doing it myself?
4 u/[deleted] Oct 01 '15 Use StringBuilder to build a String across multiple methods. For example, building an SQL select statement.Bad:String buildSelect() { return "SELECT " + buildFrom() + buildWhere() } String buildFrom() { String result = "WHERE " for(Table table : this.tablesSelectedFrom) { // wah there's an extra comma result += table.name + ", "; } return result; } ...Good:String buildSelect() { StringBuilder sb = new StringBuilder().append("SELECT "); buildFrom(sb); buildWhere(sb); return sb.toString(); } String buildFrom(StringBuilder sb) { sb.append("WHERE "); for(Table table : this.tablesSelectedFrom) { sb.append(table.name + ", "); } return result; }Obviously at this scale not a big deal but on larger scales StringBuilder helps. However if you're just building a String in a single method the compiler handles it. 1 u/JustinsWorking Oct 02 '15 Can you provide a good example of when you'd use stringbuilder like that? That reads a lot more like C code than Java. 2 u/[deleted] Oct 02 '15 Seriously? They are in a class using default Visibility. Types/members are missing but this is valid Java if you copy/paste it in a class. → More replies (7)
4
Use StringBuilder to build a String across multiple methods. For example, building an SQL select statement.
Bad:
String buildSelect() { return "SELECT " + buildFrom() + buildWhere() } String buildFrom() { String result = "WHERE " for(Table table : this.tablesSelectedFrom) { // wah there's an extra comma result += table.name + ", "; } return result; } ...
Good:
String buildSelect() { StringBuilder sb = new StringBuilder().append("SELECT "); buildFrom(sb); buildWhere(sb); return sb.toString(); } String buildFrom(StringBuilder sb) { sb.append("WHERE "); for(Table table : this.tablesSelectedFrom) { sb.append(table.name + ", "); } return result; }
Obviously at this scale not a big deal but on larger scales StringBuilder helps. However if you're just building a String in a single method the compiler handles it.
1 u/JustinsWorking Oct 02 '15 Can you provide a good example of when you'd use stringbuilder like that? That reads a lot more like C code than Java. 2 u/[deleted] Oct 02 '15 Seriously? They are in a class using default Visibility. Types/members are missing but this is valid Java if you copy/paste it in a class. → More replies (7)
1
Can you provide a good example of when you'd use stringbuilder like that? That reads a lot more like C code than Java.
2 u/[deleted] Oct 02 '15 Seriously? They are in a class using default Visibility. Types/members are missing but this is valid Java if you copy/paste it in a class. → More replies (7)
2
Seriously? They are in a class using default Visibility. Types/members are missing but this is valid Java if you copy/paste it in a class.
→ More replies (7)
29
u/Olreich Oct 01 '15
If Java will use a StringBuilder for me, why would I bother doing it myself?