Exceptional coding style

Jonathan M Davis jmdavisProg at gmx.com
Fri Jan 18 01:01:55 PST 2013


On Friday, January 18, 2013 09:47:31 Jacob Carlborg wrote:
> On 2013-01-18 08:18, Jonathan M Davis wrote:
> > In _any_ language. Inevitably, the formatter ends up totally mangling at
> > least some of the lines. In my experience, any attempt to be super strict
> > with the formatting rules (as an automatic code formatter must be)
> > results in ugly code. A basic set of formatting rules helps the code be
> > consistent and look good, but there are always corner cases where the
> > rules must be bent or broken in order to make the code appropriately
> > legible. And it requires having a human do the formatting to get that
> > kind of flexibility.
> 
> Eclipse has a pretty darn good code formatter. It formatted the code
> exactly like I wanted to, except in one or two cases but that's usually
> because I broke my own rules.
> 
> Example, this is usually how I format a switch statement:
> 
> switch (value)
> {
>      case 1:
>          // code
>      break;
> 
>      case 2:
>          // code
>      break;
> }
> 
> But on occasion, when I have only a short single line expression and
> many cases I usually format it like this:
> 
> switch (value)
> {
>      case 1: a = 2; break;
>      case 2: a = 3; break;
>      case 3: a = 4; break;
> }
> 
> The formatter will break that.

But my point is precisely that there are times that you need to break the 
formatting rules in order to make the code look code and be appropriately 
legible. The formatter will probably do a fantastic job of being consistent, 
but that's the problem. _Most_ of the time, it will do the right thing, but 
there are times when you need to break the formatting rules, and that just 
doesn't work when a program is formatting the code for you.

I had to use eclipse's formatter at one of the places that I worked, because 
all commits had to follow, and I hated it. Code formatters are not the ticket 
to good looking code. Quite the opposite, in fact.

- Jonathan M Davis


More information about the Digitalmars-d mailing list