"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
- Brian W. Kernighan.
Many programmers like me always struggle, not to find a solution, but to format a code!
As programmers, we only find a 'space' and/or 'tab' button when formatting or indenting come into mind. This is true and we always practice it. Some heaven editors might be contain an inbuilt spacing adjustment too. But do these all so called solution give a proper result (of course, our satisfaction) ? The answer might be a long NO...because, most often, the real problem comes when the code has to migrate from one editor to another since, there is no strict Universal formatter rule followed by any editors.
Astyle (Artistic Style) is a 'God's own hand' solution to this problem. Artistic Style is a source code indenter, formatter, and beautifier which currently supports C, C++, C# and Java programming languages.
Artistic Style, actually a filter written in C++ that automatically re-indents and re-formats C / C++ / C# / Java source files. The main advantage of astyle is that, it can be used from a command line, or it can be incorporated as classes in another C++ program upon the use.
How to get astyle?
Well, the answer is very simple, just download and install it;)
The latest release can be downloaded from the SourceForge file releases page. A link to the "latest version" near the top of the page will download the correct package for your platform. Or you can select the appropriate package from the "astyle" release folders. The Windows package contains a compiled binary. Other platforms will need to compile the source code. Makefiles are included for the most popular compilers. Follow the install instructions for the appropriate platform as described in the "Install" documentation.
For Ubuntu users,just get it via terminal by,
apt-get install astyle
Once it installed properly, then rest of everything is pretty easy.
How to format with astyle?
- The first and foremost step is to right a small program (without code, where we will format? :P). I wrote a ugly program in vim editor (one of my favorite editor). Examples hence with the same. But the procedure to run the astyle is almost similar and simpler in all variants. Do it according to your platform.
Sample program at initial stage without applying astle
- Once the coding getting over, the next step is to beautify the code (usually we should do normal indentation while coding itself, but nothing to worry here even if you missed out this, because astyle is always free to help you).
Astyle can be running either from a terminal or as class. Here, I'm trying it from the terminal itself. Astyle can be run as follows
- Our sample program is formatted successfully! open the program and see the difference...
Code after astyle format
Pretty nice, isn't it? That too with just a command line step!
Note that the astyle will indent and format the code automatically with the ANSI standard. You can specify the standard and can use many inbuilt features by using the 'help'.
Astyle will not format the comment lines since it users only freedom area to express the meaning of what they are doing here ;) . So remember when do coding and taken a care about the comments. Rest of master pillars will be taken care by the Astyle...