0
\$\begingroup\$

Create folder on ftp if possible.

public async Task<bool> CreateFolder(string path)
{
    FtpWebRequest ftpRequest = null;
    try
    {
        ftpRequest = ftpBuilder.Create(path, WebRequestMethods.Ftp.MakeDirectory);
        using (var ftpResponse = (FtpWebResponse)await ftpRequest.GetResponseAsync())
        using (var ftpStream = ftpResponse.GetResponseStream())
        {
            return true;
        }
    }
    catch
    {
        return false;
    }
}

This is my solution but I have two doubts

  1. Is it ok in this case to have return statement in catch?
  2. Is it ok to have just return true in using?
\$\endgroup\$
3
\$\begingroup\$

To answer your questions:

  1. Yes if you never want to know why something went wrong. You should log the exception so you can diagnose issues later. Swallowing exceptions like that is rarely a good idea.
  2. No. Web requests return status codes. It could be returning a 5xx error which would indicate that the directory was not created successfully but you'd be acting like it had worked. A response successfully is not the same thing as a successful response.
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

I would remove the try catch and do not return any boolean. As a general rule, you should only catch exceptions you can handle.

For example, if you use this piece of code in a user interface, the boolean cuts any possibility to give feedback to the user. Instead, if the method will simply throw, you can probably handle it in a more convenient way and display a message to the user (for example, can't connect to...)

Additionaly, if you catch and return booleans everywhere, your cyclomatic complexity just grows as your code will look to something like this

        if (condition)
        {
            if (otherCondition)
            {
                ...
            }
        }

If you still want to return a boolean, I would call the method TryCreateFolder

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.