Coding standards


#1

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


#2

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:

On Fri, Mar 13, 2009 at 1:10 PM, John F.
removed_email_address@domain.invalidwrote:

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


#3

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.


#4

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


#5

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


#6

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


#7

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.


#8

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


#9

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?


#10

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.


#11

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-Professional-Ruby/dp/0321445619

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.