Menu

1 / 2

Never Nesting by Nev R. Nester

DEV Community·Alex Neal Albinda·6 months ago
#vtiwDPVo
#cleancode#beginners#software#coding#return
Reading 0:00
15s threshold

Never Nesting

I once came across this video around a year ago, and I still think about it whenever I code. Never nesting is essentially never nesting past 3 indents (not strict at all, but as a recommendation.)

As Linus Torvalds says:

... if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

void process_order ( User * u , Order * o , Payment * p ) { if ( u != NULL ) { if ( o != NULL ) { if ( p != NULL ) { if ( ! u -> is_banned ) { if ( o -> total > 0 ) { if ( check_inventory ( o )) { if ( p -> balance >= o -> total ) { if ( execute_payment ( u , o , p )) { if ( finalize_order ( o )) { if ( send_receipt ( u )) { printf ( "Success \n " ); } else { log_error ( "Receipt failed" ); } } else { log_error ( "Finalize failed" ); } } else { log_error ( "Payment failed" ); } } else { log_error ( "Insufficient funds" ); } } else { log_error ( "Out of stock" ); } } else { log_error ( "Order empty" ); } } else { log_error ( "User banned" ); } } else { log_error ( "No…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.