Let’s say I have the following code:
void validate(File f) //or boolean?????
{
...
try
{
//try to validate xml file
}
catch (Exception e)
{
}
}
...
try
{
validate(xml_file); //validates XML file
move(xml_file, valid_folder); //moves valid XML file to valid_folder
}
catch (Exception e)
{
move(xml_file, error_folder); //moves invalid XML file to error_folder
}
...
Should my validate() function be boolean and I should check whether file is valid XML file or not or catching will do it for me?
One problem you might want to think about is the single responsibility principle: a method should do one thing well.
Your validate method is doing at least three things:
Perhaps you’ll have an easier time if you break these up.
What if someone wants to validate XML without moving the file? Your method can’t be called in that case.
Even validating is questionable. If you check against a schema you’ll throw an exception on the first issue. Would users want to know ALL the problems at once? If yes, you’ll have to walk the tree and find all the errors yourself.