Yan Holtz logo โ† All issues
Dataviz Universe

0.3 seconds to make your code clean!

As a software engineer and educator, I spend a lot of time watching beginners write code.

And one thing always stands out: how slow it can be. Not because of logic or bugs, but because of typing & formatting!

So today I suggest we talk about formatters.

If you're not using one yet, it's an easy win that will instantly boost your productivity.

And if you already are, read on: there's some exciting news you'll want to know.

The problem

Without a formatter, two things usually happen:

First code is often hard to read. Take this R code snippet: it works, but it's a headache to read:

Poorly formatted R code

Indentation is bad, spacings don't make sense, case is terrible and lines are too long. People usually realise this which leads to the second problem: wasting time manually cleaning it up.

Fiddling with spaces, adding line breaks, fixing tabs... You eventually get something cleaner, but at the cost of precious minutes. And probably not with a perfect result!

Cleanly formatted R code

The solution: formatters

No matter if you code in R, Python, or JavaScript, you should absolutely be using a formatter.

A formatter is a little tool that handles the layout of your code: line breaks, indentation, spacing. It takes a set of standard styling rules and apply them automatically on your file.

It doesn't change what your code does, just how it looks. Hit a keyboard shortcut or click a button and boom: clean, readable code in an instant. ๐Ÿ”ฅ

Format on save

The best way to use a formatter is to set it to run every time you hit ctrl+s to save your file. We call this feature "format on save".

In R, the styler package has long been the go-to formatter, following the tidyverse style guide.

It works well, and I explain how to use it in my Productive R Workflow course. But styler is a bit slow and the Format On Save feature was not well supported.

There's a new tool that has just been released that fix those issues. It's called Air.

It's blazingly fast. Formatting the entire dplyr codebase takes just 0.3 seconds! ๐Ÿ˜ณ

Note: you know how much I love Quarto. Air does not work with Quarto documents in R Studio. But it does work if you use Positron or VS code.

Javascript and Python

The same principle applies to Python and JavaScript too!

With Javascript, Prettier is usually the go-to formatter.

In Python, Black was one of the standard for a long time, but Ruff is definitely the way to go now, as it is much faster!

Conclusion

Using the right tools is one of the best investments you can make in your developer journey.

The seconds you waste on formatting manually every day add up quickly. ๐Ÿ“ˆ Minutes per day, days per year. Don't let that time go to waste. ๐Ÿšจ

Make the switch. There is no coming back!

PS: I'm updating my Productive R Workflow project to replace styler with Air in the coming days!

PPS: The students of Matplotlib Journey keep doing amazing work ๐Ÿ˜€. Would you know how to do that with matplotlib? (code here)

Matplotlib chart made by a student

Cheers,
Yan

Enjoyed this?

Get one short, actionable dataviz tip every Saturday. Join 19,000+ data people.