an overdose of strings can kill you - really?
In recent years I remember a discussion about a 3 lines of code, it was something easy, so let's have a look at this:
As we can see, an one liner statement was replaced with a string builder. As this is a reset password, then we shall not focus on performance as our users rest password once in a year, but the discussion was based on a style how that was done, so let me bring what we discussed.
Frist, a string builder is a great tool for dynamic text and in this case we have only one variable - so it is overkill. Secondly templating shall be moved away from logic as this is pure implementation of the process.
Now I will add a static string that will be our template, and we can see if string.Replace can help us (imho string.Replace is a heavy and will use more memory, thus it can be heavy on CPU too).
As we can see, the refactor gave us a nice looking code, one line for the template, but we lost a bit on the performance (and hidden one to store template in the class -> that in the future could be used for translation).
Regex - use REGEX - it is fast, but imho it is still a big gun for a fly.
As we started with a string concatenation, where we have "abc" + something + "xyz", and that was almost the fastest way of doing it (and clean), then let's try to add two more tests.
Firstly we will have TemplateStart and TemplateEnd as a start and end of the template and we will add them together
Secondly we will use string concat to see if that will give us any advantage.
Below gist with the tested code.
Have a great day!
Comments
Post a Comment