Reusing Code


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

Figure 9.11. The Home page contains basic logos and branding.


Figure 9.12. The Contact page contains the same elements as the Home page.


The pages have a lot in commonboth have the same header, the same logo, and the same copyright notice. If you were writing plain HTML, you'd have no choice but to copy all the code that creates those page components into every page you were creating.

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.

Listing 9.14. ows_header.cfm
 <!--- Name: ows_header.cfm Author: Ben Forta (ben@forta.com) Description: <cfinclude> header Created: 12/1/2004 ---> <html> <head>  <title>Orange Whip Studios</title> </head> <body> <!--- header ---> <table width="100%"> <tr> <td>  <img src="/books/2/448/1/html/2/../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> 

Listing 9.15. ows_footer.cfm
 <!--- Name: ows_footer.cfm Author: Ben Forta (ben@forta.com) Description: <cfinclude> footer Created: 12/1/2004 ---> <p> <hr> <p align="right"> <i>&copy;Orange Whip Studios</i> </p> </body> </html> 

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.

Listing 9.16. home.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"> 

Listing 9.17. contact.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 Listings 9.16 and 9.17. Each listing contains two <cfinclude> statements: The first includes file ows_header.cfm (Listing 9.14), and the second includes file ows_footer.cfm (Listing 9.15). ColdFusion includes those two files and generates the output seen previously in Figures 9.11 and 9.12. The content that is unique to each page can be placed between the two <cfinclude> tags.

To see the real value of this approach, modify ows_header.cfm (change colors, text, or anything else) and then reload home.cfm and contact.cfm to see your changes automatically applied to both.

Well revisit this subject in detail in Chapter 11, "The Basics of Structured Development".




Macromedia Coldfusion MX 7 Web Application Construction Kit
Macromedia Coldfusion MX 7 Web Application Construction Kit
ISBN: 321223675
EAN: N/A
Year: 2006
Pages: 282

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net