Refactoring String Into a Specific Type

Introduction

 
While this article title may sound controversial, since there is clearly nothing wrong with using string in your code, below I’ll show the cases where string type doesn’t clearly communicate all the necessary properties of a domain in question. Then, I’ll show how this can be handled. You can watch the full code on Github.
 

The Code

 
Recently, I was tasked to write code that converts Linux permissions to their octal representation. Nothing too fancy, just a static class that does the job. Here’s the code:
  1. internal class PermissionInfo  
  2. {  
  3.     public int Value { getset; }  
  4.     public char Symbol { getset; }  
  5. }  
  6.   
  7. public static class SymbolicUtils  
  8. {  
  9.     private const int BlockCount = 3;  
  10.     private readonly static Dictionary<int, PermissionInfo> Permissions =   
  11.                        new Dictionary<int, PermissionInfo>() {  
  12.             {0, new PermissionInfo {  
  13.                 Symbol = 'r',  
  14.                 Value = 4  
  15.             } },  
  16.             {1, new PermissionInfo {  
  17.                 Symbol = 'w',  
  18.                 Value = 2  
  19.             }},  
  20.             {2, new PermissionInfo {  
  21.                 Symbol = 'x',  
  22.                 Value = 1  
  23.             }} };  
  24.   
  25.   
  26.     public static int SymbolicToOctal(string input)  
  27.     {  
  28.         if (input.Length != 9)  
  29.         {  
  30.             throw new ArgumentException  
  31.                   ("input should be a string 3 blocks of 3 characters each");  
  32.         }  
  33.         var res = 0;  
  34.         for (var i = 0; i < BlockCount; i++)  
  35.         {  
  36.             res += ConvertBlockToOctal(input, i);  
  37.         }  
  38.         return res;  
  39.     }  
  40.   
  41.     private static int ConvertBlockToOctal(string input, int blockNumber)  
  42.     {  
  43.         var res = 0;  
  44.         foreach (var (index, permission) in Permissions)  
  45.         {  
  46.             var actualValue = input[blockNumber * BlockCount + index];  
  47.             if (actualValue == permission.Symbol)  
  48.             {  
  49.                 res += permission.Value;  
  50.             }  
  51.         }  
  52.         return res * (int)Math.Pow(10, BlockCount - blockNumber - 1);  
  53.     }  
  54. }  
The code does its job. However, it left me unsatisfied because the knowledge about the permission is scattered all over the place, i.e., magic number 9 or ConvertBlockToOctal method which relies on permissions being in a certain well-defined order. This left me wondering whether the string is the best way to represent Linux permission.
 
The question is rather rhetorical since Linux permission possesses additional constraints which string as a general datatype doesn’t. So the idea is to impose those restrictions on my input datatype or use OOD terminology to encapsulate them.
 

Testing

 
Automated tests are the necessary prerequisite for each refactoring. For this task, my test-suite isn’t really exhaustive, but it is enough to cover the case provided in the spec.
  1. [Fact]  
  2. public void HandlesCorrectInput()  
  3. {  
  4.     SymbolicUtils.SymbolicToOctal("rwxr-x-w-").Should().Be(752);  
  5. }  
