Modular Thinking in Network programming

Source: Internet
Author: User
Tags definition end functions modify new features
Program | design | Network First, a practical example illustrates the importance of modularity for program writing. The example is the Dynamic Web forum we are using.

1 Forum color is a forum to maintain the vitality of the foundation, a good color match means half the success of a forum. But I think the network forum's color is and the waste of resources, while the background operation is also very troublesome. First, it stores the various definitions of the CSS in the database, and then calls when the page is opened, as
--------------------------
<%
' Open database code omitted
Dim Bgcolor1
Bgcolor1=rs ("Bgcolor1")
Rs.close
Set rs=nothing
%>
&LT;TABLE&GT;&LT;TR&GT;&LT;TD bgcolor= ' <%=bgcolor1%> ' ></td></tr></table>
--------------------------
In this way, when querying the content of the database is less than the past, but a page of CSS definition of up to dozens of items and every page to call, this code is particularly stupid. Try this method:
--------------------------
<%
' 1.CSS file is a page style file and we store its file path in the database
Dim style1=rs ("style")
%>
<link rel= "stylesheet" type= "Text/css" href= "<%=style1%>" >
---------------------------
This method is much faster than the one on the network, and greatly reduces the resources, which is the advantage of modularity
Some friends may not understand what is modular, I still old habits, with an example to everyone. The example is to write a simple message book, of course, the message of such a small program is generally not required modular, I just explain.

We require that this message must be registered to leave a message, only the administrator can reply. According to the general wording, we will write a separate landing page, and then when the user did not register into the message page automatically jump to the landing page, or with an IFRAME to include the page. Of course, we have to write a registration page, A reply page

When using modularity, we only need 2 files to handle it, a display page/gb.asp, a module page/mod.asp
The idea is this:
1 will display message login register write message and so on function module words, such as login function
-----------
<%
' MOD. Login module in ASP
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= ' landing ' >
</form>
<%
End Sub
' Process the landing 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 this on the main page of the message
<%
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 example is still very incomplete, we can use this idea to define all functions as a child process unified in a file, when needed in the call

Through the above example, we should understand what is modular
A module is a piece of code in a program that can implement a function in a program and can run independently or partially. A non-fatal error in the statement section does not cause the entire program to be paralyzed, except that it loses some functionality.
Modules can be repeated calls, according to the special procedures, modules can contain other modules, combined with other modules to run

In large program writing, the use of modular words is unavoidable. Or to move the net as an example, we can look at the source of the dynamic network, it also used a lot of sub-process, for the entire program to run a great role. But there are a lot of mistakes and flaws.
Like what
1 Dynamic Network Forum from beginning to end the database connection is open, and many places open the database more than once, but can not find the code to close the database connection, which is a lot of friends with the east to say that the reason for the slow forum is
Analyzing its source, we can find that he put the code to open the database in a file, and then put the code to close the database in a function. I think it's strange, why not open the database as a subroutine or function call?
A good way to use half of it is really a pity!! Browse many Web program source code, also most exist this kind of Longtou phenomenon.

2 Many of the modules have countless errors. The most obvious is the number of online, hehe, like our forum often appear online visitors hundreds of people. And in fact, we all know that the people who come to our forum will never be landing! I don't want to talk too much about this mistake, because if I write it, I'll definitely write it in a different way.

Modular excellent model There are many, in the Forum program on the modular use of the unusually successful when push phpBB, you can go to his official website to see [http://www.phpbb.com]

Of course, there is no need to modularize all the code of a program, which is counterproductive. What code snippet should be modular, I think it should have the following conditions
1 A large number of code snippets that are reused in multiple pages or programs
2 Code snippets to be further developed
3 key functions in the program, core content
4 can extend the code snippet of the third party plug-in

OK, that should be the end of it, finally, the following modular definition and characteristics are summarized

In programming, especially in Web programming, we use modularity for the following benefits:
1 greatly reduce the line of code
2 makes the structure of the program clear, in the search for errors in the scope of the target greatly reduced
3 when the program needs to add new features or modify a feature, all we have to do is write a new module or modify a module without having to perform a major operation on the entire program. For this reason, modularity also creates a good interface environment that facilitates the development of third-party extension functions

Of course, modularity also has drawbacks:
1 The module must be designed to consider whether there is a conflict between the modules, and sometimes a lot of unexpected errors occur
2 Modules must maintain a clear structure, some of the interaction between the modules resulting in the programming of the original unordered code must be sorted

However, the advantages of modularity are obvious, and for a programmer who wants to write the most powerful Web program with minimal code, is there any reason to reject it?



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.