The Hidden Dangers of Sloppy Python Code



June 4, 2021
Code Quality

I recently took a course with Uncle Bob Martin of Clean Coders. I have long believed that clean code provides the foundation that makes your web site run well. Without clean code, your web site will become stuck in the mud, unable to grow with new features.

One thing that Uncle Bob said is very relevant to hiring freelancers to work on your project. He quoted Kent Beck as saying that software development is a three-step process:

First make it work, then make it right, then make it fast

The problem is that some freelance developers stop at the first step. Many organizations are only capable of grading the first step.

The Problem with Stopping at “Make it Work”

It’s possible to hammer away at the keyboard, refreshing the browser until errors go away. Rather than fix a sloppy mess the developer can say ‘Yes! Good to go.’ and move on. But with the sloppy mess that is left behind, the organization takes on technical debt. This can include:

  • Vulnerability to bugs/errors
  • Security flaws
  • Performance issues
  • Inability to add features or grow beyond current capability

Identifying Clean Code

Considering python projects such as Django and Flask, here are some key elements that signal a project has moved towards ‘make it right’:

  • Project files are organized in a standard way, such that things are where you expect them to be
  • Code is written in a standard such as PEP8, or uses an automatic formatter such as Black
  • Tests are in place
  • Very minimal duplicated code
  • Functions are short
  • Classes have high cohesion (share data between the various functions of the class)
  • Classes, functions, variables named in a way that makes sense
  • Comments where appropriate

Most important of all, clean code looks like it was written by somebody that cares. Programming in general is difficult, so while code can always be optimized, you can tell when someone gave it their best effort and tried to make their code understandable to others. You can also tell when it is sloppy and rushed.