First, we will illustrate the importance of modularization to programming using actual examples. The example is the dynamic network forum we are currently using.
1. The color scheme of a forum is the basis for maintaining the vitality of a Forum. A good color scheme means that a forum is half successful. however, I think that the color of the Forum is a waste of resources, and background operations are also very troublesome. first, it stores the definition parts of CSS to the database and calls them when the page is opened, as shown in figure
--------------------------
<%
'The Open Database code is omitted
Dim bgcolor1
Bgcolor1 = RS ("bgcolor1 ")
Rs. Close
Set rs = nothing
%>
<Table> <tr> <TD bgcolor = '<% = bgcolor1 %>'> </TD> </tr> </table>
--------------------------
Using this method, we can still talk about it when there is little content in the database to query, but the CSS definition of a page contains dozens of items and each page needs to be called, this code is especially idiotic. try the following method:
--------------------------
<%
'1. the CSS file is a page style file. we store the file path in the database.
Dim style1 = RS ("style ")
%>
<HTML> <LINK rel = "stylesheet" type = "text/CSS" href = "<% = style1 %>">
</Head>
---------------------------
This method is much faster than the above method, and greatly reduces resources, which is one of the advantages of modularity.
Some of my friends may not understand what modularization is. I am still in the old habit of using an instance to explain it to you. this example is to write a simple message book. Of course, in fact, such a small program does not need to be modularized. I just want to explain it.
We require that this message book be registered before leaving a message. Only the administrator can reply. in general, we will write a separate login page, and then automatically jump to the login page when the user is not registered to enter the message page, or use IFRAME to include the page. of course, we also need to write a registration page and a reply page.
After modularization, we only need two files, one display page/GB. asp, And the other module page/mod. asp.
The idea is as follows:
1. messages will be displayed, such as logon, registration, and writing of messages.
-----------
<%
'Mod. asp login module
Sub gb_login ()
%>
<Form action = 'gb. asp? Action = login 'method = post>
<Input type = text name = 'loginname' size = 12>
<Input type = password name = 'loginpass' size = 12>
<Input type = submit value = 'login '>
</Form>
<%
End sub
'Login Processing Module
Sub gb_logincheck ()
Dim A1, A2
A1 = request. Form ("loginname ")
A2 = request. Form ("loginpass ")
'Open database statement omitted
If a2 = RS ("pass") then
Session ("name") = A1
End if
Response. Redirect "GB. asp"
End sub
%>
Then we can call it on the homepage.
<%
Dim action
Action = request ("action ")
If action = "login" then
Gb_logincheck ()
Else
If SESSION ("name") = "" then
Gb_login ()
End if
End if
%>
----------------------
Of course, the above instance is not completely complete. We can define all the functions as a sub-process and put them in a single file.
Through the above example, we should understand what is modular
A module refers to a piece of code in a program, which can implement a function in the program and run independently or semi-independently. A non-fatal error in the statement segment does not paralyze the entire program, but does make the program lose some functionality.
The module can be called repeatedly. According to the particularity of the program, the module can contain other modules and run together with other modules.
In large-scale programming, the use of modules is inevitable. take the Internet as an example. You can look at the source code of the Internet. It also makes a lot of use of sub-processes and plays a great role in the entire program running. however, there are many errors and defects.
For example
1. The database connection is enabled from start to end in the online forum, and the Code for closing the database connection cannot be found when the database is opened multiple times in many places, this is why many Internet users say the Forum is slow.
By analyzing its source code, we can find that the code for opening the database is put in a file, and then the code for shutting down the database is put in a function. I think it's strange, why not call the database as a sub-process or function?
It is a pity that a good method is used in half of it !! Browsing the source code of many web programs is also the result of this kind of phenomenon.
2. There are countless errors in many modules. the most obvious thing is the number of online users. For example, we often see hundreds of online visitors in forums. in fact, we all know that people who come to our forum will never log on! I don't want to talk too much about this error, because if I write it, I will definitely write it in another way.
There are a lot of excellent modular models, in the forum program will be modular application of the exception of success when pushing phpBB, you can go to his official website to see [http://www.phpbb.com]
Of course, we do not need to modularize all the code of a program, which is counterproductive. What code segments should be modularized? I think the following conditions should be met:
1. A large number of code segments that are repeatedly used on multiple pages or programs
2 code segment to be further developed
3. key functions and core content in the program
4. Code segment that can expand third-party plug-ins
Okay, it should come to an end. At last, we will summarize the definition and features of modularization below.
In program design, especially in Web programming, modularization has the following benefits:
1. Greatly reduce code lines
2. The program structure is clear, and the target range is greatly reduced when an error is found.
3. When a program needs to add new functions or modify a function, all we need to do is write a new module or modify a module, without having to perform major procedures on the entire program. for this reason, modularization also creates a good interface environment for the development of third-party extended functions
Of course, modularization also has defects:
1. When designing a module, you must consider whether there is a conflict between modules. Sometimes many unexpected errors may occur.
The structure of the two modules must be clear, and the interaction between some modules leads to the ordering of unordered codes in programming.
However, the advantage of modularity is obvious. Does a programmer who wants to write the most powerful web program with the least Code have reason to reject it?