Following the colorful
parlance for AntiPatterns, the erroneous example on page 1 might be
called the “Out of Sight Out of Mind” AntiPattern. Except that
AntiPatterns typically describe a problem that is more systemic,
rather than one that can be so readily rectified by correcting one
line of code. For instance, failure -- or refusal -- to use a
language feature such as Regular Expressions.
Better yet exemplifying a bona fide
AntiPattern in a Javascript form validation context is what I call
the “All Web Development is New Development” fallacy. This
antiPattern, like the code defect debunked in the first example, is
commonplace both in books and in code libraries distributed all over
the web.
“All Web Development is
New Development” in the Javascript form validation context most often
occurs through the suggestion that, to signify that form fields are
required to be filled in, these required fields must be named in a
certain manner. Users of such code are instructed to do such things
as end the names of their form fields with
“_req”
.
So, in our first example,
“fname”
would become
“fname_req”
, and
“lname”
would become
“lname_req”
.
To begin with, though, this violates one of
Arthur J Riel's Object-Oriented Design Heuristics, particularly 3.5,
which states in part: “In applications which consist of an
object-oriented model interacting with a user interface, the model
should never be dependent on the interface.”
The HTML comprises the user
interface. Now substitute “data” for “model” above. Data is
required to specify one field as required, and another as not
required. But by appending
“_req”
to the name of an HTML form
element, the data becomes dependent on the interface.
Rules are made to be broken,
but not in this instance. Because, although requiring that form
field names have “_req” be appended to them might work in the
case of a new web application, what about an existing web
application? It almost certainly consists in part of server-side
form processing code that relies on pre-determined form field names.
Adding
“_req”
to those field names will almost certainly cause
existing server-side code to malfunction.
So, not all web development
is new development. And the above examples are certainly not how any
enterprise web form validation should be undertaken. For an example
how to better perform HTML form validation with Javascript, please
check devpapers.com later this year for upcoming articles, or my own website (see my devpaper's
author profile)
for an upcoming book.