Do One Thing At a Time
A common mistake many beginners make is writing too long and complicated functions. It is always recommended to design functions to only perform one specific task. Small and precise functions are easy to test and debug with modern IDEs and will be flexible.
Now, you might be thinking: ‘I never had to spend more than 2–3 minutes if my code generates an error. I could always solve them with simple
If you still think like that, try to write a script/program that actually works with at least 200 lines long, and then we will talk. In the meantime, consider this function:
First of all, the docstring does give a good description of the function. If we spend some time reading the code, we will realize that its main purpose is to read a
csv file from
path argument and subset it using
country argument and return top 25 most populated cities of that country. If you pay attention, the main purpose of the function is done in a single line (right after the second comment). The other lines are performing cleaning tasks that are not very clear.
What would be ideal is to break this function up so that all the cleaning is done in one chunk and subsetting for 25 cities in another. Let’s start with cleaning:
In the above function, I used
.rename method to rename the
lng column to
lon. In the original, dirty function, a new column was created and the old one was dropped which was unnecessary.
Next is to create another function that subsets for top cities of a given country:
This function is better than the original function because I also inserted exception handling logic for when there is no matching country in the data.
Both of the new functions contain better documentation and follow best practices.
Please write your functions in this manner so that others won’t bleed from the eyes searching for your