Styleguide for Rails Projects 2

Posted by Jens Jäger on April 11, 2008

Principles

DRY – Don’t Repeat yourself.
http://www.jensjaeger.com/2008/04/das-dry-prinzip/ (german)
http://en.wikipedia.org/wiki/DRY

KISS – Keep it Simple Stupid
http://www.jensjaeger.com/2008/04/das-kiss-prinzip/ (german)
http://en.wikipedia.org/wiki/KISS_principle

General

General Ruby Styleguide
http://rubygarden.org/ruby/page/show/RubyStyleGuide

General Rails Styleguide:
http://wiki.rubyonrails.org/rails/pages/CodingStandards

Language

Everything in the source code must be written in english. Including comments, variable names and function names.

Encoding

All files must be encoded with UTF-8.

Methods

Every method should have a meaningful name.
For a procedure name use a strong verb.
Example:

 

For a function name use a description of the return value
Example:

 

Avoid meaningless or wishy-washy verbs.
Descripe everything the method does.
Make names of routines as long as necessary.
The average length of methods should be from 15 to 20 characters.

Variables

Every variable should have a meaningful name.
The most consideration in naming is that the name fully and accurately descripe the entity the variable represents.
The average length of a variable should be from 8 to 15 characters.
Use shorter name only for iterators in really short scopes.
Example:

 

The scope of a variable should be as short as possible.
Arrays should have a plural name.
Example:

 

Everything else should have a singular name.
Example:

Constants

Naming like variables.
There should be no numbers in the sourcecode. Every number should be a constant defined in environment.rb.
Example:

Comments

Comment as much as possible.
You shouldn’t comment WHAT your code does (this would be hurt the DRY-Principle), you should comment WHY your code does something.

Stay DRY

If you are doing something more than once, extract it into a method. This is especially the case when it comes to conditional queries. So instead of writing:

you should write a method:

Paranthesis

Use paranthesis with “standard” method calls and no paranthesis for helper-style hash arguments. Example:

but

Acknowledgments

Thanks to Florian Gilcher for the helpful comments.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Extreme Programming - jensjaeger.com Fri, 25 Apr 2008 12:37:15 CET

    […] Programmierstandards – Programmierer schreiben sämtlichen Code entsprechend Regeln, die die Kommunikation mithilfe des Codes erleichtern. […]

  2. Pol81 Fri, 23 Oct 2009 00:33:49 CET

    Do you have room to over-winter potted plants in your home? ,

Comments

Information about Data protection