Code beautifiers, anyone?

Hello people,

I'm looking for the best ways to create a line of code beautification (reformatting) scripts -- one for C, one for Ruby, one for Bash and one for web development languages like XHTML, XML, CSS, PHP and Ajax. Whether as frontline warriors or household maids, they would ensure proper indentation, linebreaks, spaces, tabs and so forth.

Can anybody help me?

My studies of architectural science has taught me to pay extreme care to the correction of details, and I now wish to apply these teachings to all my code. I find myself always reformatting whatever my associates give me. Not that they're bad programmers, they just care more about the code itself rather than its structure, and I dare not argue with that. When their code is messy, however, my heart feels messy and I can't get any sleep.

I wish to be in full control of my code beautifiers. That is, I wish to have them as simple and meaningful as possible. Give me an easy Bash over a complex Ruby any day.

There's a lot of messed up tools out there. Companies with flashy websites just doing this for the money. So apart from the bullshit, I've managed to spot out the Ruby Beautifier and GNU Indent as two worthy code beautifiers. However I get the feeling they are more complex than they ought to be, and if less is more, my search will have to continue.

All the best,
Kyrre

} I'm looking for the best ways to create a line of code beautification
} (reformatting) scripts -- one for C, one for Ruby, one for Bash and
} one for web development languages like XHTML, XML, CSS, PHP and Ajax.
} Whether as frontline warriors or household maids, they would ensure
} proper indentation, linebreaks, spaces, tabs and so forth.
[...]
} I wish to be in full control of my code beautifiers. That is, I wish
} to have them as simple and meaningful as possible. Give me an easy
} Bash over a complex Ruby any day.
[...]

vim

} All the best,
} Kyrre
--Greg

···

On Thu, Aug 24, 2006 at 09:58:16PM +0900, Kyrre Nyg?rd wrote:

I agree strongly with the importance of having clean,
readable code. It's more pleasant to work with and
also wastes less of the programmer's time in trying
to understand what's going on.

The Perl community has two interesting developments
in this direction. First, there's Damian Conway's
book, "Perl Best Practices" (O'Reilly). I don't
agree with EVERYTHING Damian says, but his reasoning
is worth reading, in any case, and I think his book
will be quite influential.

Following his lead, Jeffrey Thalhammer has written

  Perl::Critic
  http://search.cpan.org/~thaljef/Perl-Critic-0.19/lib/Perl/Critic.pm

This Perl module/program applies a customizable set
of tests to a set of code, reporting discrepancies.

Unfortunately, Ruby has neither the book nor the tool
at this point, but it certainly could have...

-r

···

--
http://www.cfcl.com/rdm Rich Morin
http://www.cfcl.com/rdm/resume rdm@cfcl.com
http://www.cfcl.com/rdm/weblog +1 650-873-7841

Technical editing and writing, programming, and web development

Kyrre Nygård wrote:

Hello people,

I'm looking for the best ways to create a line of code beautification (reformatting) scripts -- one for C, one for Ruby, one for Bash and one for web development languages like XHTML, XML, CSS, PHP and Ajax. Whether as frontline warriors or household maids, they would ensure proper indentation, linebreaks, spaces, tabs and so forth.

Can anybody help me?

My studies of architectural science has taught me to pay extreme care to the correction of details, and I now wish to apply these teachings to all my code. I find myself always reformatting whatever my associates give me. Not that they're bad programmers, they just care more about the code itself rather than its structure, and I dare not argue with that. When their code is messy, however, my heart feels messy and I can't get any sleep.

I wish to be in full control of my code beautifiers. That is, I wish to have them as simple and meaningful as possible. Give me an easy Bash over a complex Ruby any day.

There's a lot of messed up tools out there. Companies with flashy websites just doing this for the money. So apart from the bullshit, I've managed to spot out the Ruby Beautifier and GNU Indent as two worthy code beautifiers. However I get the feeling they are more complex than they ought to be, and if less is more, my search will have to continue.

All the best,
Kyrre

Hi Kyrre,

This topic has come up before...I see you found some projects focused on this. However, while indentation is pretty important and useful, most text editors will do this for you. That leaves spaces, tabs, linebreaks, and those kinds of things. Unfortunately, there are many times where people disagree about Ruby formatting.
For example,

a.each {|item| ... }

or

a.each { |item| ... }

? Which is right/better? Depends on who you ask.

I know this is a simple example, but you will end up having to make those kinds of decisions.
Just a caution, that's all. Good luck with your project. I'm not sure what kind of help you wanted?

-Justin

Kyrre Nygård wrote:

Hello people,

I'm looking for the best ways to create a line of code beautification
(reformatting) scripts -- one for C, one for Ruby, one for Bash and
one for web development languages like XHTML, XML, CSS, PHP and Ajax.
Whether as frontline warriors or household maids, they would ensure
proper indentation, linebreaks, spaces, tabs and so forth.

Can anybody help me?

I have written a relatively primitive Ruby beautifier (written in Ruby) that
probably could be made ten times better if someone cared to expend the
effort:

http://www.arachnoid.com/ruby/rubyBeautifier.html

There are many things it won't do, some of which are mentioned in this
thread. But it reliably indents and reveals the basic program structure.

It's GPL, so someone could take it and run with it. I myself am too lazy.

···

--
Paul Lutus
http://www.arachnoid.com

Please explain yourself Greg?

Thanks,
Kyrre

···

At 15:14 24.08.2006, Gregory Seidman wrote:

On Thu, Aug 24, 2006 at 09:58:16PM +0900, Kyrre Nyg?rd wrote:
} I'm looking for the best ways to create a line of code beautification
} (reformatting) scripts -- one for C, one for Ruby, one for Bash and
} one for web development languages like XHTML, XML, CSS, PHP and Ajax.
} Whether as frontline warriors or household maids, they would ensure
} proper indentation, linebreaks, spaces, tabs and so forth.
[...]
} I wish to be in full control of my code beautifiers. That is, I wish
} to have them as simple and meaningful as possible. Give me an easy
} Bash over a complex Ruby any day.
[...]

