Smaller code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tree*Rat

    Smaller code

    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_block s[] = $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_d ir)) )
    {
    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_d ir)) )
    {
    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

    #2
    Re: Smaller code

    Tree*Rat wrote:
    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_block s[] = $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_d ir)) )
    {
    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_d ir)) )
    {
    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@attgl obal.net
    =============== ===

    Comment

    • Gordon

      #3
      Re: Smaller code

      On Nov 11, 9:22 am, "Tree*Rat" <tree....@googl email.comwrote:
      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_block s[] = $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_d ir)) )
      {
              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_d ir)) )
      {
              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.

      Comment

      Working...