CodeSOD: Building a Line Note

CodeSOD: Building a Line

The author discusses a common programming mistake where developers unnecessarily use StringBuilder objects. This practice, dubbed "Somebody told us string builders were more efficient, so we always use string builders, regardless of whether or not that actually makes sense," stems from a misunderstanding of performance optimization. The provided code example, ReadFromFile, demonstrates this by reading a file into a string, then appending a newline character, and finally adding this to a StringBuilder. The StringBuilder is then immediately converted back into a string. This is inefficient because the StringBuilder offers no real advantage in this specific scenario. A simpler approach, like return sr.ReadToEnd() & Constants.vbLf, would achieve the same result more effectively. The author argues this is a superstitious application of StringBuilder without understanding its purpose, treating it as a universal performance fix rather than a tool for specific use cases like building complex strings piece by piece. The code blindly applies StringBuilder because it's perceived as faster, ignoring the actual performance implications and the current requirements.