4 ms·
> Having multiple return statements in a method actually renders the method LESS readable. I 100% disagree. Introducing a variable instead of using multiple re
by Bognar 11y ago
> Having multiple return statements in a method actually renders the method LESS readable.
I 100% disagree. Introducing a variable instead of using multiple returns means I now have to keep track of yet another variable throughout the entire rest of the method. Is it modified before it's returned? Gotta read the whole thing to find out.
Early returns reduce the complexity of a method by reducing the number of possible states as you move through the method.
- glastra 11y ago> Early returns reduce the complexity of a method by reducing the number of possible states as you move through the method. As I said in other comments, early returns for exceptions and bad input are fine. They are exceptions, though, not return statements. Go ahead and use return if you want/can, although I wouldn't. If it's not for an exception or input validation failure, then we have something like: "Alright, so here we do this, unless this, that and that other condition over there were true earlier, because the method would've returned then..." I find that the complexity to keep track of with this approach is pretty much the same, or even more depending on the branching.