Article Image
Article Image
read

Or When removing code duplication can go too far

As a part of a large test suite, I was implementing I had a test case to check if a string was an email address or not. The method that was being tested simply returned a boolean, true if the string in exam was a valid email address, false otherwise. The test consisted of checking 3 different candidate email addresses and checked if the return value from the function ‘isValidEmail’ was consistent.

The method was something like this (simplified for ease of reading):

NSAssertEqual("email@mail.com".isValidEmail(), true)

NSAssertEqual("email@mail,commmm".isValidEmail(), false)

The interviewee complained that it could have been done with just one case using a regular expression instead of in 3 lines. While I agreed that on just a programming level it would have been better, ease of reading was highly compromised though. This was in a test so readability of the test was paramount for the future. Someone new to the project could just come in an in a few seconds realize what the test was doing.

If you just skim the file you immediately understand what’s going on. They did not agree and thought that the regex was to be used.

Blog Logo

Valentino Urbano


Published

Image

Valentino Urbano

iOS Developer, Swift, Writer, Husband

Back to Overview