I’m creating a printer class that will need to print both HTML strings and HTML documents. So basically it can get:
Printer.Print("<b>Hello world</b>");
And
Printer.Print(@"C:\hello.html");
So in designing my class the Print method definition I’m deciding between the following:
public static void Print(string inputString, string mode){
if(mode=="htmlString"){//Print the string itself}
else if(mode=="htmlFile"){//Print the document in the filepath}
}
Or
public static void Print(string inputString){
if(file.Exists(inputString)){//Print the document in the filepath}
else{//Print the string itself}
}
In general, which is the better practice? The first option requires another argument which is not great, but then if we use the second option, if we intend to actually print a file but use an incorrect file name, it will print the wrong thing.
A lot of times there is just too much room for contingencies, specifically in this case where you have to determine how to act based on the input, then further do validation processing (i.e. File.Exists), it’s crying out for false positives. In my opinion do something like this instead: