1

I am new to Coldfusion and trying to build a string something like

<cfif qRoute.a IS NOT "">
      <cfset str= qRoute.a>
      <cfif qRoute.b IS NOT "">
        <cfset str= str& " /  "& qRoute.b>
      </cfif>  
      <cfif qRoute.c IS NOT "" >
        <cfset str= str& " /  "& qRoute.c>
      </cfif> 
    </cfif> 

But it seems to me to be very basic technique. Is there a better way to write the code.

user3733648
  • 1,323
  • 1
  • 10
  • 25
  • 1
    perhaps better asked over at http://codereview.stackexchange.com/ – duncan Jun 15 '15 at 15:28
  • 4
    @duncan this seems to be example code which is off-topic on Code Review – Simon Forsberg Jun 15 '15 at 15:42
  • 1
    He posted it anyway. Will be closed soon. – Mast Jun 15 '15 at 15:48
  • 2
    @Leigh no it's not, ...[as other CR regulars have already mentioned](http://stackoverflow.com/questions/30848993/code-optimizing-in-coldfusoin-as-string-are-immutable-in-coldfusion#comment49742384_30848993). Boiled-down code stripped of all context is good for Stack Overflow and **off-topic** on Code Review. – Mathieu Guindon Jun 15 '15 at 16:17
  • @Mat'sMug - Did you mean to post a different link? Personally it sounds more like review/optimization to me (which I thought was the purpose of CodeReview), but I am more familiar with SO. If CR has a different policy, feel free to shoot me a link. – Leigh Jun 15 '15 at 16:21
  • 2
    @Leigh I meant to (and did) link to Simon's comment. Indeed, CR is all about reviewing and optimizing code - and to do that we need the context. Stub code, hypothetical code, and code not yet written is off-topic - it's all in the site's [help center](http://www.codereview.stackexchange.com/help/on-topic) ;-) – Mathieu Guindon Jun 15 '15 at 16:27
  • The CR version of this question [here](http://codereview.stackexchange.com/q/93653/52915). Currently on-hold. – Mast Jun 15 '15 at 16:29
  • 2
    It's old data, but this [answer](http://stackoverflow.com/a/6254490/923833) compares 4 ways of preforming string concatenation in CF 7 – Twillen Jun 15 '15 at 16:36
  • (Edit) @Mat'sMug - Fair enough about the help. I am rushing out the door and skimmed it, but mistakenly thought it was just a repeat of SO's. RE: *and to do that we need the context.* That is true of S.O. as well ;-) Granted the question itself could be improved and the asker could elaborate a bit, but is obviously real code, and not about a code error. So it still seems "on topic" for CR. I am heading out, but will take a look later after my meetings. – Leigh Jun 15 '15 at 16:39
  • 3
    @Leigh For Code Review, we would like to know what `qRoute.a`, `qRoute.b`, and `qRoute.c` is. Those identifiers make us believe that it might as well be stored in an array, such as `qRoute.data[0..2]` where 0 = a, 1 = b, 2 = c. As it is unclear whether or not such a suggestion would be valid or not, as the real identifiers might be `qRoute.speed`, `qRoute.distance` and `qRoute.power` (which is clearly not something you would want to change to an array) this question is considered 'example code'. – Simon Forsberg Jun 15 '15 at 16:57
  • @SimonAndréForsberg - I understand. However, ignoring SE terminology for a moment, my comments are about whether the overall topic is suitable for a particular site. SO is generally about specific code problems or errors to be resolved (which is not the case here), not "how can I optimize this" or "is x better than y". Those questions fall into the area of CodeReview. Now the question may still need improvement, but that is a totally different issue. Just because it may be incomplete does not automatically mean it is suitable for S.O. If it needs improvement it should be marked as such. – Leigh Jun 15 '15 at 17:34
  • @Leigh It is not suitable for SO indeed, I just wanted to point out what it was missing to become suitable for CR. If you want to discuss more about this, I recommend you come visit CR regulars in [The 2nd Monitor](http://chat.stackexchange.com/rooms/8595/the-2nd-monitor) – Simon Forsberg Jun 15 '15 at 17:51
  • @Simon André Forsberg - Gotcha. My comments were mostly in response to Mat's Mug anyway. We were on different wavelengths. M – Leigh Jun 16 '15 at 17:57
  • I'm glad the ppl here are saying "put this on Code Review", and the people on Code Review are saying "put this on Stack Overflow". I continue to be astounded by the love for bureaucracy trumping the will to just *help people* on Stack Exchange. – Adam Cameron Jun 17 '15 at 08:06
  • 1
    @AdamCameron It's off-topic for CodeReview. SO accepts example code, CR doesn't. If the OP wants to be helped, let him write some actual code which is part of an actual project and let that be reviewed on CR. Problem solved. – Mast Jun 17 '15 at 08:08

1 Answers1

1

This would perhaps be an approach. it's a complete stand-alone repro, but the bit you want to look at is the listAppend() stuff:

<cfscript>
qRoute = queryNew("");
queryAddColumn(qRoute, "a", "varchar", ["","a2","a3","a4"]);
queryAddColumn(qRoute, "b", "varchar", ["","","b3","b4"]);
queryAddColumn(qRoute, "c", "varchar", ["","","","c4"]);
</cfscript>

<cfloop query="qRoute">
    <cfif not len(qRoute.a)>
        <cfcontinue>
    </cfif>
    <cfset str = "">
    <cfset str = listAppend(str, qRoute.a)>
    <cfset str = listAppend(str, qRoute.b)>
    <cfset str = listAppend(str, qRoute.c)>
    <cfset str = listChangeDelims(str, "/")>
    <cfoutput>[#str#]<br></cfoutput>
</cfloop>

On ColdFusion 9, this outputs:

[a2]
[a3/b3]
[a4/b4/c4]

Is that more what you're after?

The code for more recent versions of CFML would be much nicer, but you're kinda hamstrung by using an obsolete version of CF (which I'm sure is outwit your control, but it irks me to have to write such clumsy code)

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78