Hacker News new | ask | show | jobs
by yonixw 2834 days ago
when you want to change someone else code, nested blocks lets you know what conditions are needed for each line. when they are gathered at the start you know the condition for the function but for each line you could start a discussion what conditions are necessary and thus make the code atomic (change all by purpose\tests or change none)
1 comments

you dont reduce nesting by moving all checks to the beginning, you reduce nesting by moving the contents into methods.

if (product.hasPrice){ if (someOtherCondition){ Cart cart = cartService.getCartForCurrentUser(); cartService.addToCart(product,1); } }

to

if(canSellProduct(product)){ addToCurrentCart(product) }