3
The one thing every rookie does with if statements that drives me nuts
I spent 3 hours last night helping a buddy debug his Python script and it was just nested ifs inside nested ifs inside more ifs. He had 8 levels of indentation for something that could have been 4 elif statements and a dictionary lookup. Why do so many tutorials teach if/else chains instead of showing beginners how to flatten logic with proper data structures?
1 comments
Log in to join the discussion
Log In1 Comment
phoenix_singh2534m ago
My buddy just went through this exact thing last week. He was trying to check user roles and had like 6 nested ifs checking if the user existed, then if they were logged in, then if they had permissions. I showed him one dictionary lookup and a couple simple conditions and it turned into 10 lines of clean code. He looked at me like I was a wizard but really it was just not overcomplicating things. The tutorials really let beginners down by not showing how maps and simple logic trees work.
4