代码之家  ›  专栏  ›  技术社区  ›  PHLAK

格式化if语句以确保可读性

  •  5
  • PHLAK  · 技术社区  · 16 年前

    if (strpos($file, '.jpg',1) && file_exists("$thumbsdir/$file") == false || strpos($file, '.gif',1) && file_exists("$thumbsdir/$file") == false || strpos($file, '.png',1) && file_exists("$thumbsdir/$file") == false) {
      createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
      fwrite($log,date("Y-m-d")." @ ".date("H:i:s")."  CREATED: $thumbsdir/$file\n");
    }
    
    8 回复  |  直到 16 年前
        1
  •  17
  •   Neil Williams    15 年前

    我会将“is a image”逻辑提取到它自己的函数中,这使得 if 更具可读性,还允许您集中逻辑。

    function is_image($filename) {
        $image_extensions = array('png', 'gif', 'jpg');
    
        foreach ($image_extensions as $extension) 
            if (strrpos($filename, ".$extension") !== FALSE)
                return true;
    
        return false;
    }
    
    if (is_image($file) && !file_exists("$thumbsdir/$file")) {
        createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
        fwrite($log,date("Y-m-d")." @ ".date("H:i:s")."  CREATED: $thumbsdir/$file\n");
    }
    
        2
  •  4
  •   Fire Lancer    16 年前
    if ((strpos($file, '.jpg',1) ||
         strpos($file, '.gif',1) ||
         strpos($file, '.png',1))
        && file_exists("$thumbsdir/$file") == false)
    {
      createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
      fwrite($log,date("Y-m-d")." @ ".date("H:i:s")."  CREATED: $thumbsdir/$file\n");
    }
    
        3
  •  2
  •   John Millikin    16 年前
    function check_thumbnail($file)
    {
        return (strpos($file, '.jpg',1) && file_exists("$thumbsdir/$file") == false ||
                strpos($file, '.gif',1) && file_exists("$thumbsdir/$file") == false ||
                strpos($file, '.png',1) && file_exists("$thumbsdir/$file") == false);
    }
    
    if (check_thumbnail ($file)) {
      createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
      fwrite($log,date("Y-m-d")." @ ".date("H:i:s")."  CREATED: $thumbsdir/$file\n");
    }
    

    将逻辑提取到单独的函数后,可以减少重复:

    function check_thumbnail($file)
    {
        return (strpos($file, '.jpg',1) ||
                strpos($file, '.gif',1) ||
                strpos($file, '.png',1)) &&
               (file_exists("$thumbsdir/$file") == false);
    }
    
        4
  •  2
  •   Rob Bell    16 年前

    我将分开ifs,因为其中有一些重复代码。我还尝试尽早退出例行程序:

    if (!strpos($file, '.jpg',1) && !strpos($file, '.gif',1) && !strpos($file, '.png',1))
    {
        return;
    }
    
    if(file_exists("$thumbsdir/$file"))
    {
        return;
    }
    
    createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
    fwrite($log,date("Y-m-d")." @ ".date("H:i:s")."  CREATED: $thumbsdir/$file\n");
    
        5
  •  2
  •   ConroyP    16 年前

    这个 file_exists 支票已通过。

    if (file_exists("$thumbsdir/$file") == false)
    {
       if(strpos($file, '.jpg',1) ||
         strpos($file, '.gif',1) ||
         strpos($file, '.png',1)
       {
         createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
         fwrite($log,date("Y-m-d")." @ ".date("H:i:s")."  CREATED: $thumbsdir/$file\n");
       }
    }
    
        6
  •  1
  •   Community CDub    8 年前

    我会这样分解它,撇开冗余问题不谈:

    if (strpos($file, '.jpg',1) && file_exists("$thumbsdir/$file") == false
     || strpos($file, '.gif',1) && file_exists("$thumbsdir/$file") == false
     || strpos($file, '.png',1) && file_exists("$thumbsdir/$file") == false) {
      createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
      fwrite($log,date("Y-m-d")." @ ".date("H:i:s")."  CREATED: $thumbsdir/$file\n");
    }
    

    @Fire Lancer's 答案很好地解决了冗余问题。

        7
  •  1
  •   Rabbit Rabbit    16 年前

    垂直代码比水平代码可读性更好,imho。

    // Extract image info if possible
        // Note: Error suppression is for missing file or non-image
    if (@$imageInfo = getimagesize("{$thumbsdir}/{$file}")) {
    
        // Accept the following image types
        $acceptTypes = array(
            IMAGETYPE_JPEG,
            IMAGETYPE_GIF,
            IMAGETYPE_PNG,
        );
    
        // Proceed if image format is acceptable
        if (in_array($imageInfo[2], $acceptTypes)) {
    
            //createThumb(...);
            //fwrite(...);
    
        }
    
    }
    

    和平+快乐黑客。

        8
  •  1
  •   eyelidlessness    16 年前

    不如把我的两分钱扔进去。

    if(!file_exists($thumbsdir . '/' . $file) && preg_match('/\.(?:jpe?g|png|gif)$/', $file)) {
        createThumb($gallerydir . '/' . $file, $thumbsdir . '/' . $file, $thumbsize);
        fwrite($log, date('Y-m-d @ H:i:s') . '  CREATED: ' . $thumbsdir . '/' . $file . "\n");
    }