Motivation

Have you ever looked at a function you wrote one month earlier and found it difficult to understand in 3 minutes? If that is the case, it is time to refactor your code. If it takes you more than 3 minutes to understand your code, imagine how long it would take for your teammates to understand your code.

If you want your code to be reusable, you want it to be readable. Writing clean code is especially important to data scientists who collaborate with other team members in different roles.

You want your Python function to:

  • be small
  • do one thing
  • contain code with the same level of abstraction
  • have fewer than 4 arguments
  • have no duplication
  • use descriptive names

These practices will make your functions more readable and easier to detect errors.

Inspired by the book _Clean Code: A Handbook of Agile Software Craftsmanship _by Robert C. Martin with code examples written in Java, I decided to write an article on how to write clean code in Python for data scientists.

In this article, I will show you how to utilize the 6 practices mentioned above to write better Python functions.

#python #code #programming

Python Clean Code: 6 Best Practices to Make your Python Functions more Readable
3.55 GEEK