PrematureConcatenation is the act of joining strings together before you really need to.
The problem is the information loss -- you can no longer tell where the boundaries were.
Sightings:
- We have seen this pattern emerge two different times when rendering tree-like data structures, where each branch recursively combines the branches below it. Instead of doing join "\n" at each branch, it turns out to make more sense to do the join at the end, when you're at the top level.
Advantages of Just-In-Time Concatenation:
- Debugging - You get a (slightly?) richer view of the data.
- Transformations - It's a little easier for branches to post-process renderings from its children and grandchildren.
- Memory usage/Concurrency - Often you want to send a concatenated string over TCP or write it to a file. If you concatenate the full string before you write it you will waste memory, and possibly keep the other end of the TCP connection waiting. A better solution is to write the smaller strings directly to the output stream, in the same order as you would have concatenated them. It is usually easy to convert the code from string-concatenating to stream-writing.
- Speed - In some languages (Python) it is more efficient to build a list of strings, and then merge them in one big operation using a join method, which concatenates all of the strings in the whole list together as a single operation. Even in C, allocating one large string and then copying all the components is more efficient than the log base 2 of n allocations and copies that you would likely do otherwise.
Also, GUI's can have a similar Play-doh'ing, except with bitmaps instead of strings. If you hold off flattening your objects until they absolutely have to be displayed, odds are, you'll end up finding uses for the richer information.
See also: PlayDohPrinciple