title: ‘Refactoring - like washing your hands in the bathroom (This Old Pony #59)’ layout: newsletter published: true date: ‘2018-08-14T10:45:00.000Z’

Do you schedule time to refactor your Django app? 

Uncle Bob on Refactoring

This quote from “Uncle Bob” Martin (of “Clean Code” and Agile Manifesto fame) got me thinking about strategies for _approaching _refactoring, to say nothing of issues around what _is _refactoring and why it’s good practice.

Of course, there’s a disconnect between _is _and _ought _as Josh points out below:

How people really approach refactoring...

Aside from being hilarious and [sadly] true, it paints a nice picture of what happens with refactoring: usually not much.

What is refactoring and why is it good

The canonical definition of refactoring is changing code in such a way that improves non-functional aspects of the code. _Renaming _is one of the most straightforward techniques for refactoring code, but it also includes breaking apart functions and methods, sometimes even class, or creating all new ones. 

There are arguments about what constitutes refactoring. Much of what developers talk about with regard to refactoring is well beyond what most of the authors on refactoring would admit as refactoring. Rewriting and redesigning are important tools for improving parts of codebases, but they’re well beyond refactoring.

Refactoring though is a good practice because it improves the code, obviously, right?. But it’s important, making these non-functional changes, because most working software is always changing. Features change, existing features are reused in different ways, a one-off class becomes a critical part of the codebase… Refactoring makes it easier to understand both what the software does, what it is intended to do, and makes it easier to work with it in a reliable way.

When to refactor?

When you’re working with the code in question!

That’s it. There’s no hard rule one way or the other, but it’s much like how to go about writing tests. If you have untested code you write tests for a block of code (or a function or a class) when you touch that block of code (or function or class).

Some modest suggestions for refactoring your Django app

If you’re unsure about what you’d refactor or where to start then use this list as your inspiration.


Management commands

Model methods




Encapsulatedly yours,

[0] https://twitter.com/unclebobmartin/status/1024254121338126336
[1] https://twitter.com/CodingItWrong/status/1024290384250331136
[2] Model instance cleaning: https://docs.djangoproject.com/en/2.1/ref/models/instances/#django.db.models.Model.clean
[3] The idea of template inheritance is a huge improvement over having to rely on includes for each rendered page to pull of the other necessary content in. However in some cases just using a conditional include makes more sense than a layer of thin base templates.
[4] with: https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#with