lorry has been supporting in-house applications for several years. Even though the code was a mess and she wasn’t at the company during the early stages of development, it’s talked about. It was chaotic, poorly estimated, way over budget, and the later phases were a pain. At the last moment she pushed 10 developers into the project.
Now it’s mostly working. But it gives Rory a lot of support tickets and shows some amazing things.
A distinctive feature of many forms is that they have a “RESET” button. Press the button to clear all values on the form. That code looks like this:
for (var i = 0, l = document.getElementsByName("someId").length; i < l; i++)
if (i === 0)
document.getElementsByName("someId")[i].checked = false;
else
document.getElementsByName("someId")[i].checked = false;
for (var j = 0, l1 = document.getElementsByName("someOtherId").length; j < l1; j++)
if (j === 0)
document.getElementsByName("someOtherId")[j].checked = false;
else
document.getElementsByName("someOtherId")[j].checked = false;
for (var k = 0, l2 = document.getElementsByName("anotherId").length; k < l2; k++)
if (k === 0)
document.getElementsByName("anotherId")[k].checked = false;
else
document.getElementsByName("anotherId")[k].checked = false;
each for
I want a loop to iterate through a set of form fields. now, document.getElementsByName
returns an array of those fieldsSo all you have to do is iterate over the elements in the array.But no, I have to keep calling. document.getElementsByName
every time.
At least they didn’t make such a mistake regarding the length check and stored it in a variable. But it’s a small thing, l
, l1
and l2
It may look eerily like a number – 12
. A good font can help with this, but a quick read of the code can play tricks on your eyes.
But, of course, the real issue here is if
comment. If you are on the first element, uncheck the checkbox. If you are using other elements, also uncheck the checkboxes.
Why is there an if statement there? Why is it repeated every block? I can only guess, but my suspicion is that this is a case of copy/paste programming. In other parts of the application I wanted to change the state of all boxes except the first one.That code is close The developer just copy/pasted it and made minimal changes to get it working without trying to understand the code.
Laurie refactored this block, but there are and will always be many other similar blocks. many About support work.