Characteristics of Good Code and How to Improve Python Code
Even if code works well, it cannot be considered great if it lacks readability or performance.
So, what exactly is great code?
Attributes of Good Code
Simply functioning without errors does not qualify as good code.
Generally, the following factors are considered when evaluating code quality:
-
Readability: It should be easy for other developers or yourself to understand the code when you look at it later. Variable names that effectively describe their function, comments, and appropriate code structure increase readability.
-
Efficiency: The code should perform the given task efficiently, without using excessive memory or CPU resources.
-
Reusability: Well-written code should be easy to reuse. By utilizing functions or classes, repetitive code should be minimized, and it should be structured to be easily used when needed.
-
Flexibility: The design should be extensible to easily respond to changing requirements. Constants should be used for fixed values, variables for those that can change, and the code should be well modularized to facilitate easy modifications.
Code Improvement Before and After: What's the Difference?
Firstly, let's look at the process of improving less readable code.
The code below returns a new list with each element of the original list squared.
numbers_list = [1, 2, 3, 4, 5]
def handle_numbers(numbers):
output = []
for num in numbers:
output.append(num ** 2)
return output
result = handle_numbers(numbers_list)
# Output: [1, 4, 9, 16, 25]
print(result)
This code functions correctly but lacks readability. It's not immediately clear what the handle_numbers
function does.
Now, let's improve this code.
numbers_list = [1, 2, 3, 4, 5]
# Using List Comprehension for conciseness
def square_numbers(numbers):
return [num ** 2 for num in numbers]
result = square_numbers(numbers_list)
The improvements made in the code above are as follows:
-
The function name was changed from
handle_numbers
tosquare_numbers
to clearly express the function's purpose. -
The code was made more concise using list comprehension.
-
Instead of using the
output
variable, the list is returned directly to simplify the code.
Improving the code in this way makes it easier to understand and modify later.
Improving Python Code Using Prompts
When using AI to improve code, it is important to write the prompt considering the characteristics of good code mentioned above.
The following Python function returns a new list with each element of the given number list squared.
def handle_numbers(numbers):
output = []
for num in numbers:
output.append(num ** 2)
return output
However, the function name does not clearly express its functionality, and the code is written verbosely.
Please improve this function to enhance code readability and make the code more concise.
When requesting code improvements like this, you should specify in the prompt that the code improvement should consider the attributes of good code such as readability, efficiency, reusability, and flexibility.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.