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?
3 comments
Log in to join the discussion
Log In3 Comments
phoenix_singh2520d 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
wader7119d ago
The part about "looked at me like I was a wizard" got me. I read some article a while back that said most coding problems are just pattern recognition in disguise. Once you see the pattern, a dictionary and a couple conditions really is all you need most of the time. It's funny how the simple solution can feel like magic when you're used to making everything complicated.
3
lucasw8419d ago
Right, because 8 levels of if statements is totally the way to solve a problem. Its like using a chainsaw to open a bag of chips.
2