alexfu / SQLiteQueryBuilder

A library that provides a simple API for building SQLite query statements in Java.
MIT License
70 stars 21 forks source link

Better memory efficient StringUtils join. #22

Closed monxalo closed 9 years ago

monxalo commented 9 years ago

Use a StringBuilder instead of the operation '+=' since this would allocate a new StringBuilder in each Loop iteration.

Changed the parameter type from String to CharSequence to allow other CharSequence objects like StringBuffers.

Also fixed minor typo in 'delimeter'.

alexfu commented 9 years ago

Instead of using a Boolean flag to determine if the loop is on the first/last iteration, consider using the classical for loop using just the indices, which is more efficient

monxalo commented 9 years ago

I thought about that too, but according to Item 46 in Effective Java by Joshua Bloch:

The for-each loop, introduced in release 1.5, gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely. The resulting idiom applies equally to collections and arrays:

// The preferred idiom for iterating over collections and arrays
for (Element e : elements) {
  doSomething(e);
}

When you see the colon (:), read it as “in.” Thus, the loop above reads as “for each element e in elements.” Note that there is no performance penalty for using the for-each loop, even for arrays. In fact, it may offer a slight performance advantage over an ordinary for loop in some circumstances, as it computes the limit of the array index only once.

alexfu commented 9 years ago

The classical for loop eliminates the need for a boolean which isn't needed at all. Granted that the for each shorthand is easier to read and may offer a slight advantage, I would prefer to not have an unnecessary boolean variable.

monxalo commented 9 years ago

Granted that the for each shorthand is easier to read.

That's was also my goal. Although the boolean is necessary for the for each loop, i think it offers a better readability to what happens in each iteration.

But sure we can go for the classical loop.