Forum: Ruby on Rails Coding standards

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John F. (Guest)
on 2009-03-13 13:56
(Received via mailing list)
One thing I really like about Java compared to PHP is that there's an
official coding standard. This is because I don't really mind which
standard
I use, I just want to be told what to do and have the highest likelihood
that other code I encounter in that language will use a standard that
I'm
used to.



Bearing that in mind... I'm starting to learn Ruby and noticed there
doesn't
appear to be an official coding standard, nor for Rails. This document
basically claims to be a de facto standard, is that true
http://www.caliban.org/ruby/rubyguide.shtml#style?  If so I will follow
their conventions. Or is there a better resource? Is the community
fairly
consistent in their coding style?  I find the Java community is fairly
consistent, the PHP community rather the opposite.



Regards,

John
Sumanth K. (Guest)
on 2009-03-13 14:27
(Received via mailing list)
Well they are more or less same, we used to have our own set of
conventions
on par with other existing scripting languages.  This to ensure all the
code-base is in sync   :)


On Fri, Mar 13, 2009 at 1:10 PM, John F.
<removed_email_address@domain.invalid>wrote:

> document basically claims to be a de facto standard, is that true
>
> >
>


--
--
Thanks & Regards,
Sumanth Krishna. A
+358 40 3276564

Blogs:
                        TwinclingCommunity:

http://sumanthtechsavvy.blogspot.com/
         http://www.twincling.org/node/227

http://yourway2health.blogspot.com/
           http://www.osef.twincling.org
Phlip (Guest)
on 2009-03-13 14:43
(Received via mailing list)
John F. wrote:

> One thing I really like about Java compared to PHP is that there’s an
> official coding standard.

If the Official Coding Standard just... jumped off a cliff, would you?

> Bearing that in mind... I’m starting to learn Ruby and noticed there
> doesn’t appear to be an official coding standard, nor for Rails. This
> document basically claims to be a de facto standard, is that true
> http://www.caliban.org/ruby/rubyguide.shtml#style?  If so I will follow
> their conventions. Or is there a better resource? Is the community
> fairly consistent in their coding style?  I find the Java community is
> fairly consistent, the PHP community rather the opposite.

Ruby is supported by a real community, not a corporate oligarchy. We
have no
motive to pay some flunky to write up a document full of our leaders'
whims and
proclivities. Yet our coding standard really does exist - it is the
amalgam of
our best literature. For example:

  - 2 spaces for indentation
  - under_bar not CamelCase
  - omit unused keywords, such as return
  - omit top-level parens
  - the closer to correct English grammar the better
  - do-end on multi-line blocks
  - {} on blocks followed by .methods
  - cram everything onto one line (then wrap it funkily)

You will notice that not all of our standards make sense. Making them
official
would only exacerbate this problem.

Ruby's incredibly rich syntax powers our style. For example, the
operators 'or'
and 'and' associate more loosely than '=', so we can write:

       match.xpath('*').each do |child|
         issue = match_nodes(child, node) and
           return issue
       end

That notation looks unfamiliar, and it saves a couple of lines - without
cramming. It also obeys the perfectly universal coding standard, "Always
promote
the positive path thru a method, and demote the negative or failing
path. Make
the failing path low or to the right, and run the positive path down
along the
left."

The Official Java Standard doubtless enforced that too...

--
   Phlip
Robert W. (Guest)
on 2009-03-13 15:12
John F. wrote:
> http://www.caliban.org/ruby/rubyguide.shtml#style?  If so I will follow

I got 4 lines into this document and my brain threw a fatal exception
after this statement:

"header block with author's name, Perforce Id tag..."

When I saw "Perforce Id," I stopped processing the document.
Brandon O. (Guest)
on 2009-03-13 15:52
(Received via mailing list)
> the positive path thru a method, and demote the negative or failing
> path. Make
> the failing path low or to the right, and run the positive path down
> along the
> left."

Can you clarify this? I don't quite understand, but it looks intriguing.

Brandon
Brandon O. (Guest)
on 2009-03-13 15:52
(Received via mailing list)
Hi,
I
'm rather new to Ruby, but most of what it mentions seems similar to
what
I've seen so far.

The only thing I question is that it says always to include the
parentheses
around parameter lists. I tend to do the very opposite, only including
parentheses when absolutely necessary. I think Phlip said something
similar?
Unless I totally misunderstood.

So I guess that's my question to everyone else: do you include
parenthesis
around arguments or not?

Brandon
Phlip (Guest)
on 2009-03-13 16:33
(Received via mailing list)
>> http://www.caliban.org/ruby/rubyguide.shtml#style?  If so I will follow
>
> I got 4 lines into this document and my brain threw a fatal exception
> after this statement:
>
> "header block with author's name, Perforce Id tag..."
>
> When I saw "Perforce Id," I stopped processing the document.

Does the document say to unit test everything? Does Java's Official
guide say that?
Phlip (Guest)
on 2009-03-13 16:36
(Received via mailing list)
Brandon O. wrote:

> The only thing I question is that it says always to include the parentheses
> around parameter lists. I tend to do the very opposite, only including
> parentheses when absolutely necessary. I think Phlip said something similar?
> Unless I totally misunderstood.

Omit the top-level parens. You need the rest, to disambiguate.

   p call_method(with, args)

The p is the top-level method.
Phlip (Guest)
on 2009-03-13 16:41
(Received via mailing list)
> Can you clarify this? I don't quite understand, but it looks intriguing.

Don't do this:

   unless good_thing
     bad_path
   else
     good_path
   end

Do this:

   if good_thing
     good_path
   else
     bad_path
   end

The condition should almost always use an 'if', should always state its
intent
positively, and should always put the happy path above the sad path.

Similarly, don't:

   return unless good_thing

Do:

   good_thing or return
Billee D. (Guest)
on 2009-03-14 19:36
(Received via mailing list)
All of these suggestions are pretty much dead-on. If you don't already
own this book, you may want to consider picking up a copy:

http://www.amazon.com/Rails-Way-Addison-Wesley-Pro...

There are some suggestions on coding style which may help to clarify
what you are looking for. It's a highly-recommended book by many in
the Rails community. Plus, since the author Obie F. is an ex-
Java dev, it's also a good book for folks coming to Ruby/Rails from
other languages like Java.

As for that document claiming to be the "de facto" standard on Rails
coding style? I think that is just the author's default coding style.
Some of that stuff is just a bit too much. Phlip's suggestions on
methods and the 2-space indentation are really the only standards that
I am aware of. Other than that, have fun and love your code. That's
what Rails is about.
Fernando P. (Guest)
on 2009-03-15 00:40
> As for that document claiming to be the "de facto" standard on Rails
> coding style? I think that is just the author's default coding style.
What definitely turned me off this book, was the chapter about fixtures.
This topic is locked and can not be replied to.