In traditional procedural programming an application’s logic is intermixed with the code that displays an application’s data. For example, it is not uncommon to see code which looks like this:
<cfquery name="getUsers" datasource="myDSN">
SELECT
*
FROM
Users
</cfquery>
<cfoutput query="getUsers">
<p>#firstName#
#lastName#</p>
</cfoutput>
There are a number of things in this example which limit this code’s reusability and maintainability. For example, consider the following questions:
What happens if the data source changes?
Answer: You need to find and replace all instances of myDSN. A typical solution to this problem is to store the DSN in a variable and to simply change the variable as needed.
What if you have another page which runs the exact same query?
Answer: The query is duplicated in both locations. If you have several locations where this query is used you have several locations to maintain. This is problematic. As an example, what if you needed to modify the query? You may need to update this query in all the locations where it was used.