a program that tidies up Java source code to some standard format. I does not rigidly follow Sun’s conventions,
but it is quite close.
Advantages of Beautifiers
- Beautified code is consistent and hence easier to read.
- You will save tens of thousands of keystrokes you would otherwise spend compulsively aligning and tidying code manually.
- Beautifiers are 100% consistent. Even team members who have not an anally retentive bone
in their bodies will turn out perfectly formatted code. Even fastidious you can never achieve perfect consistency with
purely manual alignment.
- Beautifiers stop fights in teams where team members have their own preferred style they don’t want anyone else to
meddle with. To be a good computer programmer you must be borderline OCD (Obsessive Compulsive
Disorder). This means likely you will develop stylistic formatting quirks that are very
important to you, but are meaningless to others. Others can’t follow them even when they want to. Those
stylistic details that are so glaring to you, sail right over their heads. Further it just irritates them to be bullied
into following “meaningless” rules, as if you were telling them they can’t walk on sidewalk cracks.
Team members are irked walking on eggs, trying to mimic every other team member’s style when working on "their"
code. Separate idiosyncratic styles for separate classes encourages territorialism.
- You settle on a corporate style for checkin, and team members are free to beautfy to their personal style when they edit
or view a piece of code. They automatically beautify back to corporate standard for checkin. You can make the standard
as elaborate and quirky as you like. It does not interfere with anyone’s work. Because it does not matter, there
is far less reason to fight over what it should be. The boss can have it 100% his way,
and expect 100% compliance.
- Beautifiers get rid of most false deltas on CVS checkin. Since spacing is beautified to a standard, you don’t get
CVS deltas registered by a stray space. As part of beautification, I collapse runs of blank lines down to one to avoid
that type of false delta.
- If you change your mind on what constitutes the optimal format, you can do a bulk beautify on all code you have ever
written. That is deeply satisfying to the obsessive perfectionist.
- Beautifiers prevent deceptive code, code that is indented to look like it does one thing, but actually does something
else.
- Beautifiers can be helpful in detecting bugs such as unbalanced or improperly nested {} [] (). You beautify, and look at
the beautified indent structure to detect anomalies. If you insist on only manually aligning code, you deprive not only
yourself, but all team members of this useful tool.
Disadvantages of Beautifiers
- It is an extra step. If you forget, you will create hundreds of false CVS deltas on checkin.
- You can never find a beautifier that will do things exactly the way you would if you did them by hand with
infinite time.
- Beautifiers often have bugs. This means they may break lines inappropriately, introduce white space or add superfluous
and ever growing blank lines.
- They are not manly. Real men do everything manually no matter how much time it wastes.
- Be careful when reordering declarations to make code pretty. You can often disturb the order of static or instance
initialisation and stop it working. I broke BigDate this way allowing Visual Age to sort all
my declarations alphabetically. It really had me scratching my head.
The Ideal Beautifier
I have not yet found a beautifier that is sufficiently anally-retentive about spaces that you never get spurious CVS
deltas. You should be able to control how much space there should be before and after () [] ; + - = in various contexts,
e.g. method call, cast, empty declaration, nested () depth, /* // etc.
Ideally a beautifier should work two ways:
- Inside your editor just a click away.
- As a standalone utility so you can batch beautify and routinely beautify before adding to CVS. You want spacing
absolutely standard, no judgement at all.
- You need a beautifier for each type of text you use in your project such as Java, JSP, HTML, CSS, XML, SQL…
Trita was unusual in that it woould beautify Java, JSP, HTML, CSS, PHP, JavaScript and even
C#. It learned your preferred style by looking as a sample of your formatted code.
A beautifier does not have to understand the full syntax of Java and it also has to handle code with syntax errors. This
makes it somewhat simpler than a full parser. In other words, to get the beautifier exactly the way you want, you may
have to write it yourself.
Any modern IDE will beautify code, e.g. Eclipse, Netbeans or IntelliJ Idea. I use Idea, which has two highly
configurable beautifier plugins called Rearranger and code Formatter. For tiny projects I use SlickEdit
since it is integrated into the editor, where it is convenient to type quickly/sloppily then beautify frequently.