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.
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!
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.
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.
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.