Reusing Code with ASP Include Files and Subroutines

來源:互聯網
上載者:User
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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.