All developers writeor should writecode with reuse in mind. There are many reasons why this is a good idea:
Saving time. If it's written once, don't write it again.
Easier maintenance. Make a change in one place and any code that uses it gets that change automatically.
Easier debugging. Fewer copies exist out there that will need to be fixed.
Group development. Developers can share code more easily.
Most of the code reuse in this book involves ColdFusion code, but to demonstrate basic reuse, let's look at a simple example.
Orange Whip Studios is building a Web site, slowly. Figure 9.11 shows a Home page (still being worked on), and Figure 9.12 shows a Contact page (also being worked on).
But you're using ColdFusion, and ColdFusion makes code reuse incredibly simple. The CFML <cfinclude> tag is used to include one page in another. <cfinclude> specifies the name of a file to include. At runtime, when ColdFusion encounters a <cfinclude> tag, it reads the contents of the specified file and processes it as if it were part of the same file.
To demonstrate this, look at Listings 9.14 and 9.15. The former is ows_header.cfm, and the latter is ows_footer.cfm. Between the two files, all the formatting for the Orange Whip Studios pages is present.
<!--- Name: ows_header.cfm Author: Ben Forta (ben@forta.com) Description: <cfinclude> header Created: 12/1/2004 ---> &l275> <head> <title>Orange Whip Studios</title> </head> <body> <!--- header ---> <table width="100%"> <tr> <td> <img src="../images/logo_c.gif" width="101" height="101" alt=" border="0"> </td> <td> <font face="Comic Sans MS" size="7" color="#ff8000"> Orange Whip Studios</font> </td> </tr> </table> <p>
<!--- Name: ows_footer.cfm Author: Ben Forta (ben@forta.com) Description: <cfinclude> footer Created: 12/1/2004 ---> <p> <hr> <p align="right"> <i>©Orange Whip Studios</i> </p> </body> <l>
Now that the page header and footer have been created, <cfinclude> can be used to include them in the pages. Listing 9.16 is home.cfm, and Listing 9.17 is contact.cfm.
<!--- Name: home.cfm Author: Ben Forta (ben@forta.com) Description: Demonstrate use of <cfinclude> Created: 12/1/2004 ---> <!--- Include page header ---> <cfinclude template="ows_header.cfm"> <h1>Home page goes here</h1> <!--- Include page footer ---> <cfinclude template="ows_footer.cfm">
<!--- Name: contact.cfm Author: Ben Forta (ben@forta.com) Description: Demonstrate use of <cfinclude> Created: 12/1/2004 ---> <!--- Include page header ---> <cfinclude template="ows_header.cfm"> <h1>Contact page goes here</h1> <!--- Include page footer ---> <cfinclude template="ows_footer.cfm">
As you can see, very little code exists in Chapter 11, "The Basics of Structured Development".