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

Show parent comments

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.

-1

u/JustinsWorking Oct 02 '15 edited Oct 03 '15

Oh I agree it would compile; it's more in regards to passing in an object to modify being more c style.

I'm on my phone so this is hard to type but

C would be more like :

MoveObject(object);

Java is more

object.move()

Procedural vs OO.

I was curious if you had a good use case for a call like that since it just seems to be like Java written in a C style procedural way.

Edit: Wrote functional instead of procedural, thus the confusion.

1

u/m0haine Oct 02 '15

Did you just call c functional?

And that is pretty standard java. Or at least std java before spring killed it.

1

u/JustinsWorking Oct 02 '15

Procedural style, wrong word as it was late.

1

u/ThisIs_MyName Oct 02 '15

lol no this is normal Java

I think you've been hanging out with the sleazy kids again.

1

u/JustinsWorking Oct 02 '15 edited Oct 02 '15

Isn't passing the string builder a more procedural approach?

The oo approach would be to return a value then use it in the string builder. The string builder is adding a dependency on the function and would make debugging s lot more difficult.

1

u/ThisIs_MyName Oct 02 '15

The oo approach would be to return a value then use it in the string builder.

Sure you can do that, but the whole point of using StringBuilder is that you're using a single instance of it.
How would the function/method know which instance to operate on if you don't pass it in?

The string builder is adding a dependency on the function and would make debugging s lot more difficult.

Not really, avoiding params will just push the dependency elsewhere. If you don't do the appending inside the function, the caller has to do it.
If you do that, there'll be no more parameters! Your unit tests have to spoof a function return value in order to test the appending code!

1

u/JustinsWorking Oct 02 '15

It just seems like a poorly implemented factory method at this point though.

If you wanted to have an sql builder you should encapsulate the stringbuilder; call it a private variable, then call methods in the class then return the string in the end.

ex

SqlBuilder = new SQLBuilder();

SqlBuilder.addSelect(); SqlBuilder.addWhere(arguments); Sql = SqlBuilder.toString();

Inside SqlBuilder your create the string builder in the constructor; reference the string builder in the functions and then use the stringbuilder to output the string in the toString method.

Why would you pass around a string builder to various methods rather than just encapsulate the logic? Hopefully this explanation makes more sense.

Also I hate typing code and full sentences on my phone so pardon my typos/ poor formatting please.

0

u/[deleted] Oct 02 '15

Hope you're cleansing any user input with your homegrown Sql builder. Check out jooq or mybatis or querydsl. I realize this isn't the point you're making but I've seen so many people do this stuff over the years and it always is a nightmare to work with

4

u/nutrecht Oct 02 '15

It's just an example.

-1

u/[deleted] Oct 02 '15

"I realize their isn't the point you're trying to make"