Today I came across a piece of code like the following.
Dim strSQL As New StringBuilder
When I encounter code I don’t agree with, I try to understand what made the developer code it that way.
Looking at this one I can only presume:
- He wanted to make his code more legible by separating the SQL Command to different lines.
- In doing that, he wanted to avoid string concatenation, because "string concatenation is performance wise evil"!
My answers to these points are:
- In this particular case, the code became harder to read than if it was stated in a single line.
Dim strSQL As String = "SELECT ProductID, Name FROM Production.Product"
- That may be true in a lot of situations, but is not true in this particular one: concatenating small string constants, as the compiler is smart enough to generate one single string constant comprised of what we see in code as being multiple strings being concatenated
Dim strSQL As String = _
"SELECT " + _
" ProductID, " + _
" Name " + _
Dim cmd As New SqlCommand(strSQL, cnnConn)
gets compiled to
L_001f: ldstr "SELECT ProductID, Name FROM Production.Product"
L_0027: callvirt instance void [System.Data]System.Data.SqlClient.SqlConnection::Open()
L_002f: newobj instance void [System.Data]System.Data.SqlClient.SqlCommand::.ctor(string, class [System.Data]System.Data.SqlClient.SqlConnection)
* Q: So what does this post has to do with saving the planet?
A: Keep it simple -> spend less processor cycles -> spend less energy -> … -> … ->…. Oooh… you get it!
One thought on “Keep it simple (and help to save the planet)”