# RoR programming style guide?

**URL:** <https://www.ruby-forum.com/t/ror-programming-style-guide/85206>\
**Category:** Rails\
**Created:** [February 11, 2007, 1:29pm UTC](https://www.ruby-forum.com/t/ror-programming-style-guide/85206 "2007-02-11T13:29:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 11, 2007, 1:29pm UTC](https://www.ruby-forum.com/t/ror-programming-style-guide/85206/1 "2007-02-11T13:29:11Z")

</div>

Is there a style guide for RoR? For example, I see that rails likes  
to create things like

\<% … %\>  
\<% … %\>  
\<% … %\>

and I’m tempted to write this as  
\<%  
…  
…  
…  
%\>

but I’m not sure if doing so will make my code “look wrong”.

Since I’m a beginner, I’d like to get into the habit of writing code  
that has a conventional look. Years of C coding have convinced me  
that divergent writing styles [e.g. KR versus GNU] can slow the  
transfer of ideas. And I know that Ruby has this idea of “beauty”.  
What I’m wondering is whether someone has written some examples of  
beautiful, and ugly, RoR code.

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 11, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/ror-programming-style-guide/85206/2 "2007-02-11T14:08:14Z")

</div>

Google for “style guide”.  
See how many there are for just the English language!  
Look to see the controversy there was over C and other languages!

“Good practice” is usually “sensible” - as sites like  
[http://webstyleguide.com/](http://webstyleguide.com/)  
[http://www.w3.org/Provider/Style/](http://www.w3.org/Provider/Style/)  
[http://labs.silverorange.com/local/solabs/include/styleguide/](http://labs.silverorange.com/local/solabs/include/styleguide/)  
point out.

Some languages have syntactic necessities (e.g. in C statements end with  
a  
semicolon), and that HAS to be respected.

Apart from that, readability and clarity is what counts.  
Not some prescriptive, and probably anal retentive, set of rules.

You might, for example, look at the HTTP behind many site (try ‘view  
source’  
on your browser. You will see many “machine generated” pages that would  
be  
impossible to debug and many that are quite readable. Personally I  
think  
you can learn as much from studying the bad as well as the good.

I’m sure many people will give you detailed rules, but here are mine:

1. Is it readable and understandable?
2. Does it follow the correct syntax?
3. Does it produce the desired result?  
If it doesn’t, is the output able to be ‘debugged’ to determine  
the problem?
4. Is it maintainable when you come back to work on it 3 years later?

If you think about it, that applies to any language, templating system  
or  
web site.

dankelley said the following on 02/11/2007 07:27 AM:

> …  
> beautiful, and ugly, RoR code.  
> –  
> “The third person passive,” said the style manual, “is to be avoided.”

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 11, 2007, 2:28pm UTC](https://www.ruby-forum.com/t/ror-programming-style-guide/85206/3 "2007-02-11T14:28:16Z")

</div>

Hi –

On Sun, 11 Feb 2007, dankelley wrote:

> …  
> What I’m wondering is whether someone has written some examples of  
> beautiful, and ugly, RoR code.

You can get a good sense of traditional Ruby style from looking at the  
standard libraries. If you do two-space indenting, under\_score names  
for methods and local variables, ThisStyle for classes and modules,  
and don’t put a lot of spaces around arguments (do(this) but don’t do(  
this )), you’ll be well on your way.

In ERb, which the RHTML files use, some new questions come up, because  
of the templating. I think it’s good style to keep HTML out of the  
delimited code. In other words, do this:

\<% if x %\>

Hello.

  
\<% end %\>

rather than this:

\<%= “

Hello.

” if x %\>

I also tend to start a new delimiter for an extended clause, like a  
loop – in other words, this:

\<% i = 0 %\>  
\<% @things.each do |thing| %\>  
…  
\<% end %\>

rather than:

\<% i = 0  
@things.each do |thing| %\>  
…  
\<% end %\>

Doing it the first way makes it easier to move a loop around, or  
decide to put something before or after it.

I don’t know whether this all adds up to a rule (one statement per  
delimited clause?), but it seems to cover a lot of the cases I  
encounter.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 11, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/ror-programming-style-guide/85206/4 "2007-02-11T19:32:38Z")

</div>

dankelley wrote:

> Is there a style guide for RoR? For example, I see that rails likes  
> to create things like
> 
> \<% … %\>  
> \<% … %\>  
> \<% … %\>

That is probably an artifact of a generator. In general, one Rails style  
guideline is: Don’t Lean on Generators. The point of Rails is to write  
very  
minimal code into the terminals of event handlers.

> and I’m tempted to write this as  
> \<%  
> …  
> …  
> …  
> %\>

Write it like that. However, I’m tempted to write it like this:

\<%=  
x = Builder::XmlMarkup.new  
x.html do  
x.body do  
x.etc  
end  
end  
%\>

Sometimes I write whole stretches of HTML not in ERb, but in Ruby as an  
XmlMarkup. Sometimes that makes it easier to refactor.

> Since I’m a beginner, I’d like to get into the habit of writing code  
> that has a conventional look. Years of C coding have convinced me  
> that divergent writing styles [e.g. KR versus GNU] can slow the  
> transfer of ideas. And I know that Ruby has this idea of “beauty”.  
> What I’m wondering is whether someone has written some examples of  
> beautiful, and ugly, RoR code.

If you have a team, follow what the team does. That fixes the “K&R vs  
GNU”  
nonsense. All code should look like only one (very smart) person wrote  
it.  
Its technical and esthetic style should be seamless.

Your code should…

- follow team guidelines
- pass all tests (and have tests for every detail)
- be clear and expressive
- obey Don’t Repeat Yourself

Under Don’t Repeat Yourself, that \<% %\>\<% %\>\<% %\> sample you posted is  
not  
very DRY, because it repeats those silly delimiters!

Ruby, and especially the Rails Inflector, add a new twist to the “clear  
and  
expressive” part. We use underscore\_identifiers, not camelCase, despite  
the  
former is slightly harder to type. We do it because the \_ looks as close  
as  
possible to a space.

Well formed Ruby statements should read like normal English.

Consider ‘Order.has\_many :line\_items’. That Ruby is so clearly English  
that  
it expresses a program requirement. Even a client could read it (if not  
write it).

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!
