Python tip: Consider using a dictionary over large if/else statements: Conditional statements are a great programming tool; they are very intuitive, and can help thinking through complex problems. However, as scripts build out they can become cumbersome and difficult to follow. Working in Python, dictionaries are fantastic ways of storing information that can be retrieved inside of functions or wherever you need it. The image shows a basic example. Another great benefit of dictionaries is that if the information is updated (e.g. pro is now 25), you can change the value in your dictionary and have the change propagate through your whole system without having to find every conditional statement you made. Bonus tip for my fellow R lovers out there, you can use named lists to achieve the same goal: rates <- list(basic = 10, pro = 20, enterprise = 50) rates[["basic"]] #Python #R #SoftwareEngineering #CleanCode #Refactoring #Programming #Developers #TechBestPractices
Shouldn't elif be using "==" instead of "="? Goes to show, more typing means more error lol
Helpful comparison! Just noticed a small typo in the left example, both elif lines use = instead of ==
Use .get() to deal with KeyErrors
Or you can step over beginner level and use pydantic and enums.
for the dictionary approach.. is there any reason to wrap it in a function rather than use the dictionary directly? basic_rate = RATES['basic']
Doce on the right will raise a key error if an unknown value is added, also cod on the left is well not run.
Check out Interviora to level up your interview prep: interviora.com 🚀
The sweet O(1) average too clutch. Just continues to prove the answer is always hash tables.
I like to use nested ifs for the chaos factor.