Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One script wonder?

Status
Not open for further replies.

rob51383

Programmer
Jun 23, 2004
134
US
Hello great people of tek-tips.

I am faced with a design option that can be argued for the ages. One script wonder or everything layed seperate?

So, I have about 10 PHP pages on my website, one for each top level category. The largest file being 45KB and the smalest being 17KB.

I really like how it is right now because updating is simple, everything works, and my code is clean enough that any half witted PHP programer can easily see what is going on. Now for the questions:

1. Should I chop my pages into sub pages? Each page currently handles anywhere from 2-8 different top level possabilities.

2. Should I keep going with how it is?

3. Will removing the comments (from the live version only, not the copy!) make a difference on performance?

5. What should be a file size I should not exceed?

Also, what do you think?
 
I split my scripts into logical groupings. trying to keep under 20kb for each script group.

I then hang all my script groups off a single page and include() each group only as needed. usually i include one general library and one script group and one html template per page call. Although this breaks the 20kb value this is only on the server side. What gets sent to the client is very dependent on the actual page being loaded.

do not remove comments. you will curse yourself forever if you do.
 
Modular development is in my experienmce the best. I also would recommend to write object oriented code rather than function oriented code. The re-use of objects in other parts of the same application or of general objects across applications id of immense benefit.
Clean code for the functional part that just calls methods on objects makes maintenance trivially easy. The extra burden of planning the objects used is offset by the overall benefit.
If there are timing problems use the Zend Profiler or something like that to find out the bottlenecks and optimize your ocde. If that does not help, then look at the server psecs of your host.
 
I have ready many articles which proclaim to an object oriented style of PHP programming is the best, but I can only see about 3 different places where this model can be applied (and it is) but for the most part, everything is unique and can not fit into a model (or I can just not imagine a way for it to fit).

I do not see any issues with speed of the scripts, but once I get 1,000,000 (haha) visitors a month I am sure there will be places my code could shine more.

Most of my scripts size is from HTML being printed. I have 4 includes (html) per page. Javascript, CSS, Header, Footer. The rest is quite a bit of content format. For instance:

Code:
if($cat==1){
print "About 40 lines of HTML";}
elseif($cat==2){
print "About 50 lines of HTML";}
elseif($cat==3){
print "About 40 lines of HTML";}
elseif($cat==4){
print "About 60 lines of HTML";}
elseif($cat==5){
print "About 70 lines of HTML";}

As you can see, the script size can become huge, but what is printed to the user is only one of those if conditions, but all of the conditional possabilities are in one script, which makes the script size rather large.

Yes, i deffinately will shoot myself in the foot by removing comments! Comments are VERY important (everyone please note that!). But I do my comments for large sections in the following manner to allow for easy view while scrolling:

/************************************
** IF THE USER IS UPLOADING A FILE **
************************************/

Now, comments are not required, but does the 2KB they add to the file size do anything where 100,000 visitors could make a significant difference? You could always remove the comments from the live version, and save the file with the comments on your local drive.
 
Using elseif where switch could be used is a poor way of writing code, and slows down the script speed. The above example from rob51383 should be a switch statement instead.

I only use elseif when I want to check things that are different, like processing multiple form element values for a variety of conditions. Any set of conditions like that should be put in groups as much as possible and maybe separated into individual functions for processing to at least help keep it readable for humans.

Lee
 
to MJB3K

the front pages (index or whatever) is just a great big switch statement (taking its cues from an "action" variable stored in an incoming form or url (or sometimes session)).

each case includes a library and a code grouping and calls the relevant function. code groupings tend to have one or two dynamic functions and the rest are statically called. i could make them into objects but i find it much easier to hold procedural code in my head than objects.

each function ends up creating a set of variables and then it includes an html template.

the html template looks something like this:
Code:
<?
doctype();
styles();
scripts();
menu();
?>
<div class="content">
<!-- content goes here with <?=\\included variables?> -->
</div>
<?
footer();
?>

the functions link back to more or less static content that are stored in a library (which in turn includes html pages in the most part).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top