Refactoring Bad Code
I have been learning Python for a while. I recently have looked back my old labs and projects and noticed that I had written bad codes which caused memory waste, the server need more time to run. Thus, I want to summarize some bad coding habits to help you to stay away from these bad habits.
In the examples below, some will cause performance issues, and lead to hidden bugs or difficulties in future maintenance.
1. Reasonably variable names
# Worst practice
# Variable names are hard to understand
y1 = '2018'
t = '16/07/2019'
c = 'blue'
# Fixed practice
review_year = '2018'
review_date = '16/07/2019'
color = 'blue'
After the bad code was fixed, others could understand my code easier. And, it is easy to maintain the code.
2. Shortage "for" loop
# Worst practice
# Should use "enmerate" insted of long code
numbers = [1, 2, 3, 4]
for i, j in zip(range(len(numbers)), numbers):
print (i, j)
0 1
1 2
2 3
3 4
# Fixed practice
numbers = [1, 2, 3, 4]
for i, j in enumerate(numbers):
print(i,j)
0 1
1 2
2 3
3 4
Now, the fixed code was proven, and easier to read.
3. Lambda function
# Worst practice
# Too many characters and long code
def even_fn(x):
if x % 2 == 0:
return True
return False
print(list(filter(even_fn, range(1,40))))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
# Fixed practice
# Transforming into this one-liner
print(list(filter(lambda x: x % 2 == 0, range(1,40))))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
"lambda" functionis very useful, but can only have one expression.
4. List Comprehension
# Worst practice
# the length of the code is too long.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_list_numbers = []
for i in numbers:
new_list_numbers.append(i * 2)
new_list_numbers
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# Fixed practice
new_list_numbers = [i * 2 for i in numbers]
new_list_numbers
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
The power of list comprehensions is to provide a concise way to create lists.
5. If statement
# Worst practice
# long code
x = 10
value = input("Enter a number: ")
y = int(value)
if x < y:
print(f"{x} is less than {y}")
elif x == y:
print(f"{x} is equal to {y}")
else:
print(f"{x} is more than {y}")
Enter a number: 15
10 is less than 15
# Fixed practice
x = 10
y = int(input("Enter a number:"))
equality = "is equal to" if x == y else "is less than" if x < y else "is more than"
print(f"{x} {equality} {y}")
Enter a number:15
10 is less than 15
This is another way how to use "if" statement briefly
6. Good comments
# useless comment
k = 30 # assigning k a value of 30
# helpful comment
salestax_10 = 1.10 # defining a sales tax of 10%
salestax_20 = 1.20 # defining a sales tax of 20%
Commenting is an important skill that needs to be learned. Commenting our code helps us to explain our thoughts process, and others to understand the code flow.
7. Line Continuations
# Worst practice
new_columns_dict_sat_2017 = {'Participation':'part_sat_2017', 'Evidence-Based Reading and Writing':'proficiency_2017', 'Math':'math_sat_2017', 'Total':'total_sat_2017'}
# Fixed practice
new_columns_dict_sat_2017 = {'Participation':'part_sat_2017',
'Evidence-Based Reading and Writing':'proficiency_2017',
'Math':'math_sat_2017',
'Total':'total_sat_2017'
}
Making code flow is more organized and easy to understand