Looping through Cfquery or Struct?

I have a query that fetches some data. I want to display this data given some conditions in different div tags. Now my question is: I do this by repeating the request once and getting the data in three different structures and using those structures when displaying. Is this a good approach, or is it a rirht approach to loop through the query every time in each div to check the condition?

     <tr >
<td >
  features:
 </td>
 <td >
    <cfloop query="getAttributes">
      <cfif getAttributes.type_id EQ 1>
        #getAttributes.seat#<br>
      </cfif>
    </cfloop>
 </td>
</tr>
<tr>
 <td >
  Disclosures:
 </td>
 <td >
    <cfloop query="getAttributes">
   <cfif getAttributes.type_id EQ 2>
          #getTicketAttributes.seat#<br>
   </cfif>
  </cfloop>
  </td>
 </tr> 

      

Or I can use the approach below

seatStruct 
disclosureStruct 
<cfloop query="getAttributes">  
<cfif getAttributes.type_id EQ 1> 
Insert seatStruct 
<cfelseif getAttributes.type_id EQ 2> 
insert disclosureStruct 
</cfif> 
Now use these structs to display

      

+2


a source to share


2 answers


I think you will have to modify your question a little, add an example.

Fewer loops is always the best approach :) Less conversion if not needed is the best approach :)



If your data is in one request, then there is no need to quote more than once, I guess ...

+4


a source


The best approach will always depend on your specific problem.

While fewer iteration loops will always lead to better performance, it is sometimes acceptable to sacrifice some performance to improve readability.

Maintenance costs tend to be the most expensive piece of software, so it's worth making your code readable.



In this particular case:

  • If the query result is getAttributes

    unusually large (for example, more than 10,000 rows), or this page is loaded unusually often (for example, more than once / sec), there probably won't be a noticeable difference in how many times you iterate over it.

  • Both options will take exactly the same amount of time, one way or another: The first option is repeated twice above the request. The second option is repeated once to fill the two structures, then your displayed code will go through each of the generated structures (which collectively have the same number of elements as the query has rows), resulting in the same exact number of full iterations (equivalent getAttributes.recordcount*2

    ).

  • Code that breaks up query results into different structures is somewhat unusual, which reduces readability and increases maintenance costs. Since it does not actually improve performance, it is completely counterproductive and should not be used.

0


a source







All Articles