wesol89
wesol89

Reputation: 33

Proper way of coding return statement with many conditions

I'm wondering if there is a nice way of coding something like this:

return IOTools.CreateFile(@"D:\test")
    && IOTools.CreateDir(@"D:\test_folder\")
    && IOTools.DeleteFile(@"D:\test_file_2")
    && IOTools.DeleteDir(@"D:\test_folder_2\");

I know that I can put it all in other function and there use if statements to return false after every condition, but maybe there is another way to stop program from trying to do next conditions if one is false?

Upvotes: 1

Views: 138

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460288

&& is already a short-circuiting operator. So it stops as soon as one condition is false.

if x is false, y is not evaluated, because the result of the AND operation is false no matter what the value of y is. This is known as "short-circuit" evaluation.

Btw, || does also stop evaluation as soon as one condition is true.

If you instead ask for a more readable way, you could use variables:

bool canCreateFile = IOTools.CreateFile(@"D:\test");
bool canCreateDir = IOTools.CreateDir(@"D:\test_folder\");
bool canDeleteFile = IOTools.DeleteFile(@"D:\test_file_2");
bool canDeleteDir = IOTools.DeleteDir(@"D:\test_folder_2\");

return canCreateFile && canCreateDir && canDeleteFile  && canDeleteDir; 

If code is getting too complicated i use variables to increase readability, maintainability and testability.

Upvotes: 4

drew_w
drew_w

Reputation: 10430

A few really important guidelines:

  • If you use "&&" and something returns false, the rest of the 'if' will not be executed. This may be the desire or it may not be. In your case, if any create directory/delete fails, the rest of them are not going to be executed.
  • You need to think about maintainability with code. Boolean statements checking a series of conditions with side effects can be difficult to edit later because the programmer making the changes may not be aware of these side effects.

Hopefully that helps a bit.

More Details

An example of where editing is difficult. Lets say another programmer wants to add a new create to your program. Now the program is:

return IOTools.CreateFile(someNewFile)
    && IOTools.CreateFile(@"D:\test")
    && IOTools.CreateDir(@"D:\test_folder\")
    && IOTools.DeleteFile(@"D:\test_file_2")
    && IOTools.DeleteDir(@"D:\test_folder_2\");

But the new programmer makes a mistake and that file happens to never be created. They followed your pattern - and actually broke your code. This is a serious issue suddenly! A much better pattern would be:

var overallSuccess = false;
overallSuccess |= IOTools.CreateFile(someNewFile);
overallSuccess |= IOTools.CreateFile(@"D:\test");
overallSuccess |= IOTools.CreateDir(@"D:\test_folder\");
overallSuccess |= IOTools.DeleteFile(@"D:\test_file_2");
overallSuccess |= IOTools.DeleteDir(@"D:\test_folder_2\");
return overallSuccess;

Now all statements run and the return value represents whether or not ANY create was successful. You can change the "|=" to an "&=" to have true return if ALL statements were successful. Best of luck!

Upvotes: 0

Derek
Derek

Reputation: 8793

This is about as clean as you will get. Introducing anything else will likely add, rather than reduce, complexity.

Upvotes: 0

Related Questions