Structuring

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Structuring

Post by thatsme »

Which structuring method is good?

I am thinking of making a folder for each link. Suppose i have links: Category, Products, Members, Orders - each links will have add, edit, view, paging, delete functionality,

index.php
category - folder
add_cat
edit_cat
view_cat
delete_cat
page_cat

cat_big_image - folder

Same for Products, Members

In many tutorials, all the above functionality are written in a single file for each link. If i write it in a single file, the code increases and sometimes(if the form having many fields) looks ugly. Is there any disadvantage in creating folder for each links? will it slow down the site as the files will be inside folders and takes time to access them?

Thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Structuring

Post by califdon »

What do you mean by "links"? It sounds like maybe you're talking about dividing your script code into script files, but you're confusing me with talking about folders. There's no performance loss in using folders, but there could be in loading very large script files that contain code that includes a lot that is never used for any one load. I would say the advantages of folders are entirely in how convenient it is for the programmer to work on them.
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Re: Structuring

Post by thatsme »

Thanks.

folders for each script file. For example, Category may be a link it will have add, edit, delete, view and paging functionality. Instead of creating a single file for 5 conditional statements, i am thinking of using 5 files. As this will create many files i would be keeping them in separate folders. In tutorials all the above functionality are handled in a single file either using conditional statements or switch cases.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Structuring

Post by califdon »

thatsme wrote:Thanks.

folders for each script file. For example, Category may be a link it will have add, edit, delete, view and paging functionality. Instead of creating a single file for 5 conditional statements, i am thinking of using 5 files. As this will create many files i would be keeping them in separate folders. In tutorials all the above functionality are handled in a single file either using conditional statements or switch cases.
So you're saying one folder per file? I see no point in doing that. As to splitting a script into multiple files, I would say it's not worth the trouble unless your routines are extremely large. Different programmers may have different ideas on this, but I think it helps to have related routines in the same file because very often changes in one part of the routine, such as changing a variable name, are easier to manage when they are together; also, I find it easier to understand the flow logic when they are together. But that's probably a preference that wouldn't be shared by everyone. In theory, splitting alternative branches of a script into separate files would result in faster loading, since you would only load one of your 5 files, as required, instead of all 5 every time. But I think the difference wouldn't be noticeable unless, as I said, the combined file was extremely large.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Structuring

Post by Christopher »

I am not clear how creating more more files AND a folder for each of them is going to be less confusing than just more files? In general, the practice in PHP is going the opposite direction -- which is to use mod_rewrite for clean URLs and to group related request handlers into Action controllers.
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Structuring

Post by RobertGonzalez »

I agree with arborint. You may want to think your app through to see what ways your app would benefit from something like this.
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Re: Structuring

Post by thatsme »

Everah | Please use appropriate bbCode tages when posting code in the forums. For monospace code text, use [code]. For a specific language, including PHP, use [code={lang}] where lang is the lowercase name of the language you want to use.

Code: Select all

$condition = $_REQUEST['condition'];
 
if($condition=='')
  $condition='paging';
 
if($condition=='submit')
{
  //validation and insert or update statement
}
 
if($condition=='form')
{
    //form is displayed
}
 
 
if($condition=='view')
{
   //form in new mode
}
 
if($condition=='delete')
{
//delete statement
}
 
if($condition=='paging')
{
  //code for paging;
}
 
As you can see even if the form consists of five fields a minimum of 30 lines of code is needed. I want to reduce the code in each page.

Everah | Please use appropriate bbCode tages when posting code in the forums. For monospace code text, use [code]. For a specific language, including PHP, use [code={lang}] where lang is the lowercase name of the language you want to use.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Structuring

Post by califdon »

I think what we're all saying is that you're worrying about the wrong things. Your goal of reducing the number of lines in a script doesn't seem to be based on any sound reasoning and might even be counterproductive. Another way of saying it is, "if it ain't broke, don't fix it."
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Structuring

Post by RobertGonzalez »

You are trying to build a controller that is handling routing for you. This is not a new concept, but it can get hairy if you don't plan this out right.

Something you could do (and this is only an example) is:

Code: Select all

<?php
$condition = $_REQUEST['condition'];
$file = 'module.'.$condition.'.php';
 
if (file_exists($file)) {
  // If there is a known file get that one
  require_once $file;
} else {
  // Otherwise use this as a default
  require_once 'module.paging.php';
}
?>


This is only one of many ways to achieve what you want. Put some thought into it and see what you can come up with.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Structuring

Post by Christopher »

thatsme wrote:As you can see even if the form consists of five fields a minimum of 30 lines of code is needed. I want to reduce the code in each page.
Reducing code is a practice -- not a goal. Goals are things like: implementing the required functionality, reducing maintenance, or meeting some resource constraints. Solve the problem first ... then optimize.
(#10850)
Post Reply