coding help

Discussion in 'Programming' started by jennifer_48219, May 30, 2008.

  1. #1
    Just want to check my code to see if I wrote them right. If wrong please correct me

    Create a cfif block that checks for the condition:
    IsDefined("url.productid") AND NOT IsDefined("form.productid") --- so I wrote
    Help with Code Tags
    
    <cfif IsDefined("url.productid") AND NEQ IsDefined("form.productid")>
    Code (markup):
    Create a ciff block that checks for the condition:
    if url.action is defined and form.action is not defined
    Inside the block, assign url.action to form.action ----so I wrote
    Help with Code Tags
    
    <cfif IsDefined(url.action) AND IsDefined(form.action) NEQ "1">
    
    Code (markup):
    Create a cfif block that checks for getCartItems.quantity is greater than 0.
    If the quantity is greater than 0, assign getCartItems.quantity + 1 to variables.quantity .
    Otherwise, assign 1 to variables.quantity ---- so I wrote
    Help with Code Tags
    
    <cfif getCartItems.quantity GT 0 AND getCartItems.quantity + 1 to variables.quantity>
    Code (markup):

     
    jennifer_48219, May 30, 2008 IP
  2. dshuck

    dshuck Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    StructKeyExists is the preferred use for testing existence of variables, due to increased performance over IsDefined. Here is how I would write your examples:

    <cfif StructKeyExists(url,"ProductId") AND NOT StructKeyExists(form,"ProductId")>
    Code (markup):
    <cfif StructKeyExists(url,"action") AND NOT StructKeyExists(form,"action")>
    Code (markup):
    and lastly....

    
    <!--- remember that the "is greater than 0" is implied in the condition below --->
    <cfset variables.Quantity = 1 />
    <cfif getCartItems.quantity>
         <cfset variables.Quantity = variables.Quantity + getCartItems.quantity />
    <cfelse>     
    
    Code (markup):
     
    dshuck, May 31, 2008 IP
  3. websiteideas

    websiteideas Well-Known Member

    Messages:
    1,406
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Is not isDefined almost just as fast when you pass in the scope of the var? I thought isDefined was only slow when you did not pass in the scope.
     
    websiteideas, Jun 6, 2008 IP
  4. dshuck

    dshuck Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    A friend and I recently decompiled some of the generated Java classes that are responsible for this work. We were quite surprised at how much more efficient StructKeyExists() was. In fact - and this one really blew us away - while, I (and everyone else!) has always preached that explicitly scoping variables is not only good from an organizational standpoint, but also in regards to performance, we found this to only be half true. If you get a chance, clear out the generated class files in the j2ee server you are working in, and run something like <cfset variables.blah = "blah"> then, <cfif variables.blah EQ "foo"><!--- nothing ---></cfif> and compare what is generated by not doing "variables.". After looking at several of these in depth, I think you will be surprised at how the underneath processes split and take different paths, not always doing what you might think.

    While you are at it, take a look at StructKeyExists vs. IsDefined. Don't just take my word for it!
     
    dshuck, Jun 8, 2008 IP