QA tester was reading HTML/JS code to write a functional test of a web form, and saw:
if (form_field == empty) { ...do stuff for empty field } else if (form_field != empty) { ...do stuff for non-empty field } else { ...do stuff that will never be done }
After a couple embarrassing attempts, tester realized that they couldn’t trigger the alert strings hidden in the third block.
Things I’m wondering are if this Is this problem more or less language specific (can non-JS people learn lessons here?) and are there legitimate reasons code ended up this way?
How can I find/address the problem?
This is a language-agnostic problem. It’s quite easy to write the following in Java, for example:
The key thing to remember is that the ‘if(!x)’ is not required. Making that a simple ‘else’ would create simpler code.
Sort of. It’s standard practice for an else condition to always exist when a fall-through is needed. The problem was the programmer wasn’t thinking very clearly single his ‘(form_field != empty)’ was exactly the same as a simple ‘else’. Point it out to him and he should kick himself. If he doesn’t, question his role on the team.
Static code analysis tools can catch this sort of issue. However, I’m not aware of any for Javascript. JSLint can catch a lot of bad stuff, but not logic flow issues.