Well formatted Arduino code in one click

Warning, the title of this post is a little bit misleading. It isn’t just one-click to get cleanly formatted code, it’s actually two.

There are two things seasoned programmers will tell new programmers, that they don’t understand (at first):

  1. Always comment your code
  2. Properly indent code

The problem?  As a new programmer, you might not know how to do #2 until you get some experience.

How indenting helps

The reason you indent code is to make it easier for a reader to understand the code’s flow.  A compiler (well except in Python) doesn’t care if two lines line up or not.  The recent issues around Apple’s “goto fail;” are a great example of how indentation is meaningless to a C compiler.

Looking at the following example, it is pretty clear (to humans) that line 1 is a decision point and lines 2-3 belong with line.

if (sensorValue > 100) {
   warningFlag = true;

Most modern code development environments try to help with proper formatting, and the Arduino IDE is no different. However, the IDE isn’t proactive as some of the more professional-focused tools. For this reason, one of the least used, yet very powerful features of the IDE is hidden in the Tools menu.

The magic command won’t remove extra spaces between lines, but it’ll reformat some pretty messy things.

Arduino Auto-Format Example

A little Ctrl-T Magic

The image above shows how it takes the non-formatted code and turns it into something far, far more readable.

One Caution

The only thing i don’t like is how it handles “else” blocks.  I prefer to write my else blocks like this:

if (something) {
   // Do It!
} else {
  // Don't!

and it’ll format them into this:

if (something) {
   // Do It!
else {
  // Don't!

Not a huge difference and both are readable. For me, it is just personal preference to keep the braces (or brackets?) along with the else.

Where is it at?

How do you access this wonderful command?  It’s “hidden” in the tools menu.

Arduino Tools Window

The keyboard shortcut is Ctrl-T (Win/Linux) or Cmd-T (Mac) and it is under the Tools Menu.  Before I compile code, I almost always hit the keyboard shortcut to see the result.

Since I focused on the Arduino IDE, let me know if other environments offer this kind of clean up in the comments.

written by

Capacitor Expert by Day, Enginerd at Night. On paper I have a EE which I use to make things blink, beep, and fly. I created and enjoy making the AddOhms Tutorials at addohms.com. You can follow James on Twitter @baldengineer or on Google+.
Related Posts

2 Responses to "Well formatted Arduino code in one click"

  1. Dave Naves says:

    So glad you posted this… I’ve been formatting by hand for a year!


    Thanks much!

  2. Jerome Demers says:

    I was also not aware of that feature.

    Thank you


Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

©2014 Baldengineer Productions, Inc.