What are best practices around toString?

tsbockman thomas.bockman at gmail.com
Sat Oct 1 17:50:54 UTC 2022


On Saturday, 1 October 2022 at 10:02:34 UTC, Salih Dincer wrote:
> On Saturday, 1 October 2022 at 08:26:43 UTC, tsbockman wrote:
>> `StringBuilder` is a utility shared across the entire project:
>
> Appender not good enough; at least in terms of allocating 
> memory and accumulating a string?

`Appender` is a legitimate option, but unless it is provided with 
a good estimate of the final length at the beginning, it will 
allocate several times for a longer string, and the final buffer 
will be, on average, 50% larger than needed.

Neither of these things is a major problem, but `StringBuilder` 
is only a few lines of code to perfectly minimize allocation, so 
why not?


More information about the Digitalmars-d-learn mailing list