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... ← NOT a blog!!!