Before, I had this code, sometimes it is longer:
String dbEntry = "idBldgInfo = '" + currentBldgID + "',scenario = '305',installCost='" +
installCost + "',annualSave='" + annualSave + "',simplePayback='" + simplePayback +
"',kwhPre='" + preKWH + "',kwhPost='" + postKWH + "',co2Pre='" + preCO2e + "',co2Post='" +
postCO2e + "',mbtuPre='" + preMBtu + "',mbtuPost='" + postMBtu + "',shortDescription='" +
econ4ShortDescription + "',category='" + category + "',longDescription='" +
econ4LongDescription + "'";
I refactored it into:
String newDbEntry =
getDBEntryFromDBPairs(
new DBPair("idBldgInfo", currentBldgID), new DBPair("scenario", "305"),
new DBPair("installCost", installCost), new DBPair("annualSave", annualSave),
new DBPair("simplePayback", simplePayback), new DBPair("kwhPre", preKWH),
new DBPair("kwhPost", postKWH), new DBPair("co2Pre", preCO2e),
new DBPair("co2Post", postCO2e), new DBPair("mbtuPre", preMBtu),
new DBPair("mbtuPost", postMBtu), new DBPair("shortDescription", econ4ShortDescription),
new DBPair("category", category), new DBPair("longDescription", econ4LongDescription));
It looks only slightly better. Is it worth changing it?
The getDBEntryFromDBPairs
method is much more efficient since it uses a StringBuilder. But IDK. Any thoughts/suggestions are appreciated.
2 Answers 2
I think your second approach is better. It is more readable and more easily maintained. I don't have much experience with databases, but lets say (sorry if its a stupid example) there is a database that causes problems for strings like "colId = colValue
", and to fix it you need to remove the spaces. How many places there would be where you will go through String dbEntry = ...;
statements to correct this problem? What if for a certain database you need to use :
instead of =
? Using data structures like this is definitely going to make your life a lot easier in such cases.
So the conclusion is, are there any such cases that you need to consider where you might need to go through your code and change the resultant string? Because changing DBPair's implementation once would be a lot easier.
Your first implemenattion also uses StringBuilder
under the hood.
See javadoc of java.lang.String
:
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method
So you cannot expect an speed improvement by your second version.
-
\$\begingroup\$ Well, JLS only says it may do that. So why not. \$\endgroup\$jn1kk– jn1kk2013年01月10日 18:13:25 +00:00Commented Jan 10, 2013 at 18:13
-
\$\begingroup\$ Experience shows it is done. \$\endgroup\$MrSmith42– MrSmith422013年01月10日 18:51:41 +00:00Commented Jan 10, 2013 at 18:51
-
\$\begingroup\$ I tried to make a test. But JIT optimizes the hell out of it, so the + concatenation turns out faster. \$\endgroup\$jn1kk– jn1kk2013年01月10日 19:00:06 +00:00Commented Jan 10, 2013 at 19:00
dbEntry
? Be sure you are not going to concatenate it with other strings to build a SQL query, it is extremely risky and will lead to SQL Injections. \$\endgroup\$currentBldgID
.currentBldgID
is valided in the front-end. \$\endgroup\$