0
\$\begingroup\$

I would like to ask a code review for the class below.

I am asking this because VS showed a quite low Maintainability Index of 54 and I am wondering if it is good code.

I know I could have extracted some parts of the long method to smaller private methods (or local methods but that would not help a lot I think), but I think in case of extension method classes it is better to stick with the one method - one extension concept.

What do you think? (Any feedback is highly appreciated!)

using System;

namespace GridPathFinder.Utilities
{
    public static class StringExtensions
    {
        /// <summary>
        ///     Converts the string to a 2 dimensional char array (<see cref="char[,]"/>),
        ///     finding new lines based on the <see cref="Environment.NewLine"/> value.
        /// </summary>
        /// <param name="input">The input string to convert.</param>
        /// <returns>
        ///     A <see cref="char[,]"/> where dimension 0 indicates rows, dimension 1 indicates columns.
        /// </returns>
        public static char[,] To2DCharArray(
            this string input,
            StringSplitOptions stringSplitOptions = StringSplitOptions.RemoveEmptyEntries)
        {
            return To2DCharArray(input, new string[] { Environment.NewLine }, stringSplitOptions);
        }

        /// <summary>
        ///     Converts the string to a 2 dimensional char array (<see cref="char[,]"/>),
        ///     finding new lines based on the <paramref name="separators"/> given.
        /// </summary>
        /// <param name="input">The input string to convert.</param>
        /// <param name="separators">Array of separator strings</param>
        /// <returns>
        ///     A <see cref="char[,]"/> where dimension 0 indicates rows, dimension 1 indicates columns.
        /// </returns>
        public static char[,] To2DCharArray(
            this string input,
            string[] separators,
            StringSplitOptions stringSplitOptions = StringSplitOptions.RemoveEmptyEntries)
        {
            if (input == null)
                throw new ArgumentNullException(nameof(input));
            if (input == string.Empty)
                throw new ArgumentException($"{nameof(input)} cannot be empty.");
            if (separators == null)
                throw new ArgumentNullException(nameof(separators));
            if (separators.Length == 0)
                throw new ArgumentException($"{nameof(separators)} array has to contain at least one separator string.");

            string[] inputLines = input.Split(separators, stringSplitOptions);

            int widthOfFirstLine = inputLines[0].Length;
            char[,] array = new char[inputLines.Length, widthOfFirstLine];

            for (int row = 0; row < inputLines.Length; row++)
            {
                if (inputLines[row].Length != widthOfFirstLine)
                {
                    throw new ArgumentException($"{nameof(input)}'s all rows has to be equal in width," +
                        $"but the width of row with index {row} differs from the width of first row.");
                }

                for (int col = 0; col < inputLines[row].Length; col++)
                {
                    array[row, col] = inputLines[row][col];
                }
            }

            return array;
        }
    }
}
```
\$\endgroup\$
  • \$\begingroup\$ Does it need to be a multiimensional array [,] or can it also be a jagged array [][]? \$\endgroup\$ – dfhwze May 31 at 8:24
  • \$\begingroup\$ @dfhwze Good finding, in my application it is more convenient to work with a 2D array instead. (However jagged arrays offer a tiny performance improvement over multidimensional arrays) \$\endgroup\$ – Károly Ozsvárt May 31 at 8:26
  • \$\begingroup\$ Perhaps you could use the flow described at stackoverflow.com/questions/26291609/… to convert 'inputLines' to 'array'. \$\endgroup\$ – dfhwze May 31 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.