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:
- Check if the folder exists before calling the
RenameFolder(string folderPath, string newName)
call. Print the error message in this folder exists check. - 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. - 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.
- 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.
- 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?