Reusing Code with ASP Include Files and Subroutines
By Nannette Thacker - 05/01/1999

I call it "Frankenstein" code when an author cuts and pastes the same snippet of code over and over throughout an application. What a maintenance nightmare!!!

Later you may have to update 50 snippets of code and imagine what happens when you miss updating a few!

Reuse Code - Use Includes or Create Subroutines

Writing subroutines or using an include file is easy to do. Then later on, when updating is necessary, only one piece of code must be edited! You may have a function which converts a date string into something like this: "JAN-24-99." Place the code in a subroutine, in an include file, so that it can be called on each page that uses it:

This would be in the "include" file "DateToText.asp":

<%
   Function DateToText(convertdate)
      DateToText = ' remainder of code goes here
   End Function
%>

Call the DateToText function in the files which use it:

<!--#include virtual="/include/DateToText.asp"-->

I have edited a major application where the header, logo, and links on every page were identical, yet they were cut and pasted into every page! What a horror trying to ensure that changes were made consistently, and tested, on every single page!

If the developer had used forethought, this information would have been placed into an include file, which would be called at the top of every page. Thus, any changes would be made once, and testing only a few of the pages would have been ample.

(What if the header is "similar" on every page, but has a few differences? See my article on Using Templates With Your ASP Development for ideas.)

Include File Naming Conventions

Include files can be named with the "inc" extension. However, for two reasons, I prefer using the "asp" extension:

  1. Color-Coded: The "asp" extension allows color-coded editing within Visual Interdev. That alone is reason enough for me. You can easily spot problems while coding!
  2. Security Issues: Functions written in files using the "inc" extension can be opened in a browser window and viewed. Functions written in files using an "asp" extension are not displayed to the viewer, thus keeping the source code hidden from prying eyes.

I prefer keeping "include" files used throughout the entire project in an "include" subdirectory. But if an include file is unique to a given module, I prefer keeping the include file in that module's directory.