Coding standards

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

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 :slight_smile:

John Fletcher 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 The Unofficial Ruby Usage Guide? 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...

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

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

Brandon

The Unofficial Ruby Usage Guide? 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?

Brandon Olivares 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.

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

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:

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 Fernandez 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.