Tuesday, September 7, 2010

Ruby on Rails Coding Standards

This is a list of Rails coding guidelines that I've been putting together and generally suggesting are good practice for Rails development for a while, as well as a couple of Gotcha's that it's very easy to miss. Hopefully some people will find it interesting or useful.

Basic Stuff:
  • Two Spaces, No tabs
  • Keep lines to a reasonable length(80 characters is classical but 100-120 is probably acceptable with screen sizes these days)
  • Method names should be intuitive and meaningful
  • Variable names should be intuitive and meaningful
  • Don’t commit commented out code - It makes everything confusing and it’s in the version control anyway
  • Comment when necessary - If comments are necessary check that your code couldn’t be simplified first
  • Maintain application style - If it’s a new application then be Railsy.
  • If you want your application to survive then prioritize making the code easy to understand and navigate.
Code:
  • Skinny Controllers, Fat models - If a controller method is more than a few lines long then think very carefully about what you’re doing.
  • Views should have very very little ruby in them and certainly shouldn’t touch the Databases.
  • If something requires more than one commit then do it in a branch. Almost everything should take more than one commit.
  • Use plugins only if they’re exactly what you need. Do not cargo cult.
  • In Ruby Regexes \A is the beginning of the string and \z is the end, ^ and $ also match the beginning and end of lines. You almost always want \A and \z, especially in input validations.
  • Try to keep initializers limited to config.
  • Make sure your calls to the database are including everything they need to in the original call, N+1 problems are way too common in most rails apps.
  • RESTful controllers, they’re much easier to navigate and generally more secure.
  • Ternaries (?:) are good if they fit on one line (remember the short lines rule).
  • ||= is good
  • def self.method to define singleton methods not class << self
  • Select the appropriate columns in a database call if you don’t need everything and the table has lots of data.
  • Migrations go up AND down - they maintain database structure not data.
  • Test first all the time unless you’re prototyping. If you’re prototyping then either you throw the code away afterwards or you have to convince someone else to write tests for all of it.
  • Blocks should be {|x| ... } on one line and do |x|...end on multiple lines. .
  • One line if statements when appropriate.
  • A ridiculously large number of Railsy plugins use single table inheritance for things that it will turn out that you want to search over, avoid them if you want to be able to scale at all.
Security:
  • Rails has built in SQL Injection protection if you do :conditions => [“something =? “, thing] - Use it
  • h() to escape user inputted content in all pre Rails3 apps.
  • Use attr_accessible to whitelist variables that should mass-assignable.
These are guidelines, break them if you have a good reason. Feel free to leave any extra suggestions, I've probably missed stuff.

9 comments:

Chirantan Rajhans said...

def self.method to define singleton methods not class << self

Why?

I use it because it keeps all my class methods in one place and I don't have to write "self." in front of each of them.

Breccan said...

The major reason is that if you have more than a couple of self methods in a row then it becomes quite possible to end up looking at a method definition and not realise that it's a self method.

Also, it's easier to grep for self.something.

Basically, in the only case where it leads to less typing it runs a serious risk of making the code less clear.

Anonymous said...

I also like to split multiple lines as a series of single line statements (where applicable) with variable assignment. The variable names help clarify the code. Or better still move it into a method.

The Learning Lab said...

The learning lab is providing the best Maths and English tutor in Sydney and if you are looking for maths tutor for your child contact us now!

Coach Hire UK said...

This information really attracts me and I will share this with everyone too.Coach Hire UK

Unknown said...

cara alami menghilangkan kutil pada kelamin
kutil kelamin pada pria
OBAT HERBAL UNTUK WASIR
CARA MENGHILANGKAN WASIR | OBAT WASIR HERBAL
obat kutil di daerah kelamin
DAPATKAH KUTIL KELAMIN DISEMBUHKAN | OBAT KUTIL KELAMIN HERBAL
obat kutil di bibir vagina
OBAT WASIR HERBAL
obat kutil kelamin tradisional
gejala kutil kelamin dan pengobatannya
OBAT AMBEIEN HERBAL
CARA PENGOBATAN TRADISIONAL KUTIL KELAMIN
obat kencing nanah herbal
PENYAKIT KUTIL KELAMIN UNTUK WANITA | OBAT HERBAL ALAMI
obat herbal penyakit ambeien pada pria 
obat tradisional untuk menyembuhkan sipilis
OBAT WASIR HERBAL
obat kutil kelamin
resep pengobatan kutil kelamin
cara terbaik menuntaskan kutil kelamin
obat yang ampuh untuk menghilangkan kutil
OBAT KUTIL KELAMIN RESEP DOKTER | HERBAL ALAMI

Seagrove Beach real estate said...

Read your blog, I find your inner world is very rich also fascinating, I believe you will be successful, as long as you self-confidence!Seagrove Beach real estate

Unknown said...

Such maps are used in town planning, in the insurance industry, and by individuals who want to avoid moving into a home at risk of flooding or to know how to protect their property. wedding flowers

Anonymous said...

Really awesome blog. ipower Coupons and
iobit coupons