How come SonarCloud doesn’t tag the following code? not even as Code Smell:
private static string PhoneNumberValidation(string phoneNumber)
{
var rgx = new Regex(@"(?i)[^0-9\s]");
var replacement = "";
var newValue = rgx.Replace(phoneNumber, replacement);
if (newValue.Length < 8 || newValue.Length > 9)
{
throw new ArgumentException("Value mismatch", nameof(phoneNumber));
}
return newValue;
}
- For starters,
anythingValidation
that returns astring
should be a smell. Also,anythingValidation
should not explicitly throw an exception. - The guy created a regex to clean the string of non-number characters but ended up using another technique to check its length. IMHO should be a smell because (based on his tests) the regex match
\d{4,5}.?\d{4}
would suffice -
Regex.Replace
is a very expensive operation, since the declaration of intention is validation (per method naming), this should be an issue.