Skip to content

Commit 2f3cb44

Browse files
committed
Refactor Luhn algorithm digit processing logic
Introduced a `DigitProcessor` delegate to simplify digit processing logic. Separated computation and validation steps into dedicated helper methods for better readability and maintainability. Renamed `ToDigit` to `ToUnsignedIntegerDigit` for improved clarity. Resolves: No entry
1 parent 5634c2d commit 2f3cb44

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

src/LuhnAlgorithm.cs

+46-13
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ internal static class LuhnAlgorithm
5252
/// </summary>
5353
private const int AsciiCodeForZero = 48;
5454

55+
/// <summary>
56+
/// Represents a delegate used for processing digits within the Luhn algorithm context.
57+
/// </summary>
58+
/// <param name="digit">The original value of the digit before doubling.</param>
59+
/// <param name="doubleDigit">The value of the digit after being doubled.</param>
60+
/// <param name="index">The index of the digit in the sequence, starting from the rightmost end.</param>
61+
/// <returns>
62+
/// A <see cref="System.UInt32"/> representing the processed result for the current digit.
63+
/// </returns>
64+
private delegate uint DigitProcessor(uint digit, uint doubleDigit, int index);
65+
5566
/// <summary>
5667
/// Doubles every second digit in the given sequence based on the context of validation or computation.
5768
/// </summary>
@@ -70,25 +81,47 @@ internal static uint DoubleEverySecondDigit(this string number, bool forValidati
7081
#endif
7182
{
7283
uint sum = 0;
84+
DigitProcessor digitProcessor = forValidation ? ProcessDigitForValidation : ProcessDigitForComputation;
7385
for (int index = 0; index < number.Length; index++)
7486
{
75-
uint digit = number[number.Length - index - 1].ToDigit();
87+
uint digit = number[number.Length - index - 1].ToUnsignedIntegerDigit();
7688
uint doubleDigit = digit << 1;
77-
if (forValidation)
78-
{
79-
doubleDigit = doubleDigit > 9 ? doubleDigit - 9 : doubleDigit;
80-
sum += index.IsEven() ? digit : doubleDigit;
81-
}
82-
else
83-
{
84-
doubleDigit = doubleDigit / Modulus + doubleDigit % Modulus;
85-
sum += !index.IsEven() ? digit : doubleDigit;
86-
}
89+
sum += digitProcessor(digit, doubleDigit, index);
8790
}
8891

8992
return sum % Modulus;
9093
}
9194

95+
/// <summary>
96+
/// Processes a digit during the computation phase of the Luhn algorithm.
97+
/// Depending on the index, either the doubled digit's sum of digits or the original digit is returned.
98+
/// </summary>
99+
/// <param name="digit">The current digit being processed from the numeric input.</param>
100+
/// <param name="doubleDigit">The value of the current digit multiplied by 2.</param>
101+
/// <param name="index">The position of the digit in the sequence, considering rightmost digit as index 0.</param>
102+
/// <returns>
103+
/// A <see cref="System.UInt32"/> value representing the transformed digit based on its position.
104+
/// </returns>
105+
private static uint ProcessDigitForComputation(uint digit, uint doubleDigit, int index)
106+
{
107+
return !index.IsEven() ? digit : doubleDigit / Modulus + doubleDigit % Modulus;
108+
}
109+
110+
/// <summary>
111+
/// Processes a digit for validation based on the rules defined within the Luhn algorithm.
112+
/// </summary>
113+
/// <param name="digit">The numeric value of the digit being processed.</param>
114+
/// <param name="doubleDigit">The value of the digit multiplied by two.</param>
115+
/// <param name="index">The position index of the digit in the sequence, starting from zero.</param>
116+
/// <returns>
117+
/// A <see cref="System.UInt32"/> value representing the result of the processing based on the digit's index.
118+
/// </returns>
119+
private static uint ProcessDigitForValidation(uint digit, uint doubleDigit, int index)
120+
{
121+
doubleDigit = doubleDigit > 9 ? doubleDigit - 9 : doubleDigit;
122+
return index.IsEven() ? digit : doubleDigit;
123+
}
124+
92125
/// <summary>
93126
/// Checks whether the <paramref name="value"/> is even.
94127
/// </summary>
@@ -98,9 +131,9 @@ internal static uint DoubleEverySecondDigit(this string number, bool forValidati
98131
private static bool IsEven(this int value) => value % 2 == 0;
99132

100133
/// <summary>
101-
/// Converts a character representing a numeric digit to its integer value.
134+
/// Converts a character representing a numeric digit to its unsigned integer value.
102135
/// </summary>
103136
/// <param name="character">The character to convert. Must represent a numeric digit.</param>
104137
/// <returns>The numeric value of the character as an unsigned integer.</returns>
105-
private static uint ToDigit(this char character) => (uint)character - AsciiCodeForZero;
138+
private static uint ToUnsignedIntegerDigit(this char character) => (uint)character - AsciiCodeForZero;
106139
}

0 commit comments

Comments
 (0)