Skip to content
TURNKEYGNU / LINUX
AppsDocumentationBlogScreenshotsGitHubTurnKey Hub
Home/Blog/My code refactoring algorithm

From the project

My code refactoring algorithm

By Liraz Siri2 May 20112 min read
developmentpythoninsightrefactoring

You're looking at a block of Python code. It's not immediately obvious what it does. It's sort of a mess and you realize it needs to be refactored. But how? What mental algorithm do you use?

Here's a trick I just figured out how to put into words. You first add comments to your code to make it easier to explain what it's doing in each part. Then you restructure the code until those comments are no longer necessary, using the comments as a guide to creating high level constructs that not only tuck complexity neatly away, they also explain the purpose of the surface.

What tricks do you use? 

Comments (2)

Those are good rules of thumb

Reply 1 2011-05-04
Implementing the "common guidelines" will bring you a long way, but I find that often isn't enough. The point I was trying to make is that truly readable code shouldn't need any explanation / comments. If it does, I take that as a signal that something is wrong.

Version control essential

Reply 2 2011-05-04
I started using version control about a decade ago (RCS integrated with Emacs) and found it immediately improved my productivity. By a lot. Suddenly it was much easier to spot exactly what I had changed since the last revision. Nowadays I try to make all my commits self-contained and self-documenting. Thankfully I don't use RCS any more. Git is much nicer.

Keep exploring

Find your next server

Browse ready-to-use appliances by the job you need to do.

Explore the app library
TURNKEYGNU / LINUX

Own your infrastructure without starting from scratch.

Explore TurnKey
AboutDocumentationFAQBlogGitHub
Free and open sourceSecure by defaultReady to deploy