I want to re-write a method that has way too many nested if statements.
I came up with this approach and wanted your opinions:
public void MyMethod() { bool hasFailed = false; try { GetNewOrders(out hasFailed); if(!hasFailed) CheckInventory(out hasFailed); if(!hasFailed) PreOrder(out hasFailed); // etc } catch(Exception ex) { } finally { if(hasFailed) { // do something } } }
I’ve done stuff similar to that, but without the exception handling:
Or if you want to be clever:
If you are using exceptions anyway, why do you need the
hasFailedvariable?