Introduction
Code review is an integral part of a developer's day-to-day life. Getting mega pint of code review comments has lots of problems but most importantly it hurts developer-ego! I hope you'll find this article helpful.
Disclaimer: This blog does not claim to be one-and-only solution for all coding problems. Apart from some adopted community guidelines everything is subjective to the coder or reviewer. This is merely my opinions and compiled list of bad coding practices I've found through my experience. All the code examples are made to demonstrate a single problem at a time.
1. Variable Names
Bad
temp={"banana":"yellow","apple":"red","grape":"green"}list1=[]list2=[]fork,vintemp.items():list1.append(k)list2.append(v)print(f"fruits:{list1}")print(f"colors:{list2}")
Good
fruit_colors={"banana":"yellow","apple":"red","grape":"green"}fruits=[]colors=[]forfruit,colorinfruit_colors.items():fruits.append(fruit)colors.append(color)print(f"fruits:{fruits}")print(f"colors:{colors}")
Pros
- Code readability
- Less-prone to bugs
2. Exeption Handling
Bad
try:fruit_colors={"banana":"yellow","apple":"red","grape":"green"}color=fruit_colors["watermelon"]exceptException:print("Failed to get color of watermelon :(")
Good
fruit_colors={"banana":"yellow","apple":"red","grape":"green"}try:color=fruit_colors["watermelon"]exceptKeyError:print("Failed to get color of watermelon :(")
Pros
- Code readability
- Easy troubleshooting
3. Utilizing Standard Library
Bad
fruits=["apple","banana","grape"]i=0forfruitinfruits:print(f"{i}:{fruit}")i+=1
Good
fruits=["apple","banana","grape"]fori,fruitinenumerate(fruits):print(f"{i}:{fruit}")
Pros
- Compact
4. Error Logging
Bad
try:res=requests.get("https://www.google.com")res.raise_for_status()logging.info("Success!")exceptExceptionase:logging.error(f"Unexpected error occured:{e}")
Good
try:res=requests.get("https://www.google.com")res.raise_for_status()logging.info("Success!")exceptHTTPErroraserr:logging.error(f"Request failed with status code:{err.response.status_code}")exceptException:# logging.exception logs traceback informationlogging.exception("Unexpected error occured:")
Pros
- Code readability
- Easy troubleshooting
5. Block Comments
Bad
username=username.lower()username=username.strip()username=re.sub(r"\s+","",username)ifnotre.match(r"^[a-zA-Z0-9_]{5,20}$",username):print("Invalid username")exit(1)
Good
# Perform string transforms on the usernameusername=username.lower()username=username.strip()username=re.sub(r"\s+","",username)# Check if username is validifnotre.match(r"^[a-zA-Z0-9_]{5,20}$",username):print("Invalid username")exit(1)
Pros
- Code readability
Conclusion
This blog is a part of a series and each of these points can be extended with some advanced scenarios. Unfortunately, I can't mention all the scenarios in a single blog so I will try to cover other cases in future parts. Feel free to reach out if you have any questions, suggestions for improvments or any good coding practice you've found.
See post on my blog:https://alacrity.dev/better-coding-practices-python-part-1/
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse