DEV Community

Discussion on: How Simple is too Simple to Test?

Collapse
 
scotthannen profile image
Scott Hannen

I don't know the language. What happens if you compare the first category to the second category but there's only one category? Will it get to the second check which checks to see if there are two?

Collapse
 
bosepchuk profile image
Blaine Osepchuk • Edited

Good question! I actually had to run some code to make sure it behaves as I expected.

It will get to the check for the number of elements in the collection unless the first element in the array is null. If that happens the first "if" returns true and you'll get an exception, which is what we want but the error message will be deceptive. That's a real edge case though.

Collapse
 
scotthannen profile image
Scott Hannen

I was hesitant to mention it because I don't details of the language. I was just curious because I didn't know if you could compare the first and second elements without an exception if there were fewer than two elements. In .NET the check itself would throw an exception, so I'd check the number of elements first.

Thread Thread
 
bosepchuk profile image
Blaine Osepchuk • Edited

No worries. We're just talking. Bring up anything you like.

I swapped the if statements this morning in the actual code (not updated in the blog post). So the size check is now before the equality check.

If you turn up the warnings enough in PHP you'll get a notice about accessing a nonexistent array element but it won't throw an exception on its own.