tags:

views:

319

answers:

3

Hi,

I have a messy function that needs refactoring, it has too many nested IF's and it makes me nervous just to look at it!

Please ignore what the functions are doing, I'm more concerned with the structure/flow and how it can be refactored so it has less nested IF statements

The basic flow is as follows:

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
      }


}
+9  A: 

I would be tempted to go as far as:

    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
        }

    }
Andrew Kennan
In the original code, if IsValidFileFormat was false, the file got deleted. This isn't equivalent.
Bill the Lizard
Sorry - that's what I get for copying and pasting without reading properly. I've fixed it.
Andrew Kennan
+1 then, for showing the "extract subroutine" refactoring.
Bill the Lizard
this answer is technically correct and better than what the poster had, but it's still not really designed with good object orient principals.
mson
I think the original question was more about structuring nested conditions than actually deciding if an uploaded file can be moved to a safe folder. My point is that moving the checks to another method preserves the logic while improving the readability.
Andrew Kennan
+3  A: 

The following is equivalent to your original code.

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
    }
}
Bill the Lizard
I see the error I made -- just missed the last else because it didn't fit on my screen -- but there's no point in me rewriting mine to make it look like yours.
tvanfosson
This is what I would do. I don't think I would split that bit out into another function as another answerer did, unless I expected to use that logic again.
BobbyShaftoe
A: 

This honors the original conditions under which the file is deleted and is cleaner and more compact. My first answer set folderId, but I then realized it isn't used.

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
        }
    }
Software Monkey