Introducing Sourcery


A high quality code base is a joy to work with. It's easy to understand, easy to add new features and easy to track down issues. Unfortunately, time pressure and the need to maintain legacy systems means that we encounter bad code far too often, and it's hard to find enough hours in the day to refactor.

We think that it should be as simple as possible to create brilliant code, whether writing from scratch or updating what's already there. That's why we've spent the last eighteen months building Sourcery.

Sourcery

Sourcery is a brand new tool that uses AI to automatically refactor Python code, making it dramatically more readable and maintainable. It integrates into your IDE, making it super easy to use. We're going into open beta later this year and are excited to share what we're doing and to hear what you think.

So how does Sourcery work? Let's take a look.

Code example

Here's a small example of some code that could be tidied up.  The yumminess variable is declared but not used immediately. If it is moved inside the if condition then that condition can be merged with the previous one. Sourcery detects that this change can be made and highlights the code.

You can then apply the change straight away.

Code example after refactoring

Sourcery also gives you a diff so that you can see exactly what has been suggested, which is particularly useful for more complex refactorings.

Unlike existing refactoring tools it uses AI to dramatically simplify and improve code in one step, with no human input required.

More complex refactoring example
Code before and after refactoring by Sourcery

The important thing here is that Sourcery won't change the functionality of the code. Our advanced code analysis ensures that the suggested refactorings maintain correctness - the code will still do exactly what it did before.

A great example is the Gilded Rose refactoring kata. This is a refactoring exercise where you have to clean up some very hard to understand spaghetti code.

Gilded Rose refactoring kata
Gilded Rose refactoring kata

You may have seen this before, or even had a go at refactoring it yourself. It's a challenging exercise that can take quite some time to get right, especially if you need to create unit tests first.

If you haven't seen it before, it's worthwhile looking over the code and having a think about what it does, and how you might refactor it. For instance, what updates does it do if item.name == "Sulfuras, Hand of Ragnaros"?

Here's what happens when we apply Sourcery:

Gilded Rose refactored by Sourcery
Gilded Rose refactored by Sourcery

As you can see this is much clearer. The "Sulfuras" item has been moved into a guard clause, and the others are now in a clear, switch-like statement. The update to item.sell_in has also been separated from the updates to item.quality.

These are major improvements, and they took Sourcery's cloud-based server less than two seconds to refactor.

We've been using Sourcery internally and it's incredibly useful. When you're hacking out new features Sourcery can help you clean them up ready for commit. Alternatively when faced with some complex legacy code it can transform it and make it easier to understand without changing its functionality.

We're continuing to optimise and improve the product, ready to launch into open beta in Q3 this year. We currently have plugins developed for IntelliJ and Vim, with more in the pipeline.

If you'd like to hear more, please subscribe to our newsletter, or contact us on Twitter or LinkedIn. We're excited to hear what you think!