github.com/justinamiller/LiteStringBuilder
|
C#
C# alternative to StringBuilder, 70% faster and 72% less memory allocation. |
|
Dec 11, 2019 11:24 AM
(This post was last modified: Dec 11, 2019 02:57 PM by MavenGradle.)
This is some constructive criticism that may or may not help you on your development road. Don't be like this guy.
LiteStringBuilder class: First of all, the LiteStringBuilder class is incredibly large. I would advise you to split it up into multiple classes, each performing their own actions. There are quite a few imports that hold no purpose, such as: Code using System.Collections.Generic;The following variables are declared using default values and can also be simplified: Code private char[] _buffer = null;Code private char[] _buffer;Some properties can be simplified. For example the Length getter: Code public int LengthCan be rewritten as: Code public int Length => _bufferPos;You also have a char[][] that you gave a weird name (s_bool). I would advise you to use names that properly reflect the function or usage of a variable, like you did with the Length property. Also, you can simplify this code: Code private readonly static char[][] s_bool = new char[2][]by using something like (C# 8): Code private static readonly char[][] s_bool = {Also notice how I put static before readonly? That's because inconsistent modifier declarations aren't pretty to look at. There are more variables that you declare using inconsistent modifier orders. In the 'this' return function, you throw an ArgumentOutOfRangeException like so: Code if (index > _bufferPos || 0 > index)I would advise you to use the nameof operator instead for better maintainability: Code if (index > _bufferPos || 0 > index)https://docs.microsoft.com/en-us/dotnet/...ors/nameof You also have a lot of this keywords that can be avoided: Code if (other.Length != this.Length)Code if (other.Length != Length)In the Set method you implement a for loop. I would advise you to implement a for each loop instead for the sake of readability: Code for (int i = 0; i < values.Length; i++)Code foreach (int t in values)The following method can also be simplified and made more usable, using a simple trick: Code public LiteStringBuilder Append<T>(params T[] values)Code public LiteStringBuilder Append<T>(params T[] values)In the following Append method, you once again implement a lot of this keywords that can be avoided and simplified using a switch: Code private void Append<T>(T value)Code switch (value)The following Append method can also be simplified like so: Code public LiteStringBuilder Append(bool value)Code public LiteStringBuilder Append(bool value)I find that this greatly improves the readability. Same goes for the next Append method that can be simplified: Code public LiteStringBuilder Append(char[] value)Code public LiteStringBuilder Append(char[] value)The next Append method can also be simplified: Code public LiteStringBuilder Append(object value)Code public LiteStringBuilder Append(object value)This Append method can once again be simplified. Take the cast to int for example that's not required: Code public LiteStringBuilder Append(sbyte value)Code public LiteStringBuilder Append(sbyte value)The Replace function is quite another beast that you need to take a look at. I see quite a few exceptions there waiting to happen: Code public LiteStringBuilder Replace(string oldStr, string newStr)You never check if oldStr actually has any values at all which opens you up to NullReferenceException cases. There are a couple of others in there that I deliberately did not mention, which I'm sure you can figure out. If not, feel free to let me know and I'll point them out to you. The following loop is frowned upon Code doWhy not implement a regular while loop instead? It's way more readable. Finally, in the GetHashCode method, you use a variable that should be read-only: Code _bufferPosThere are quite a few other methods that could be simplified but I'm sure you get the picture with the comments that I already gave you. Feel free to let me know if you want me to look further but for now that's all I'm going to point out due to a lack of time. Utilities class: The only using statement you need is this one: Code using System.Runtime.CompilerServices;In the GetIntLength method, you can simplify the final if statement like so: Code return n < 10000000000000000000L ? 19 : 20;In the GetPaddedCapacity method, you can simplify this: Code if (num1 > 1U)into this: Code if (num1 <= 1U) return 16 << (num2 + (int) num1);Notice how I also changed the prefix increment operator to be postfix? Anyway, that's all I have time for to look into right now. I would really suggest that you refactor and simplify your code because as it stands right now, your code would not be accepted if you committed this into a repository that I control. That's not to say it's not good or does not perform better. Quite simply it means that you have a lot of work to do before your repository would be acceptable in my eyes. Read my comments to understand why that is. If I can give you another tip, take a look at git-flow. You are working on a master branch that's ahead of the development branch which is extremely weird when it comes to working with git and can cause a lot of confusion for developers that look at your code. More information about git flow can be found here: https://www.atlassian.com/git/tutorials/...w-workflow ![]() |
|
|


![[Image: Syr3DlV.png]](https://i.imgur.com/Syr3DlV.png)

![[Image: vp6iypF.gif]](https://i.imgur.com/vp6iypF.gif)