Difference between revisions of "Prince:Ruby Best Practices"

From OpenWetWare
Jump to: navigation, search
(New page: {{PrinceLab}} [From Russ Olsen's Eloquent Ruby] The ruby community has settled on some conventions in an effort to create readable code. Following these conventions will help you to writ...)
 
 
Line 16: Line 16:
 
* if/unless & while/until are logical pairs: use the one that fits the occasion
 
* if/unless & while/until are logical pairs: use the one that fits the occasion
 
* use '''each''' instead of '''for'''
 
* use '''each''' instead of '''for'''
*
+
* use case when
 +
* everything in ruby returns, so can use the return value
 +
* use ternary operator:  is_alive? ? 'bark' : 'do_nothing'

Latest revision as of 08:23, 24 May 2011

Prince lab logo.png

Home        Lab Members         Research         Publications         Internal         Mass Spec         Contact        


[From Russ Olsen's Eloquent Ruby] The ruby community has settled on some conventions in an effort to create readable code. Following these conventions will help you to write clean code and allow others to understand it.

Chapter 1

  • readability/comprehensibility trumps all other conventions
  • Indent with 2 spaces (use spaces over tabs)
  • CamelCase classes, snake_case_everything_else, & CONSTANTS_UPPER_SNAKE
  • generally use parentheses for arguments (can sometimes leave off): mymethod(one, 3)
  • leave parentheses off empty argument lists: word.split
  • generally leave parentheses off control statements, so write: if something...
  • one line block: {}, multi-line block: do ...\n... end

Chapter 2

  • if/unless & while/until are logical pairs: use the one that fits the occasion
  • use each instead of for
  • use case when
  • everything in ruby returns, so can use the return value
  • use ternary operator: is_alive? ? 'bark' : 'do_nothing'