Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only want to ever see Allman style code and save K&R style code

One of our developers decided he'd dictate to other developers how people should format their source code.

Myself, I'm a firm believer that Allman style indentation is better than K&R.

Formatting styles is like religion, there are always those that disagree. No matter how much evidence is shown that one is better than the other, people will not accept it.

So what I want to do, is every time I open a file in Eclipse, I have it format the source code, so that I can look at pretty code. Every time I save, it should format to the dictated style in the office.

I think I'm ok with doing this manually and having two hot-keys for it. Ctrl-Alt-PgUp: my way Ctrl-Alt-PgDown: high way

The hot key option is probably good because then I get to verify all is well instead of it happening invisibly behind the scenes.

Eclipse has a single format option. What I need is two formatting styles and map them each to a shortcut key. Is that possible, and if so, how?

like image 232
Mike Avatar asked Jul 29 '11 17:07

Mike


3 Answers

With all due respect to Mike, if you can't beat them, join them. Indeed, learn to tolerate ugliness, as djna has advised. I give this advice after having spent an entire career quietly fighting this battle.

I believe that Allman-style coding is better in several meaningful, significant ways, starting with code alignment and visibly clarifying blocks of code. Allman-style people tend to be those that favor/tolerate more use of vertical whitespace to help clarify which things go together and when a new step or phase has begun.

From my own anecdotal research, the main advantage to K&R most often cited, other than homage to earlier precedent, is that K&R is a style favored by those that like to see more code on one screen. Yes, it is true, that's the main reason. Code density. I thought this issue would have died its final death once we got past the age of 80 x 25 character console screens in the early 90's, but alas, but it seems not to have mattered. So for those that like code density so they can see more code on one screen at a time, we suffer with millions of lines of K&R style code. * Sigh *

Bottom line: You can't win a religious war. It's not worth the effort. When in Rome, do as the Romans do and just learn to live with what is. This is an issue that strikes us psychologically, because it is kind of about Power and Control. But we all understand that some things we simply don't have legitimate power or control over, so let those things be and learn to adapt. That is truly the right way to approach your software development career.

like image 66
bearvarine Avatar answered Nov 19 '22 21:11

bearvarine


Well, it seems like the age-old debate is back. Unfortunately, Eclipse JDT UI does not support applying different code formatters yet.

There's bug no 45423, which is intended to resolve this. As long as that bug remains unfixed, the best workaround would be applying a formatter in a commit hook as each project is associated with it's own formatter; you cannot even choose a different formatter to apply on the file save action (Preferences -> Java -> Editor -> Save Actions). Honestly, I don't think shortcut keys would even work.

Also, it might be worth investigating the use of the Questoid Code Formatter, which appears to be the closest (or only) plugin to support your need. I haven't attempted using it; I've only read the description from Eclipse Marketplace.

like image 23
Vineet Reynolds Avatar answered Nov 19 '22 21:11

Vineet Reynolds


My advice: just learn to tolerate ugliness. Unless your "compare with history in SCM" function is really good it's all too easy to get spurious differences when formatting.

like image 6
djna Avatar answered Nov 19 '22 22:11

djna