3 ms·
I’ve always been a fan of refactoring deeply nested if/else into a more linear less nested sequence of if/return statements. If the function spec is a sequence
by zaroth 9y ago
I’ve always been a fan of refactoring deeply nested if/else into a more linear less nested sequence of if/return statements.
If the function spec is a sequence of complex AND checks which must be satisfied in order to do work, an unnested sequence of “if (!x) return” statements are logically equivalent, have less indentation, and I think it’s much cleaner and easier to read comment blocks above each IF explaining the reason why each check is there.
I also really like seeing the main body of the function which is doing the real work at be bottom without any nesting/conditionals around it at all. It helps you find it if it’s always down there at the end, rather than in the middle indented way to the right.