Denilson was doing some appliance shopping, and upon submitting a form, it errored out. Denilson was sure the form was correct, and did what any of us would do: pop open the developer console and check the code.

The console dumped out a huge number of errors. And to no one's shock, the code had all sorts of home-made validation methods, like validateNumber. And, much to my surprise, they found new ways to make the simple task of validating a number horrible.

function validateNumber(value) 
      var digit, tam, dif;
   for (i=0;i<value.length;i++)
          digit = value.substring(i,i+1);
           /* Advanced Programming Resources -> Must be improved... */
         if(digit != 0)
         if(digit != 1)
         if(digit != 2)
         if(digit != 3)
         if(digit != 4)
         if(digit != 5)
         if(digit != 6)
         if(digit != 7)
         if(digit != 8)
         if(digit != 9)
         if(digit != ".")
               if(digit != "-")
                     return 2;
       if(value == "")
             return 1;

At it's core, this is a pretty basic "verify the string doesn't contain non-numeric characters for our locale", one character at a time. But there are a few special twists on this one, like the pile of nested if's used to accomplish this. Then, of course, there's the cryptic return codes: 1 if the string is empty, 2 if it contains invalid characters, and… no value at all if it's a "valid" number?

But, of course, the real star of this block in the unusually honest comment: this code "must be improved", presumably by "advanced programming resources". It's an unusual cry for help, but it is clearly a cry for help.

[Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!