Public Function buildSQLStatement() As String
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
sb.Append("SELECT * FROM X_TABLE ORDER BY X_FIELD_1")
Return sb.ToString
sb = Nothing
End Function
However, they do not get points for efficiency on this particular function. This is a classic example of what I call "overcoding" -- which means exactly what you think it means.
Have you ever read a book (I highly recommend anything by Dean Koontz) and after pages of descriptions of scenery and flowers and landscape, you were ready to scream "Enough already! Get to the point!"? Have you? That, my friends, is overwriting, and it is as exasperating to me as overcoding.
So, let's get to the point, shall we? Here's the new code:
Public Function buildSQLStatement() As String
Return "SELECT * FROM X_TABLE ORDER BY X_FIELD_1 "
End Function
The moral of the story?
Don't overthink what you're doing. Write code that does what it needs to, and no more. If you need to explain what's going on, use a comment. And yes, use StringBuilder when it's needed, and don't when it's not. The best intentions in the world won't make your code run faster. Please, stop overcoding.
NOTE: Even though the function itself has been optimized, there's STILL a potential problem here. Can you spot it? I will address it in my next post.