views:

55

answers:

3

Hello,

Here is a pattern I am thinking about in ASP :

Imagine you have a file main.asp that contains

<!--#include file="1.asp"-->

code of 1.asp

   ...my code...

Do you think it is valid to refactor this as

main.asp

Dim defined_1_asp = false
<!--#include file="1.asp"-->

1.asp

if (not defined_1_asp) then
    defined_1_asp = true
    ...my code...
end if

This way I could refactor all my SSI includes while making sure they are executed only once. Of course, the content of the includes would be included, but execution would be protected by the if.

I read that the if statement does not have its own scope in classic ASP so it seems to me that the behavior of the code would not be impacted by the refactoring.

Would I hit a bottleneck if the same files are SSI-included several times ?

Thanks a lot for your help,

Jerome Wagner

+1  A: 

AFAIK You cannot include code more than once (you will get errors with duplicate identifiers).

I create classes , creating them if and when needed.

Edelcom
yes you are right, i did the test and the _if_ statement does not circumvent the redefinition error ! Thanks.
Jerome WAGNER
unfortunately the redefinition problem is not avoided by classes. If i include 2 times a class definition, the redefinition bug appears
Jerome WAGNER
Agree with creating classes. Sadly I never used to do this enough when I started out on classic asp and it still hurts to this day.
Castrohenge
A: 

If you need to include any piece of code more than once, you should make it a Sub or a Function. In my experience, SSIs are used to store those Subs and Functions. So what you could do is create a Sub in 1.asp and then in main.asp do this:

<!--#include file="1.asp"-->

Dim defined_1_asp = false

Call MySub

With the Sub being something like

If Not defined_1_asp Then
  ...code here...
End Sub
jorrit787
the whole problem is that because of code spaguetti in legacy code, 1.asp is included 2 times in the inclusion hierarchy. This creates a conflict (cannot redefine variable, class or function)
Jerome WAGNER
A: 

The SSI include is done before any rendering of the page is done. That means that 1.asp is included two times, giving you problems with declarations of variables and all kinds of error issues. That should be avoided at all cost. What you can do instead (and it is a much better design and programming practice) is to put the code in 1.asp into a Sub, then you can call the sub whenever the logic requires it. If it is a more complex issue you can create classes for the stuff in 1.asp or break it into many subs and functions. Much cleaner and better for future maintenance.

bjorsig
but that does not seem to solve the include collision if the class is included more than once in the inclusion graph. does it ?
Jerome WAGNER
No, you should not include it twice. You use the class in all appropriate places.
bjorsig