tekasian / intro-data-capstone-biodiversity

0 stars 0 forks source link

Use line breaks to increase readability #1

Open asadmehdi785 opened 6 years ago

asadmehdi785 commented 6 years ago

In Python, in general, we would like to keep the number of characters per line relatively small. In most cases, keeping it at or below 80 characters works fine, but if it's needed then we can go up to 100 characters. However, that's generally seen as a limit to how long a line should be.

https://github.com/tekasian/intro-data-capstone-biodiversity/blob/297a56430354b6ef4894b9f0bcaa2b78767381c9/Biodiversity_Capstone_Project_Bryan_Leung/biodiversity.py#L123

This line here seems to go over 100 characters. We can fix this in a number of ways, but typically if we want to put a line of code onto multiple lines in order to increase readability, we can use \ at the end of a line to break it up. Take a look at this code:

protection_counts = species.groupby('conservation_status')\
    .scientific_name.nunique().reset_index()\
    .sort_values(by='scientific_name')

It does exactly the same thing as the previous code, however this way it's a lot easier to read. It's good to keep this in mind while you're coding, because many times you will need to go back to your code or have another person work on the same file. Keeping things easy to read will help when to understand what your code is doing.

tekasian commented 6 years ago

I see. If a line seems to go over 100 characters, a \ is used at the end to make the code easier to read. Makes sense.