🐿️
14

A senior dev told me to stop nesting my if statements 3 levels deep and I fought it for weeks

I was working on this little Python script for scraping weather data and my code looked like a mess of tabs and brackets. This guy on a code review pointed out I had if statements inside if statements inside if statements, like 4 levels down. He said try breaking each condition into its own function or use early returns to flatten it. I thought he was being picky but I tried it on a small 50 line block and suddenly I could actually read what I wrote. Now I refactor any function that goes past 2 levels deep and my debugging time has dropped by maybe half. Has anyone else had a simple code review tip that just clicked after you tried it?
2 comments

Log in to join the discussion

Log In
2 Comments
betty_shah
betty_shah26d ago
But I fought it for weeks" - that part got me. Don't you think we all have this weird pride thing about our spaghetti code? Like it's a badge of honor or something.
5
susan_nguyen
Brought back a memory of this one time I spent three days chasing a bug in someone's old code (a legacy project, you know the type) only to find it was a single misplaced semicolon. And you know what, @betty_shah? I was weirdly proud of that spaghetti mess after I fixed it, like I'd wrestled a wild animal. It's that weird bond you form with something that's totally broken but still somehow running in production, bless its heart.
2