Filter validation
For validation code, you could use a more compact but somewhat verbose pattern of failing early, failing fast.
{
// Test for: XXX-XXXXXXXX-XXX
var parts = SNString.Split( '-' );
if ( parts.Length != 3 )
return false;
if ( parts[ 0 ].Length != 3 )
return false;
if ( parts[ 1 ].Length != 8 )
return false;
if ( parts[ 2 ].Length != 3 )
return false;
return true;
}
The function will have a lot a exit points, but only a "success" result, at the end. Think the validation process as a filter, what only let pass correct data.
I may nitpick this code is only testing what:
{
// "Mask" test for: ???XXX-????????XXXXXXXX-???XXX
var hyphens = 0;
for ( int pos = 0 ; pos < SNString.Length ; pos++ )
if ( SNString[ pos ] == '-' )
hyphens++;
if ( hyphens == 2 && SNString.Length == 16 && SNString[ 3 ] == '-' && SNString[ 12 ] == '-' )
return true;
return false;
}
No string splits, no allocations, only validations in place.