I have been coding C # code for some time now and overall have a good understanding of the rule of thumb for coding standards. My colleagues have recently encouraged a results-based approach to coding function blocks rather than logic blocks and are looking for your advice. Below is an example of what I'm talking about, where the result of one situation is used to determine the code path, not nesting. It has been suggested that this approach is easier to read, especially if the result requires several levels of nesting, but I prefer to use Curlys Law and refactoring methods and functions where the nesting becomes deep.
private void MethodOne() { bool CarryOn = false; // first layer if (ValidationRuleOne() == true) { CarryOn = true; } else { CarryOn = false; } // second layer if (CarryOn) { CarryOn = ValidationRuleTwo(); } else { CarryOn = false; } // third layer if (CarryOn) { CarryOn = ValidationRuleThree(); } else { CarryOn = false; } }
This approach seems wrong to me, as I would suggest that the method should be rewritten as ..
private void MethodOne() { // first layer if (ValidationRuleOne() == true) { // second layer if (ValidationRuleTwo() == true) { // third layer if (ValidationRuleThree() == true) { } } } }
If the embedding becomes too complicated, would I suggest that the structure of the method / function be rethought to combine the logical groups of functions together into additional methods or functions?
Any thoughts were greatly appreciated.
Respectfully,
Tim
source share