Connecting Tech Pros Worldwide Forums | Help | Site Map

Smaller code

Tree*Rat
Guest
 
Posts: n/a
#1: Nov 11 '08
Trying to reduce alot of my codes size and am having no joy with this
code. How can I get this smaller?

// Block
$prefix_length = strlen( PREFIX_BLOCKS );

$block_dir = @opendir($root_path . 'modules/modules_blocks');
while( false !== ($dir_name = readdir($block_dir)) )
{
if( $dir_name != "." && $dir_name != ".." )
{
if ( substr( $dir_name, 0, $prefix_length ) == PREFIX_BLOCKS
)
{
if( is_dir( $root_path . 'modules/modules_blocks/' .
$dir_name))
{
$mod_list_blocks[] = $dir_name;
}
}
}
}
@closedir( $block_dir );

// Page
$prefix_length = strlen( PREFIX_PAGES );

$page_dir = @opendir($root_path . 'modules/modules_page');
while( false !== ($dir_name = readdir($page_dir)) )
{
if( $dir_name != "." && $dir_name != ".." )
{
if ( substr( $dir_name, 0, $prefix_length ) == PREFIX_PAGES
)
{
if( is_dir( $root_path . 'modules/modules_page/' .
$dir_name))
{
$mod_list_page[] = $dir_name;
}
}
}
}
@closedir( $page_dir );

// Admin
$prefix_length = strlen( PREFIX_ADMIN );

$admin_dir = @opendir($root_path . 'modules/modules_admin');
while( false !== ($dir_name = readdir($page_dir)) )
{
if( $dir_name != "." && $dir_name != ".." )
{
if ( substr( $dir_name, 0, $prefix_length ) == PREFIX_ADMIN
)
{
if( is_dir( $root_path . 'modules/modules_admin/' .
$dir_name))
{
$mod_list_admin[] = $dir_name;
}
}
}
}
@closedir( $admin_dir );


big thanks

Jerry Stuckle
Guest
 
Posts: n/a
#2: Nov 11 '08

re: Smaller code


Tree*Rat wrote:
Quote:
Trying to reduce alot of my codes size and am having no joy with this
code. How can I get this smaller?
>
// Block
$prefix_length = strlen( PREFIX_BLOCKS );
>
$block_dir = @opendir($root_path . 'modules/modules_blocks');
while( false !== ($dir_name = readdir($block_dir)) )
{
if( $dir_name != "." && $dir_name != ".." )
{
if ( substr( $dir_name, 0, $prefix_length ) == PREFIX_BLOCKS
)
{
if( is_dir( $root_path . 'modules/modules_blocks/' .
$dir_name))
{
$mod_list_blocks[] = $dir_name;
}
}
}
}
@closedir( $block_dir );
>
// Page
$prefix_length = strlen( PREFIX_PAGES );
>
$page_dir = @opendir($root_path . 'modules/modules_page');
while( false !== ($dir_name = readdir($page_dir)) )
{
if( $dir_name != "." && $dir_name != ".." )
{
if ( substr( $dir_name, 0, $prefix_length ) == PREFIX_PAGES
)
{
if( is_dir( $root_path . 'modules/modules_page/' .
$dir_name))
{
$mod_list_page[] = $dir_name;
}
}
}
}
@closedir( $page_dir );
>
// Admin
$prefix_length = strlen( PREFIX_ADMIN );
>
$admin_dir = @opendir($root_path . 'modules/modules_admin');
while( false !== ($dir_name = readdir($page_dir)) )
{
if( $dir_name != "." && $dir_name != ".." )
{
if ( substr( $dir_name, 0, $prefix_length ) == PREFIX_ADMIN
)
{
if( is_dir( $root_path . 'modules/modules_admin/' .
$dir_name))
{
$mod_list_admin[] = $dir_name;
}
}
}
}
@closedir( $admin_dir );
>
>
big thanks
What's wrong with the code (other than using '@'? Clarity is MUCH more
important than size.

The only change I would make is to put the left braces on the same like
as the if or while statement, i.e.

if( $dir_name != "." && $dir_name != ".." ) {

but that's only a matter of style.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Gordon
Guest
 
Posts: n/a
#3: Nov 15 '08

re: Smaller code


On Nov 11, 9:22*am, "Tree*Rat" <tree....@googlemail.comwrote:
Quote:
Trying to reduce alot of my codes size and am having no joy with this
code. How can I get this smaller?
>
// Block
$prefix_length = strlen( PREFIX_BLOCKS );
>
$block_dir = @opendir($root_path . 'modules/modules_blocks');
while( false !== ($dir_name = readdir($block_dir)) )
{
* * * * if( $dir_name != "." && $dir_name != ".." )
* * * * {
* * * * * * * * if ( substr( $dir_name, 0, $prefix_length) == PREFIX_BLOCKS
)
* * * * * * * * {
* * * * * * * * * * * * if( is_dir( $root_path . 'modules/modules_blocks/' .
$dir_name))
* * * * * * * * * * * * {
* * * * * * * * * * * * * * * * $mod_list_blocks[] = $dir_name;
* * * * * * * * * * * * }
* * * * * * * * }
* * * * }}
>
@closedir( $block_dir );
>
// Page
$prefix_length = strlen( PREFIX_PAGES );
>
$page_dir = @opendir($root_path . 'modules/modules_page');
while( false !== ($dir_name = readdir($page_dir)) )
{
* * * * if( $dir_name != "." && $dir_name != ".." )
* * * * {
* * * * * * * * if ( substr( $dir_name, 0, $prefix_length) == PREFIX_PAGES
)
* * * * * * * * {
* * * * * * * * * * * * if( is_dir( $root_path . 'modules/modules_page/' .
$dir_name))
* * * * * * * * * * * * {
* * * * * * * * * * * * * * * * $mod_list_page[] = $dir_name;
* * * * * * * * * * * * }
* * * * * * * * }
* * * * }}
>
@closedir( $page_dir );
>
// Admin
$prefix_length = strlen( PREFIX_ADMIN );
>
$admin_dir = @opendir($root_path . 'modules/modules_admin');
while( false !== ($dir_name = readdir($page_dir)) )
{
* * * * if( $dir_name != "." && $dir_name != ".." )
* * * * {
* * * * * * * * if ( substr( $dir_name, 0, $prefix_length) == PREFIX_ADMIN
)
* * * * * * * * {
* * * * * * * * * * * * if( is_dir( $root_path . 'modules/modules_admin/' .
$dir_name))
* * * * * * * * * * * * {
* * * * * * * * * * * * * * * * $mod_list_admin[] = $dir_name;
* * * * * * * * * * * * }
* * * * * * * * }
* * * * }}
>
@closedir( $admin_dir );
>
big thanks
The size of a piece of PHP code has no bearing on its performance.
Keeping code readable is far more important. If you hare having
performance issues that would suggest you have picked an incorrect
algorithm or have implemented it in a suboptimal way. If this is the
case then there is far more to be gained by looking into what your
code is doing and how it works rather than how it is written.
Closed Thread