Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> It still amazes me how they can believe that putting all the guards in tens of nested ifs can be saner than a few extra returns at the top.

If I have a function that needs a group of guards like that but local style guides demand a single exit point, I start a boolean like “AllHunkyDory” that gets set false where a return would be. Then you can keep the less nested structure and just wrap the main body in “if(AllHunkyDory){}”.

It is probably a touch less efficient, that variable is going to take up a register at least throughout the checks, but a fiunction with piles of guard clauses is hopefully not in a tight loop anyway so that is unlikely to matter.



Sure, there are ways, less readable and more error prone. But that's not what you see where this rule is enforced. You see nested ifs.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: