“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”

Martin Fowler

We all know that during the rush of the development process, we are generally more focused on making our program work than making it fully readable. This situation becomes even more complicated when we face a problem never seen before and have a tight deadline to deliver the work. Sometimes we have to appeal to Stack Overflow to find a solution or take some time to read all similar questions until finding a new idea of how to work around the problem.

“When your program is a complete mess, but it does it’s job.”

Image for post

Source: kéké at tumblr

I use Stack Overflow a lot, but at least for me, the time I spent looking for a solution reduced the time (a hard and long one) I generally take to make my code cleaner and readable. If this happens to you too, maybe it’s because we are focusing more on “how to solve” than makes it accessible to other developers, well…** we want to make it work! **And even though we had time to format and document some things, who never spent a few hours trying to understand some code from years ago? The reason for this is simple, writing clean and readable code is a hard and tiring thing, but we should always think about the following sentence from the great “Uncle Bob”:

“The ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. …[Therefore,] making it easy to read makes it easier to write.”

_Robert C. Martin, _Clean Code: A Handbook of Agile Software Craftsmanship

We must put in our minds that as developers, programmers, software engineers, data scientists, and so on, our real audience is not computers, but other programmers (including ourselves). As the sentence from Uncle Bob defines, we usually spend more time reading documentation or other people code than making new ones, so why not spend more time on this part (however tiring it may be) and help you or others in the future?

Image for post

Source: Thom Holwerda at OSNews Comics

It will not only make you a better programmer, but it will also help with the scalability and maintainability of your product, also reducing the number of bugs (this is real) and the system complexity/risks reduction to changes or additions. If none of this is enough for you, I can give you one more thought to change old habits!

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”

John F. Woods

Let’s see some ways on how to do this then? Below is a summary of some methodologies to achieve high-quality and clean code.

Refactoring

Image for post

Source: Randall Munroe at XKCD

Refactoring is a way to restructure your code to improve its internal structure without changing its external functionality. The mindset behind this is: Did you manage to make it work? Go back to the beginning, clear, and modularize your program! It may seem like a waste of time to do this right at the beginning when you have several features to add, but doing this at steps will give you the following advantages:

  • Reduce workload in the long run;
  • Easier to maintain the code;
  • Increase reusability;
  • Decrease the time it takes to do this in the future or at new projects (the more you do, faster you will become in this activity)
  • If you try to do a better job than the previous refactoring, you will surely master this skill soon;
  • This skill is highly valuable in the job market and will highlight your profile (just look at “Desired to Have” Job Posts at LinkedIn or Indeed)

OK! I understand the advantages of refactoring, but how do I do this? It’s simple, here are some ways to achieve this:

If you want to go deeper into the topic, here is an excellent article that explains step by step how to achieve refactoring in Python, and here is a compilation of some Code Metrics available! If you prefer books, I advise the following:

Some good video lectures or workshops:

#data-science #refactoring #software-development #python #clean-code #visual studio code

Modular: The “Must” Foundations to Improve your Python Code and Carrer
1.45 GEEK