vim

} All the best,
} Kyrre
--Greg

Kyrre Nygård wrote:
> There's a lot of messed up tools out there. Companies with flashy
> websites just doing this for the money. So apart from the bullshit,
> I've managed to spot out the Ruby Beautifier and GNU Indent as two
> worthy code beautifiers. However I get the feeling they are more
> complex than they ought to be, and if less is more, my search will
> have to continue.

a.each {|item| ... }
or
a.each { |item| ... }
? Which is right/better? Depends on who you ask.

Yes, that is probably a problem for someone who wants to create a code
formatter/beautifier. Eclipse's one (for java) is a very good and configurable
one, IMHO. Maybe you should take a look at it. Of course, not to include
ALL of the features it has (we already learned that sometimes convention is
better than configuration, right?), but to see what they let you
configure. Maybe
it can help you figure out what is good to let the user configure and what is
better to let as a convention.

Good luck!

Alvim.

···

On 8/24/06, Justin Collins <collinsj@seattleu.edu> wrote:

Hello,

Yes you are absolutely right. I have to put a lot of thought into decisions like that, and possibly convert the pieces of code into ontologies which I then visualize using modern 3D technologies to see which solution seems more semantic.

I understand what you mean by your example. No matter what though, the most logical solution always wins. Forgive me if I'm wrong, as I'm not familiar with every premise that might come into play, but wouldn't be the latter be more correct?

Thanks a lot Justin!

All the best,
Kyrre

···

At 19:38 24.08.2006, Justin Collins wrote:

Hi Kyrre,

This topic has come up before...I see you found some projects focused on this. However, while indentation is pretty important and useful, most text editors will do this for you. That leaves spaces, tabs, linebreaks, and those kinds of things. Unfortunately, there are many times where people disagree about Ruby formatting.
For example,

a.each {|item| ... }

or

a.each { |item| ... }

? Which is right/better? Depends on who you ask.

I know this is a simple example, but you will end up having to make those kinds of decisions.
Just a caution, that's all. Good luck with your project. I'm not sure what kind of help you wanted?

-Justin

A lot of respect to you for creating it man, it's a beautiful script and it has taught me a lot. I will do my best to further evolve it. Thanks a lot.

All the best,
Kyrre

···

At 22:10 24.08.2006, Paul Lutus wrote:

I have written a relatively primitive Ruby beautifier (written in Ruby) that
probably could be made ten times better if someone cared to expend the
effort:

http://www.arachnoid.com/ruby/rubyBeautifier.html

There are many things it won't do, some of which are mentioned in this
thread. But it reliably indents and reveals the basic program structure.

It's GPL, so someone could take it and run with it. I myself am too lazy.

--
Paul Lutus
http://www.arachnoid.com

Thanks! I'm using your beautifier, and finding it pretty robust. I
edited it a little into more idiomatic Ruby (noMoreNamesLikeThis),
made some things more concise, and applied a few rules of my own taste
(outdenting private and protected).

If you're interested:
http://po-ru.com/files/beautify-ruby

Paul.

···

On 24/08/06, Paul Lutus <nospam@nosite.zzz> wrote:

I have written a relatively primitive Ruby beautifier (written in Ruby) that
probably could be made ten times better if someone cared to expend the
effort:

http://www.arachnoid.com/ruby/rubyBeautifier.html

There are many things it won't do, some of which are mentioned in this
thread. But it reliably indents and reveals the basic program structure.

It's GPL, so someone could take it and run with it. I myself am too lazy.

Paul Battley wrote:

/ ...

Thanks! I'm using your beautifier, and finding it pretty robust. I
edited it a little into more idiomatic Ruby (noMoreNamesLikeThis),

Yes, a holdover from my Java days. I'll have to purge myself of these old
habits. :slight_smile:

made some things more concise, and applied a few rules of my own taste
(outdenting private and protected).

If you're interested:
http://po-ru.com/files/beautify-ruby

Nice. Better layout and more concise statement of the regexps, although your
version may perhaps run slower (because each of the parenthesized tests are
run against each encountered character). I haven't actually speed tested
it, but it seems logical that more partial tests are being run.

Don't get me wrong, I like how your layout looks. It's easier to read and
understand than mine, because similar tests are grouped together.

I have replied to several bug reports since the version you started with,
and, since your version now looks much better than mine, perhaps you would
be willing to look at my present version and incorporate a few of my recent
changes:

http://www.arachnoid.com/ruby/rubyBeautifier.html

In particular, the beginning of the line filter now does this:

            # throw out sequences that will
            # only sow confusion
            while tline.gsub!(/\(.*?\)/,"")
            end
            while tline.gsub!(/\/.*?\//,"")
            end

I did this because my original script didn't handle nested parentheses or
forward slashes properly, resulting in occasional bugs.

Also, "case" is now handled differently, because someone pointed out to me
that this --

return case ...

-- is valid Ruby syntax, and I wasn't taking this possibility into account.
So now I have this test:

/(^| )case\b/,

And thanks for your version, it gives me some ideas.

···

--
Paul Lutus
http://www.arachnoid.com