重构这个嵌套在try / catch中的嵌套IF函数

时间:2008-12-08 02:47:16

标签: refactoring

我有一个需要重构的混乱功能,它有太多嵌套的IF,只是看着它让我感到紧张!

请忽略这些函数正在做什么,我更关心结构/流程以及它如何被重构,因此它具有较少的嵌套IF语句

基本流程如下:

public static void HandleUploadedFile(string filename)
{

      try
      {
        if(IsValidFileFormat(filename)
        {

            int folderID = GetFolderIDFromFilename(filename);

            if(folderID > 0)
            {

                if(HasNoViruses(filename)
                {

                    if(VerifyFileSize(filename)
                    {

                        // file is OK
                        MoveToSafeFolder(filename);

                    }
                    else
                    {
                        DeleteFile(filename);
                    }


                }
                else
                {
                    DeleteFile(filename);
                }


            }
            else
            {
                DeleteFile(filename);
            }



        }
        else
        {
            DeleteFile(filename);
        }
      }
      catch (Exception ex)
      {

      }
      finally
      {
        // do some things
      }


}

4 个答案:

答案 0 :(得分:10)

我很想去:

    private static bool CanMoveToSafeFolder(string filename)
    {
        return IsValidFileFormat(filename)
            && GetFolderIDFromFilename(filename) > 0
            && HasNoViruses(filename)
            && VerifyFileSize(filename);
    }

    public static void HandleUploadedFile(string filename)
    {

        try
        {

            if (CanMoveToSafeFolder(filename))
            {
                // file is OK
                MoveToSafeFolder(filename);
            }
            else
            {
                DeleteFile(filename);
            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            // do some things
        }

    }

答案 1 :(得分:3)

以下内容相当于原始代码。

public static void HandleUploadedFile(string filename)
{
    try
    {
        if( IsValidFileFormat(filename) && 
            (GetFolderIDFromFilename(filename) > 0) && 
            HasNoViruses(filename) &&
            VerifyFileSize(filename) )
        {
              MoveToSafeFolder(filename);
        }
        else
        {
              DeleteFile(filename);
        }
    }
    catch (Exception ex)
    {
        // do some things
    }
    finally
    {
        // do some cleanup
    }
}

答案 2 :(得分:0)

这符合删除文件的原始条件,更清晰,更紧凑。我的第一个答案是设置folderId,但后来我意识到它没有被使用。

public static void HandleUploadedFile(string filename) {
    try {
        if(IsValidFileFormat(filename) 
        && GetFolderIDFromFilename(filename)>0 
        && HasNoViruses(filename) 
        && VerifyFileSize(filename)) {
            MoveToSafeFolder(filename);                                 // file is OK
            }
        else {
            DeleteFile(filename);
            }
        }
    catch (Exception ex) {
        // HANDLE THE EXCEPTION; AT LEAST LOG IT!
        }
    finally {
        // do some things
        }
    }

答案 3 :(得分:0)

这是我重构你的案例的解决方案(抱歉java风格):

我将创建一个如下所示的课程UploadedFileChecker

private class UploadedFileChecker {
    String fileName;

    UploadedFileChecker(String fileName) {
        this.fileName = fileName;
    }

    public void check() throws BadUploadedFileException {
        checkNameFormat();
        checkFolderId();
        scanVirus();
        checkFileSize();
    }

    private void checkFolderId() {
        // throw BadUploadedFileException if not passed.
    }

    private void checkNameFormat() {
        // throw BadUploadedFileException if not passed.
    }

    private void scanVirus() {
        // throw BadUploadedFileException if not passed.
    }

    private void checkFileSize() {
        // throw BadUploadedFileException if not passed.
    }
}

有例外

public class BadUploadedFileException extends RuntimeException {

}

重构的handleUploadedFile将成为:

public static void handleUploadedFile(string filename) {

   try {
        checkFile(fileName);
    } catch (BadUploadedFileException ex) {
        deleteFile(fileName);
    } catch (Exception ex) {

   } finally {

   }
}

private static void checkFile(String fileName) {
    new UploadedFileChecker(fileName).check();
}

private static void deleteFile(String fileName) {

}