Skip to main content
2 of 2
Added in the section on checking the number of segments, before processing them.
Jhal
  • 119
  • 2
string[] splitStrArray = SNString.Split('-');
int[] expectedLengths = { 3, 8, 3 };

if( splitStrArray.length != 3 ){
    return false;
}

for( int i = 0; i < expectedLengths; ++i ){
    int expectedLength = expectedLengths[i];
    int actualLength = splitStrArray[i].Length;

    if( expectedLength != actualLength ){
        return false;
    }
}

return true;

I prefer loops, early exits and an "innocent until proven guilty" approach.

With regards to making code simple and generic, the original code is terrible. Explicitly naming values in an array like that, then doing the same operation to them all is bad practise. Reasons being:

  • It's awkward to maintain
  • It's not scalable
  • It just causes code to become longer than needed

It might be overkill in this situation, but generalised code that don't have names like "part1, part2, part3" is just way easier for me to read and get to grips with.

Hope this helps!

Jhal
  • 119
  • 2