I'm writing a simple console app to rename a bunch of files/folders, and I want to design this console app to be as modular as possible.

The console app is as simple as it gets: Ask the user what the source folder path is, what they want to rename the folder to, and the user presses enter. The app then finds the folder, and starts to recursively rename all the files/folders within the sub folders.

If the situation occurs where the user were to type a folder path that doesn't exist, then at some point I want the app to print "Couldn't find folder" to the user. This is where my question comes in: where should this error message be printed at? I can think of several options:

  1. Check if the folder exists before calling the RenameFolder(string folderPath, string newName) call. Print the error message in this folder exists check.
  2. Go ahead and call RenameFolder(string folderPath, string newName) immediately, but if it doesn't find a folder within that function, then return some error code or enum signifying that it couldn't find the folder. The caller would then recognize this enum and print an error message.
  3. Put an assert in the beginning of the function. If the assert fails, then print the error message. Note that this will stop program execution if the assert fires.
  4. Put an if statement in the beginning of the function checking if the folder exists, and if it doesn't, print the error message in that check and return.
  5. Put a try/catch within the function, have the exception be thrown, and in the catch statement print the error message then return.

I can't decide what way creates the most "modular" code. What are your guy's thoughts?

  • 2
    Note that options 1, 3 and 4 in your list produce a race condition
    – Jules
    Jun 14, 2016 at 0:16
  • 1
    Please get the folder name from the command line and not interactively. Jun 14, 2016 at 4:27

1 Answer 1


There's a problem with 1, 3, and 4: the folder could be deleted between your check and when you try to open it. Or the network could die, or permissions could get in the way, etc. Interacting with the filesystem is a great source of exogenous exceptions.

Deciding between 2 and 5, C# style is generally to avoid returning error codes, so I wouldn't recommend 2. Option 5 is fine, especially for a small program, but you might want to throw the exception to the caller and keep your user interaction away from your program logic.

So the final flow might go something like this:

var filename = GetFilename();
var newFilename = GetTarget();

    renameFile(filename, newFilename);
catch (IOException e)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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