Extracting SymbolicPermission Class
  1. internal class SymbolicPermission  
  2. {  
  3.     private struct PermissionInfo  
  4.     {  
  5.         public int Value { getset; }  
  6.         public char Symbol { getset; }  
  7.     }  
  8.   
  9.     private const int BlockCount = 3;  
  10.     private const int BlockLength = 3;  
  11.     private const int MissingPermissionSymbol = '-';  
  12.   
  13.     private readonly static Dictionary<int, PermissionInfo> Permissions =   
  14.                                      new Dictionary<int, PermissionInfo>() {  
  15.             {0, new PermissionInfo {  
  16.                 Symbol = 'r',  
  17.                 Value = 4  
  18.             } },  
  19.             {1, new PermissionInfo {  
  20.                 Symbol = 'w',  
  21.                 Value = 2  
  22.             }},  
  23.             {2, new PermissionInfo {  
  24.                 Symbol = 'x',  
  25.                 Value = 1  
  26.             }} };  
  27.   
  28.     private string _value;  
  29.   
  30.     private SymbolicPermission(string value)  
  31.     {  
  32.         _value = value;  
  33.     }  
  34.   
  35.     public static SymbolicPermission Parse(string input)  
  36.     {  
  37.         if (input.Length != BlockCount * BlockLength)  
  38.         {  
  39.             throw new ArgumentException  
  40.                   ("input should be a string 3 blocks of 3 characters each");  
  41.         }  
  42.         for (var i = 0; i < input.Length; i++)  
  43.         {  
  44.             TestCharForValidity(input, i);  
  45.         }  
  46.   
  47.         return new SymbolicPermission(input);  
  48.     }  
  49.   
  50.     public int GetOctalRepresentation()  
  51.     {  
  52.         var res = 0;  
  53.         for (var i = 0; i < BlockCount; i++)  
  54.         {  
  55.             res += ConvertBlockToOctal(_value, i);  
  56.         }  
  57.         return res;  
  58.     }  
  59.   
  60.     private static void TestCharForValidity(string input, int position)  
  61.     {  
  62.         var index = position % BlockLength;  
  63.         var expectedPermission = Permissions[index];  
  64.         var symbolToTest = input[position];  
  65.         if (symbolToTest != expectedPermission.Symbol &&   
  66.               symbolToTest != MissingPermissionSymbol)  
  67.         {  
  68.             throw new ArgumentException($"invalid input in position {position}");  
  69.         }  
  70.     }  
  71.   
  72.     private static int ConvertBlockToOctal(string input, int blockNumber)  
  73.     {  
  74.         var res = 0;  
  75.         foreach (var (index, permission) in Permissions)  
  76.         {  
  77.             var actualValue = input[blockNumber * BlockCount + index];  
  78.             if (actualValue == permission.Symbol)  
  79.             {  
  80.                 res += permission.Value;  
  81.             }  
  82.         }  
  83.         return res * (int)Math.Pow(10, BlockCount - blockNumber - 1);  
  84.     }  
  85. }  
What happened is that class SymbolicPermission now holds all the knowledge about Linux permission structure. The heart of this code is the Parse method that checks whether the string input matches all the necessary requirements. Another point to highlight is the use of a private constructor.
  1. private SymbolicPermission(string value)  
  2. {  
  3.     _value = value;  
  4. }  
This makes Parse the single entry point, thus disabling the possibility to create permission that doesn’t match all the required constraints.
 
Now the usage of the old static method looks as simple as:
  1. public static int SymbolicToOctal(string input)  
  2. {  
  3.     var permission = SymbolicPermission.Parse(input);  
  4.     return permission.GetOctalRepresentation();  
  5. }  

Bonus - Refactoring SRP Violation

 
At this point, ConvertBlockToOctal method not only converts a block of permissions to its octal representation but also extracts it from the provided input.
  1. private static int ConvertBlockToOctal(string input, int blockNumber)  
  2. {  
  3.     var res = 0;  
  4.     foreach (var (index, permission) in Permissions)  
  5.     {  
  6.         var actualValue = input[blockNumber * BlockCount + index];  
  7.         if (actualValue == permission.Symbol)  
  8.         {  
  9.             res += permission.Value;  
  10.         }  
  11.     }  
  12.     return res * (int)Math.Pow(10, BlockCount - blockNumber - 1);  
  13. }  
This violates the single responsibility principle. This is the reason why we will split this code into two methods.
  1. private string GetBlock(int blockNumber)  
  2. {  
  3.     return _value.Substring(blockNumber * BlockLength, BlockLength);  
  4. }  
  5.   
  6. private int ConvertBlockToOctal(string block)  
  7. {  
  8.     var res = 0;  
  9.     foreach (var (index, permission) in Permissions)  
  10.     {  
  11.         var actualValue = block[index];  
  12.         if (actualValue == permission.Symbol)  
  13.         {  
  14.             res += permission.Value;  
  15.         }  
  16.     }  
  17.     return res;  
  18. }  
Let’s have a look at how they are called:
  1. public int GetOctalRepresentation()  
  2. {  
  3.     var res = 0;  
  4.     for (var i = 0; i < BlockCount; i++)  
  5.     {  
  6.         var block = GetBlock(i);  
  7.         res += ConvertBlockToOctal(block) * (int)Math.Pow(10, BlockCount - i - 1);  
  8.     }  
  9.     return res;  
  10. }  

Conclusion

 
Often, a string is a jack-of-all-trades type that does not represent all the necessary constraints that actual type in question possesses. As one of the possible solutions in this article, I propose crafting dedicated types and using the Parse method in order to construct specific types from the general input.


Similar Articles