[thelist] Friday Freebie

jeff jeff at members.evolt.org
Wed Dec 27 00:51:23 CST 2000


raymond,

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: From: Raymond Camden
:
: I understand it may be just a personal feeling, but
: why do you feel IsDefined() makes code
: unreadable?
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

let's say for whatever reason you've chosen to define a variable whose name
is based partially on a form field value.  normally something like this will
send up red flags about it being necessary to approach the problem from
another angle.  however, i've seen this sort of thing all too commonly.

<cfif isDefined('form.new_#form.category#')>

wtf is that?  how am i supposed to know what variable names, let alone
variable values are floating around?  if something breaks, it's very
difficult to track down the problem.

another problem i have with it is that users will often prefer to not define
the variables their template will be using (which gives me an idea of what
it's expecting when it runs) and use isDefined() checks everywhere.  then,
they ask silly questions like "how do i destroy a variable so the block of
code inside of my <cfif NOT isDefined('blah')> conditional will run?".
that's just sloppy.  it's so much easier for everyone if you define and
check - define and check - define and check.  you (or another programmer)
can come back later and know exactly what the purpose of the variables are
and their expected values or data types.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: In almost all cases, it's not the function that makes
: code unreadable it's the USE of the function, or the
: structure of the code.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

agreed - which is exactly what i just got done saying this myself (only in a
much more long-winded fashion).  however, i have yet to find an instance
where the use of isDefined() has lent to code readability over my preferred
method of "define and check".  you can often solve alot of code structure
problems and function use problems that affect readability by simply showing
other ways to solve the problem.  so, that's why i think i prefer to just
say "isdefined is evil" and move on to solving the problems in a more
manageable fashion.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: The only exception to this case, imho, is the IIF()
: function, which you shouldn't use both for
: readability issues as well as speed reasons.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

agreed.

all the soap-boxing aside, i still find myself using the IIf() function too
much cause it's easy for me.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Also, why do you say there is no need for it? How
: would you get around a situation like this:
:
: details.cfm expects a URL variable, ID, passed to it.
: This ID var is used in a sql statement.
:
: Sure, you can CFPARAM it to some default value, but
: normally you would probably rather just send them back
: to index.cfm or the previous page.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

imo, paraming to a default value is the correct way to do it.  this is how i
would approach it.  i'm going to assume that it's a numeric that's going to
be passed - hence the default value of 0.

<cfparam name="url.id" default="0">

<cfif NOT Val(url.id)>

  <cflocation url="#cgi.script_name#" addtoken="no">

<cfelse>

  <cfquery name="blahdetails" datasource="#dsn#">
    SELECT blah
    FROM table_name
    WHERE id = #Val(url.id)#
  </cfquery>

</cfif>

this one was a rather easy instance of where isDefined() is not necessary.
i might be able to think of more difficult ones, but i've never been able to
think of one where the use of isDefined() couldn't be avoided.

again, just my honest opinion from doing this for a long time.

thanks,

.jeff

name://jeff.howden
game://web.development
http://www.evolt.org/
mailto:jeff at members.evolt.org





More information about the thelist mailing list