• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

alternative way of long cfif

New Here ,
Jun 06, 2007 Jun 06, 2007

Copy link to clipboard

Copied

anyone can help me to make this shorter or effective way? thank you!
TOPICS
Advanced techniques

Views

720

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 06, 2007 Jun 06, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 06, 2007 Jun 06, 2007

Copy link to clipboard

Copied

> 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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 06, 2007 Jun 06, 2007

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 07, 2007 Jun 07, 2007

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 08, 2007 Jun 08, 2007

Copy link to clipboard

Copied

Look into using CFCASE instead of cfifs...much easier to code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 08, 2007 Jun 08, 2007

Copy link to clipboard

Copied

> 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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 08, 2007 Jun 08, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 10, 2007 Jun 10, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 11, 2007 Jun 11, 2007

Copy link to clipboard

Copied

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:

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 11, 2007 Jun 11, 2007

Copy link to clipboard

Copied

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:

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

LATEST
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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation