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):
Always comment your code
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) {
Serial.println("Warning!");
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.
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.
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.
Fan of making things beep, blink and fly. Created AddOhms. Writer for Hackster.io News. Freelance electronics content creator for hire! KN6FGY and, of course, bald.
Using Linux 16 and Chromebook, with early version of Arduino installed with Crouton, Auto Format not only realigned the text but also told you if you were missing a { or } or had too many } or {.
The newer version of Arduino on Gallium OS installed with Chrx allows only formatting the text without telling you the {} status.
This is a minor point but I used the Auto Formatter as a trouble shooting tool.
Is there any way of modifying the formatter tool to make this check for {} using Linux or is the formatter only realigning text on new versions of Arduino?
The logic is that you put “else” exactly below the “if” to which it belongs, so the block of statements under “if” clearly “belongs” to the block of “else” statements.
I think this comes from the way that block structured programming was taught in the late 70s/early 80s. Working in pseudocode and/or Pascal, as we did in those lessons, you would (be told to) use a lot of indentation and vertical space:
if (test) then
begin
indented lines of code to show what the "if" was doing
end
else
begin
indented lines of code to show what the "else" was doing
end
Lots of space used, but you knew what belonged with what! (and so did your teacher, it’s far easier to debug.)
Nobody seems to format code like that nowadays, all been reading K&R and programming html, so putting a squiggle straight after the “if” test, making it less obvious where the end squiggle belongs, seems like a good idea because they’ve always done it.
What the IDE lacks, or I can’t find, is the character position in the line – i.e.,, you get the line number down the bottom of the page, but not the character position.I tried Auto Format once and it made a complete hash of it. Notepad++ is much clearer when it comes to finding the closing bracket of a long if statement and I like the tab positions being shown. If you have a long sketch thrown together like mine, you tend to lose track of how many nested ifs, fors etc you have. I’ve just tried Auto Format again on some expendable code and if you line up everything on the left-hand side, then use Auto Format, it works quite well, assuming of course you haven’t missed out a few closing brackets.
Thank you for pointing it out, I didn’t know the Arduino IDA had it.
I don’t know which types of IDE you mean, but the bigger ones like Visual Studio, Eclipse or Netbeans do that.
I work a lot with Eclipse and there you can set formatting as a “save action”, this means, your code is automatically formatted while saving. Also, you can set the formatting behaviour, so like you said the if-else thing (to be on separate lines or not), if there should be blanks beneath parentheses
(like func( param ) or func(param) or func (param)) and whatnot else. You can also make missing comments on classes, public constants and functions cause a warning!
Like other websites, this one uses cookies to remember things. Mostly, I use Google Analytics to know how many people come here. ¯\_(ツ)_/¯
By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-advertisement
1 year
Set by the GDPR Cookie Consent plugin, this cookie is used to record the user consent for the cookies in the "Advertisement" category .
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
CookieLawInfoConsent
1 year
Records the default button state of the corresponding category & the status of CCPA. It works only in coordination with the primary cookie.
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Cookie
Duration
Description
language
session
This cookie is used to store the language preference of the user.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Cookie
Duration
Description
_ga
2 years
The _ga cookie, installed by Google Analytics, calculates visitor, session and campaign data and also keeps track of site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognize unique visitors.
_ga_LHR6J24XSY
2 years
This cookie is installed by Google Analytics.
_gat_gtag_UA_42726312_1
1 minute
Set by Google to distinguish users.
_gid
1 day
Installed by Google Analytics, _gid cookie stores information on how visitors use a website, while also creating an analytics report of the website's performance. Some of the data that are collected include the number of visitors, their source, and the pages they visit anonymously.
browser_id
5 years
This cookie is used for identifying the visitor browser on re-visit to the website.
CONSENT
2 years
YouTube sets this cookie via embedded youtube-videos and registers anonymous statistical data.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Cookie
Duration
Description
VISITOR_INFO1_LIVE
5 months 27 days
A cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.
YSC
session
YSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.
yt-remote-connected-devices
never
YouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
yt-remote-device-id
never
YouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
yt.innertube::nextId
never
This cookie, set by YouTube, registers a unique ID to store data on what videos from YouTube the user has seen.
yt.innertube::requests
never
This cookie, set by YouTube, registers a unique ID to store data on what videos from YouTube the user has seen.
10 Comments
Using Linux 16 and Chromebook, with early version of Arduino installed with Crouton, Auto Format not only realigned the text but also told you if you were missing a { or } or had too many } or {.
The newer version of Arduino on Gallium OS installed with Chrx allows only formatting the text without telling you the {} status.
This is a minor point but I used the Auto Formatter as a trouble shooting tool.
Is there any way of modifying the formatter tool to make this check for {} using Linux or is the formatter only realigning text on new versions of Arduino?
The logic is that you put “else” exactly below the “if” to which it belongs, so the block of statements under “if” clearly “belongs” to the block of “else” statements.
I think this comes from the way that block structured programming was taught in the late 70s/early 80s. Working in pseudocode and/or Pascal, as we did in those lessons, you would (be told to) use a lot of indentation and vertical space:
Lots of space used, but you knew what belonged with what! (and so did your teacher, it’s far easier to debug.)
Nobody seems to format code like that nowadays, all been reading K&R and programming html, so putting a squiggle straight after the “if” test, making it less obvious where the end squiggle belongs, seems like a good idea because they’ve always done it.
Comments strip out a lot of formatting.
This might have worked:
Yup… that does work. Fortunately, the formatting you did gets stored, just not displayed. I edited your original comment.
Thank you. It wasn’t obvious that html would have worked, and I’m rubbish at html, anyway!
Delete this at your own pleasure after reading!
What the IDE lacks, or I can’t find, is the character position in the line – i.e.,, you get the line number down the bottom of the page, but not the character position.I tried Auto Format once and it made a complete hash of it. Notepad++ is much clearer when it comes to finding the closing bracket of a long if statement and I like the tab positions being shown. If you have a long sketch thrown together like mine, you tend to lose track of how many nested ifs, fors etc you have. I’ve just tried Auto Format again on some expendable code and if you line up everything on the left-hand side, then use Auto Format, it works quite well, assuming of course you haven’t missed out a few closing brackets.
Ctrl + F in eclipse = Ctrl+T in Arduino
Thank you for pointing it out, I didn’t know the Arduino IDA had it.
I don’t know which types of IDE you mean, but the bigger ones like Visual Studio, Eclipse or Netbeans do that.
I work a lot with Eclipse and there you can set formatting as a “save action”, this means, your code is automatically formatted while saving. Also, you can set the formatting behaviour, so like you said the if-else thing (to be on separate lines or not), if there should be blanks beneath parentheses
(like func( param ) or func(param) or func (param)) and whatnot else. You can also make missing comments on classes, public constants and functions cause a warning!
thanks.it worked..
I was also not aware of that feature.
Thank you
So glad you posted this… I’ve been formatting by hand for a year!
=)
Thanks much!
//D