11 Replies Latest reply on Jun 12, 2007 7:06 AM by Stefan_K.

    alternative way of long cfif

    TESS_GEAR
      anyone can help me to make this shorter or effective way? thank you!
        • 1. Re: alternative way of long cfif
          insuractive Level 3
          A couple of recomendations from looking at your code

          1) If your code is getting a little verbose, switch over to using <cfscript>, its a little more condensed
          2) You can shortcut myVar EQ "true" and myVar EQ "false" to just:
          <cfif myVar> and <cfif NOT myVar> respectively
          3) You don't need a <cfbreak> at the end of your <cfif> block.
          4) I would recommend dropping the "variables." prefix. CF will treat unprefixed variables as being in the variables scope anyway (unless you are in a custom tag or User Defined Function)

          Hope that helps.
          • 2. Re: alternative way of long cfif
            Level 7
            > 4) I would recommend dropping the "variables." prefix. CF will treat
            > unprefixed variables as being in the variables scope anyway (unless you are in
            > a custom tag or User Defined Function)

            It's not something I've confirmed recently, but IFAIK they're still
            variables-scoped in custom tags. The variables scope being the *custom
            tag's* variables scope, not the calling template's one.

            Similarly my recollection of unscoped variables in a UDF *are* put in the
            variables scope of the calling template. Where else would they go? (NB:
            obviously if you VAR them first, they're in the magic scope-wth-no-name).


            That said: the rest of your advice is sound.

            --
            Adam
            • 3. Re: alternative way of long cfif
              Level 7
              Insuractive's advice is good. I'd add to that that TRUE and FALSE are
              tokens, and don't need quotes around them.

              My concern, though, is WTF you're doing with the REST of your code to
              necessitate this sort of construct.

              I think also it might be better to have "positive" sounding variable names:
              doDEPQuery rather than skipDEPQuery. I presume at some point you have a
              conditional around the <cfquery> tags, which will need to be negative if
              you're setting a "skip" flag. The code will be easier to read if it's
              positive.

              --
              Adam
              • 4. Re: alternative way of long cfif
                ale_lia
                Sometimes, when I have long list of variables to set as in your code, I do a loop on a list, like this

                <cfscript>
                my_skip="";
                my_do="";

                if(not variables.skipACTquery){
                my_skip="skipDEPquery";
                my_do="skipACTquery";
                }
                else if (not skipDEPquery){
                my_skip="skipDIVquery";
                my_do="skipDEPquery,skipACTquery";
                }
                else if(not variables.skipDIVquery){
                my_skip="skipBCHquery";
                my_do="skipDIVquery,skipDEPquery,skipACTquery";
                }
                else if (not variables.skipBCHquery){
                my_skip="skipSECquery";
                my_do="skipBCHquery,skipDIVquery,skipDEPquery,skipACTquery";
                }
                else if(not variables.skipSECquery){
                my_skip="skipUNTquery";
                my_do="skipSECquery,skipBCHquery,skipDIVquery,skipDEPquery,skipACTquery";
                }
                else if (not variables.skipUNTquery){
                variables.usererrormsg = "error msgs."
                }
                </cfscript>

                <cfloop list="#my_skip#" index="sdx">
                <cfset variables[sdx]=false>
                </cfloop>

                <cfloop list="#my_do#" index="ddx">
                <cfset variables[ddx]=true>
                </cfloop>


                Hope this helps
                Ale
                • 5. Re: alternative way of long cfif
                  Abinidi Level 1
                  Look into using CFCASE instead of cfifs...much easier to code.
                  • 6. Re: alternative way of long cfif
                    Level 7
                    > Look into using CFCASE instead of cfifs...much easier to code.

                    Yeah, but each of the OP's conditional expressions are different. A SWITCH
                    won't work here.

                    --
                    Adam
                    • 7. Re: alternative way of long cfif
                      Level 7
                      Abinidi wrote:
                      > Look into using CFCASE instead of cfifs...much easier to code.

                      actually there's a hidden performance hit if you use cfswitch w/non-numeric cases.
                      • 8. Re: alternative way of long cfif
                        BKBK Adobe Community Professional & MVP
                        anyone can help me to make this shorter or effective way?
                        Tess_Gear, Adam's advice to use the positive variant of a variable name is a qualitative enhancement. Apart from that, I think your code is as short and as effective as it can be. The only thing I don't understand is the presence of the cfbreak tag.

                        • 9. Re: alternative way of long cfif
                          Stefan_K. Level 1
                          I'd definitly think about another data-structure or otherwise rethink something.
                          However, your code follows a pattern, so the following code is definitly shorter, but a comment on the underlying pattern next to it is recommended:
                          • 10. Re: alternative way of long cfif
                            BKBK Adobe Community Professional & MVP
                            Stefan K. wrote:
                            However, your code follows a pattern, so the following code is definitly shorter

                            To the human eye, perhaps. However, this is how Coldfusion "sees" your code:

                            • 11. Re: alternative way of long cfif
                              Stefan_K. Level 1
                              Hey, the challenge was to either make it shorter OR more effective ;-) and mine is shorter to human eyes, just for the challenge!

                              I agree that my code is less effective, but the entire data-structure looks like an ineffective way to set up things IMHO.