SlideShare ist ein Scribd-Unternehmen logo
1 von 53
Downloaden Sie, um offline zu lesen
Rails Traps
Reuven M. Lerner • reuven@lerner.co.il

    International Rails Conference
    November 12th, 2012 • Tel Aviv
Who am I?

• Web developer since 1993
• Linux Journal columnist since 1996
• Long-time developer/trainer/consultant in
  Ruby, Rails, and PostgreSQL (and many
  more)
What do I do?

• Web development, especially in Rails
• Teaching/training
 • 4-day Ruby course in Herzliya next week
• Coaching/consulting
What is a trap?
Rails traps
It looks really good ...
... but it’s not worth the
         long-term
      consequences
Rails traps
Rails traps

• Techniques that look like they’ll benefit us...
• ... but which will haunt us down the road
• There is some overlap these and “anti-
  patterns,” a term that people use to
  describe “design patterns that you should
  avoid”
Rails traps
Trap #1:
    Always using Rails
• Rails is wonderful. Really, it is.
• But it’s not the only framework.
• (And Ruby isn’t the only language!)
• Use Rails because it’s the right framework
  for the job
Alternatives
• My favorite: Sinatra
• Ridiculously easy to create Web apps, built
  in Ruby, works with ERb and Haml (and
  other gems we know and love), and
  fantastic documentation
• But really, you should use what you know, if
  it gets the job done. (Even PHP...)
Trap #2:
Ignoring Rails’ opinions
• One of the best things about Rails is that
  it’s opinionated
  • Files go in specific places
  • MVC split is (fairly) obvious
  • ActiveRecord naming is pre-set
• If your opinions differ, you may have trouble
Don’t be a salmon
Opinions may differ...
   but not in Rails
• Yes, you can configure Rails to conform to
  your opinions
• But in doing so, you’re likely to encounter
  pain — now, and in the future
• Different parts of Rails make the same
  assumptions, so expect extended pain
• Stick with the defaults as much as possible
Some examples

• Always have a synthetic primary key, and
  always call it “id”
• Naming conventions for tables, classes
• Fat models and skinny controllers
• DRY things up with filters, callbacks
Trap #3:
     Ignoring the logs
• Logs are a developer’s best friend
• Is there a problem? Check the logs!
• Having a problem? Write to the log!
• (Check the database log, too!)
• Using a debugger should be your last
  resort, not your first one
Trap #4:
  Ignoring the console
• The console (especially with Pry) is one of
  the most useful parts of Rails
• Don’t ignore it, or think that it’s primitive
• The console is a critical part of my
  development and debugging process
• It helps me “feel” my way through the app
Trap #5:
     Not being RESTful
• For years, Rails has encouraged us to use
  RESTful resources
• You don’t have to; config/routes.rb still
  contains the following line (in a comment):
  match ':controller(/:action(/:id))(.:format)'

• Maybe you don’t have to be RESTful. But life
  is far easier when you accept REST.
Being RESTful

• Think “resources” rather than controllers
• Resources should be nouns, and actions
  operate on those nouns
• Those actions can usually be mapped onto
  the standard seven methods
What REST gives you
• Convention over configuration
• Ease of maintenance/understanding
• Routes, forms, and helpers that just work
• Scaffolds (yes, I like scaffolds)
• Forces you to think (and rethink) the
  structure of your application, which an
  definitely improve it
Trap #6:
   Only being RESTful
• REST is a technique, not a religion
• If you have non-RESTful controller actions,
  you are not an evil person!
  • (Well, not too evil)
• Add new member or collection methods
  when you need them — but not more
  often than that
Trap #7:
    Self authentication
• Rails doesn’t have a built-in authentication
  system. (This drives me crazy.)
• There are gems (e.g., Devise) that do a
  fantastic job, and which have been used and
  tested by many sites
• Security is hard to get right!
• Please don’t roll your own. Use a gem.
Trap #8:
Premature optimization
• Cache everything!
• Index everything!
• Get huge servers!
• Use bit masks in the database to avoid
  overloading the system!
Think about Ruby
• Ruby isn’t optimized for program speed, but
  rather for programmer speed
• That is an important competitive advantage
• Computers are cheap, but people are not
• First make sure your software is
  maintainable. Then worry whether it’s fast.
• And then benchmark before optimizing!
Trap #9:
DB operations in Ruby
• Of course, there are some places where
  you should consider speed
• Databases are really smart and efficient
  about handling data
• Plus, the data is far smaller as tuples than as
  ActiveRecord objects
• Let the database do the serious data lifting
Don’t do this
• Please don’t do this:
  Person.all.select {|p| p.age > 40}

• This will be incredibly slow.
• Serious databases (e.g., PostgreSQL) let you
  create server-side functions and views, which
  can manipulate the data before it ever gets
  to Ruby, at database speeds
Trap #10:
   Different databases
• I often see people using SQLite in
  development, and PostgreSQL in
  production
• Don’t do this, especially if you’re using an
  open-source database in production
• Databases are different — different data
  types, integrity solutions, and transactional
  behavior.
Trap #11:
       NoSQL or Bust
• I hear many people say that SQL is slow,
  bad, complex, or otherwise
• All of their problems will disappear if they
  switch to NoSQL!
• Really?
• That’s like saying, “Hashes are great, so I’ll
  use them instead of arrays.”
Trap #12:
The cloud! The cloud!
• Cloud solutions are great ...
• ... in certain circumstances.
• I’m using Heroku with three of my clients
  right now, and I’m very pleased with their
  product and support.
• But that doesn’t mean it’s a better, or even
  cheaper, option for everyone out there.
My cloudy questions
• How much direct control do I want/need
  over my company’s servers?
• Is the cost of cloud services lower than the
  cost of configuring and maintaining these
  myself?
• How likely am I to have to scale up
  massively, at a moment’s notice?
Trap #13:
Long controller actions
• It’s so tempting! You want to do so much
  in your controller action.
• But the user doesn’t want to wait for your
  e-mail to be sent.
• Use Resque, Delayed Job, or whatever you
  want... but put such things in the
  background, and answer the user ASAP.
Trap #14:
   No DB constraints
• Is your Rails application the only program
  that will ever touch the database?
• (Answer: Almost certainly not.)
• So why are your constraints only in Rails?
• A good database will let you specify not
  only NOT NULLs, but also valid values to
  protect your data
Trap #15:
Lack of callbacks, filters
• Callbacks (on ActiveRecord models) and
  filters (on controller actions) let you
  separate the “real work” from the less-
  important work
• Keep code short, readable, and DRY
Trap #16:
       Not using “lib”
• We talk a lot about MVC in Rails
• But don’t forget the “lib” directory
• It’s a great place to place modules and
  classes that are used across your app, or
  that aren’t specifically tied to MVC
• (Remember that lib isn’t automatically in
  the load path, as of Rails 3.x.)
Trap #17:
 Modifying migrations
• Migrations are one of my favorite parts of
  Rails.
• Migrations are reversible: If you make a
  mistake, you can migrate down, change the
  migration file, and then migrate up.
• Only do this before you have shared your
  migration with other people
Why not?

• Once you have pushed your migration,
  someone might have applied them
• Maybe you know to migrate backward,
  revert the old version, git pull, and then
  migrate forward... but does everyone?
Trap #18:
  Using class variables
• When I teach Ruby, everyone loves class
  variables! (They also love to call them
  “static variables.”)
• There is a place for class variables, but it’s a
  very limited one:
• Data that needs to be shared across all
  instances of a class
Class variable
         alternatives
• Class-level constants
• Class methods
• A module with methods or constants
• A model (for some types of constants)
And don’t forget


• Class variables vs. class instance variables
• This is where your understanding of the
  Ruby object model really comes in handy
class Confusion
  @x = 11             # class instance variable
  @@x = 22            # class variable
  def initialize
      @x = 999        # instance variable
  end
  def return_x
      @x              # returns 999
  end
  def self.return_x
      @x              # returns 11
  end
  def return_double_x
      @@x             # returns 22
  end
  def self.return_double_x
      @@x             # returns 22
  end
end
class Confusion
  @x = 11             # class instance variable
  @@x = 22            # class variable
  def initialize
      @x = 999        # instance variable
  end
  def return_x
      @x              # returns 999
  end
  def self.return_x
      @x              # returns 11
  end
  def return_double_x
      @@x             # returns 22
  end
  def self.return_double_x
      @@x             # returns 22
  end
end
class Confusion
  @x = 11             # class instance variable
  @@x = 22            # class variable
  def initialize
      @x = 999        # instance variable
  end
  def return_x
      @x              # returns 999
  end
  def self.return_x
      @x              # returns 11
  end
  def return_double_x
      @@x             # returns 22
  end
  def self.return_double_x
      @@x             # returns 22
  end
end
class Confusion
  @x = 11             # class instance variable
  @@x = 22            # class variable
  def initialize
      @x = 999        # instance variable
  end
  def return_x
      @x              # returns 999
  end
  def self.return_x
      @x              # returns 11
  end
  def return_double_x
      @@x             # returns 22
  end
  def self.return_double_x
      @@x             # returns 22
  end
end
class Confusion
  @x = 11             # class instance variable
  @@x = 22            # class variable
  def initialize
      @x = 999        # instance variable
  end
  def return_x
      @x              # returns 999
  end
  def self.return_x
      @x              # returns 11
  end
  def return_double_x
      @@x             # returns 22
  end
  def self.return_double_x
      @@x             # returns 22
  end
end
class Confusion
  @x = 11             # class instance variable
  @@x = 22            # class variable
  def initialize
      @x = 999        # instance variable
  end
  def return_x
      @x              # returns 999
  end
  def self.return_x
      @x              # returns 11
  end
  def return_double_x
      @@x             # returns 22
  end
  def self.return_double_x
      @@x             # returns 22
  end
end
class Confusion
  @x = 11             # class instance variable
  @@x = 22            # class variable
  def initialize
      @x = 999        # instance variable
  end
  def return_x
      @x              # returns 999
  end
  def self.return_x
      @x              # returns 11
  end
  def return_double_x
      @@x             # returns 22
  end
  def self.return_double_x
      @@x             # returns 22
  end
end
Trap #19:
      Cool technology
• Don’t use technology because it’s cool.
  Use technology because it gets the job
  done, and helps the business.
• So it might be cool to use a pub-sub system
  written in a .NET port of Clojure that
  transfers XML using EBCDIC...
• ... but is it necessary?
                         Is it the easiest and
  best solution? Is it maintainable?
Choosing technologies
• My preference is to use things that are
  proven, stable, and a little boring
• (Like me)
• The stability and viability of a project is far
  more important than the latest cool hack
• (Which I’ll install on my own machine to
  play with, but not use on clients’ projects)
Trap #20:
Not learning, improving

• You cannot survive in our business without
  learning new techniques all of the time
• The Ruby and Rails worlds move especially
  fast, with new gems released all of the time
What to do?
• Podcasts (Ruby Rogues, Ruby show, Ruby 5)
• Newsletters (Ruby Weekly)
• Blogs
• Books (remember those?)
• Learn other languages! You can pick up a
  lot of ideas and techniques from them
Bonus trap:
Not enjoying yourself
• Ruby and Rails make programming fun
• If you’re not having fun, then maybe you’re
  not doing it right
• (Or go join the wild party that I hear .NET
  developers are having!)
Thanks!
(Any questions?)

    reuven@lerner.co.il
   http://www.lerner.co.il/

       054-496-8405
“reuvenlerner” on Skype/AIM

Weitere ähnliche Inhalte

Was ist angesagt?

Web development basics (Part-7)
Web development basics (Part-7)Web development basics (Part-7)
Web development basics (Part-7)Rajat Pratap Singh
 
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010ssoroka
 
The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)lazyatom
 
Wed Development on Rails
Wed Development on RailsWed Development on Rails
Wed Development on RailsJames Gray
 
introduction to javascript
introduction to javascriptintroduction to javascript
introduction to javascriptKumar
 
Ruby On Rails Overview
Ruby On Rails OverviewRuby On Rails Overview
Ruby On Rails Overviewjonkinney
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agilityelliando dias
 
Tutorial visitor
Tutorial visitorTutorial visitor
Tutorial visitorWei Wang
 
SQL Server High Availability and DR - Too Many Choices!
SQL Server High Availability and DR - Too Many Choices!SQL Server High Availability and DR - Too Many Choices!
SQL Server High Availability and DR - Too Many Choices!Mike Walsh
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in PharoMarcus Denker
 
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)Rajat Pratap Singh
 
NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]Huy Do
 
The no-framework Scala Dependency Injection Framework
The no-framework Scala Dependency Injection FrameworkThe no-framework Scala Dependency Injection Framework
The no-framework Scala Dependency Injection FrameworkAdam Warski
 
Eureka Moment UKLUG
Eureka Moment UKLUGEureka Moment UKLUG
Eureka Moment UKLUGPaul Withers
 

Was ist angesagt? (20)

Web development basics (Part-7)
Web development basics (Part-7)Web development basics (Part-7)
Web development basics (Part-7)
 
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010
 
Children of Ruby
Children of RubyChildren of Ruby
Children of Ruby
 
Java script ppt
Java script pptJava script ppt
Java script ppt
 
The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)
 
Wed Development on Rails
Wed Development on RailsWed Development on Rails
Wed Development on Rails
 
introduction to javascript
introduction to javascriptintroduction to javascript
introduction to javascript
 
Ruby On Rails Overview
Ruby On Rails OverviewRuby On Rails Overview
Ruby On Rails Overview
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
Metaprogramming with javascript
Metaprogramming with javascriptMetaprogramming with javascript
Metaprogramming with javascript
 
Tutorial visitor
Tutorial visitorTutorial visitor
Tutorial visitor
 
SQL Server High Availability and DR - Too Many Choices!
SQL Server High Availability and DR - Too Many Choices!SQL Server High Availability and DR - Too Many Choices!
SQL Server High Availability and DR - Too Many Choices!
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Web Development with Smalltalk
Web Development with SmalltalkWeb Development with Smalltalk
Web Development with Smalltalk
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)
 
NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]
 
The no-framework Scala Dependency Injection Framework
The no-framework Scala Dependency Injection FrameworkThe no-framework Scala Dependency Injection Framework
The no-framework Scala Dependency Injection Framework
 
Eureka Moment UKLUG
Eureka Moment UKLUGEureka Moment UKLUG
Eureka Moment UKLUG
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 

Andere mochten auch

20080628001
2008062800120080628001
20080628001jbadenes
 
CON_Location_Based_Marketing
CON_Location_Based_MarketingCON_Location_Based_Marketing
CON_Location_Based_MarketingMathias Jahn
 
Teatre auditori sant_cugat_avanc_programacio
Teatre auditori sant_cugat_avanc_programacioTeatre auditori sant_cugat_avanc_programacio
Teatre auditori sant_cugat_avanc_programacioPremsa Sant Cugat
 
The TAS Group at InsideView #IS12
The TAS Group at InsideView #IS12The TAS Group at InsideView #IS12
The TAS Group at InsideView #IS12InsideView
 
Leonardo Scarcia
Leonardo ScarciaLeonardo Scarcia
Leonardo Scarcialeoscarcia
 
Logistikk Nettverk Nr 4 2011
Logistikk Nettverk Nr 4 2011Logistikk Nettverk Nr 4 2011
Logistikk Nettverk Nr 4 2011DB Schenker
 
Enerplan - Les Français et l'autoconsommation photovoltaïque - Par OpinionWay...
Enerplan - Les Français et l'autoconsommation photovoltaïque - Par OpinionWay...Enerplan - Les Français et l'autoconsommation photovoltaïque - Par OpinionWay...
Enerplan - Les Français et l'autoconsommation photovoltaïque - Par OpinionWay...OpinionWay
 
sitacon - Solutions in Tourism
sitacon - Solutions in Tourism sitacon - Solutions in Tourism
sitacon - Solutions in Tourism sitacon
 
Comunicare senza la parola?
Comunicare senza la parola?Comunicare senza la parola?
Comunicare senza la parola?leluda
 
Hcl offers BPO services in 14 international languages, with widest reach
Hcl offers BPO services in 14 international languages, with widest reachHcl offers BPO services in 14 international languages, with widest reach
Hcl offers BPO services in 14 international languages, with widest reachHcl Brand
 
Kouvaros S. et al (2015). Hippocampal sharp waves and ripples. Effects of agi...
Kouvaros S. et al (2015). Hippocampal sharp waves and ripples. Effects of agi...Kouvaros S. et al (2015). Hippocampal sharp waves and ripples. Effects of agi...
Kouvaros S. et al (2015). Hippocampal sharp waves and ripples. Effects of agi...Stylianos Kouvaros
 
La noticia periodistica.
La noticia periodistica.La noticia periodistica.
La noticia periodistica.miguelurdin4a
 
Unic AG - Testflug in die Cloud mit Microsoft Office 365
Unic AG - Testflug in die Cloud mit Microsoft Office 365Unic AG - Testflug in die Cloud mit Microsoft Office 365
Unic AG - Testflug in die Cloud mit Microsoft Office 365Unic
 
27 10 14 el hombre multiorg. algunas paginas
27 10 14 el hombre multiorg. algunas paginas27 10 14 el hombre multiorg. algunas paginas
27 10 14 el hombre multiorg. algunas paginasRenzzo QuiroSalud
 

Andere mochten auch (20)

Digital Savvy Free Study Final 5.12.08
Digital Savvy Free Study Final 5.12.08Digital Savvy Free Study Final 5.12.08
Digital Savvy Free Study Final 5.12.08
 
20080628001
2008062800120080628001
20080628001
 
CON_Location_Based_Marketing
CON_Location_Based_MarketingCON_Location_Based_Marketing
CON_Location_Based_Marketing
 
Godrej properties limited
Godrej properties limitedGodrej properties limited
Godrej properties limited
 
Teatre auditori sant_cugat_avanc_programacio
Teatre auditori sant_cugat_avanc_programacioTeatre auditori sant_cugat_avanc_programacio
Teatre auditori sant_cugat_avanc_programacio
 
Gestar Ii 4 Unidade
Gestar Ii 4 UnidadeGestar Ii 4 Unidade
Gestar Ii 4 Unidade
 
Onlinemktg
OnlinemktgOnlinemktg
Onlinemktg
 
The TAS Group at InsideView #IS12
The TAS Group at InsideView #IS12The TAS Group at InsideView #IS12
The TAS Group at InsideView #IS12
 
Leonardo Scarcia
Leonardo ScarciaLeonardo Scarcia
Leonardo Scarcia
 
Logistikk Nettverk Nr 4 2011
Logistikk Nettverk Nr 4 2011Logistikk Nettverk Nr 4 2011
Logistikk Nettverk Nr 4 2011
 
Enerplan - Les Français et l'autoconsommation photovoltaïque - Par OpinionWay...
Enerplan - Les Français et l'autoconsommation photovoltaïque - Par OpinionWay...Enerplan - Les Français et l'autoconsommation photovoltaïque - Par OpinionWay...
Enerplan - Les Français et l'autoconsommation photovoltaïque - Par OpinionWay...
 
sitacon - Solutions in Tourism
sitacon - Solutions in Tourism sitacon - Solutions in Tourism
sitacon - Solutions in Tourism
 
Comunicare senza la parola?
Comunicare senza la parola?Comunicare senza la parola?
Comunicare senza la parola?
 
Hcl offers BPO services in 14 international languages, with widest reach
Hcl offers BPO services in 14 international languages, with widest reachHcl offers BPO services in 14 international languages, with widest reach
Hcl offers BPO services in 14 international languages, with widest reach
 
Kouvaros S. et al (2015). Hippocampal sharp waves and ripples. Effects of agi...
Kouvaros S. et al (2015). Hippocampal sharp waves and ripples. Effects of agi...Kouvaros S. et al (2015). Hippocampal sharp waves and ripples. Effects of agi...
Kouvaros S. et al (2015). Hippocampal sharp waves and ripples. Effects of agi...
 
La noticia periodistica.
La noticia periodistica.La noticia periodistica.
La noticia periodistica.
 
Unic AG - Testflug in die Cloud mit Microsoft Office 365
Unic AG - Testflug in die Cloud mit Microsoft Office 365Unic AG - Testflug in die Cloud mit Microsoft Office 365
Unic AG - Testflug in die Cloud mit Microsoft Office 365
 
27 10 14 el hombre multiorg. algunas paginas
27 10 14 el hombre multiorg. algunas paginas27 10 14 el hombre multiorg. algunas paginas
27 10 14 el hombre multiorg. algunas paginas
 
Riesgo laboral
Riesgo laboralRiesgo laboral
Riesgo laboral
 
Diseño publicitario
Diseño publicitarioDiseño publicitario
Diseño publicitario
 

Ähnlich wie Rails traps

Rapid Application Development using Ruby on Rails
Rapid Application Development using Ruby on RailsRapid Application Development using Ruby on Rails
Rapid Application Development using Ruby on RailsSimobo
 
25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails Development25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails DevelopmentBelighted
 
Rails Tips and Best Practices
Rails Tips and Best PracticesRails Tips and Best Practices
Rails Tips and Best PracticesDavid Keener
 
Java Closures
Java ClosuresJava Closures
Java ClosuresBen Evans
 
Data consistency: Analyse, understand and decide
Data consistency: Analyse, understand and decideData consistency: Analyse, understand and decide
Data consistency: Analyse, understand and decideLouis Jacomet
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesDr. Syed Hassan Amin
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Nilesh Panchal
 
Kevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScriptKevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScriptAxway Appcelerator
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
JSR 335 / java 8 - update reference
JSR 335 / java 8 - update referenceJSR 335 / java 8 - update reference
JSR 335 / java 8 - update referencesandeepji_choudhary
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Codersebbe
 
Ruby codebases in an entropic universe
Ruby codebases in an entropic universeRuby codebases in an entropic universe
Ruby codebases in an entropic universeNiranjan Paranjape
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentationBhavin Gandhi
 
Clojure's take on concurrency
Clojure's take on concurrencyClojure's take on concurrency
Clojure's take on concurrencyyoavrubin
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Codeeddiehaber
 
Architecture by Accident
Architecture by AccidentArchitecture by Accident
Architecture by AccidentGleicon Moraes
 

Ähnlich wie Rails traps (20)

Rapid Application Development using Ruby on Rails
Rapid Application Development using Ruby on RailsRapid Application Development using Ruby on Rails
Rapid Application Development using Ruby on Rails
 
25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails Development25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails Development
 
Rails Tips and Best Practices
Rails Tips and Best PracticesRails Tips and Best Practices
Rails Tips and Best Practices
 
Rails tools
Rails toolsRails tools
Rails tools
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
Data consistency: Analyse, understand and decide
Data consistency: Analyse, understand and decideData consistency: Analyse, understand and decide
Data consistency: Analyse, understand and decide
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design Principles
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Kevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScriptKevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScript
 
Testing gone-right
Testing gone-rightTesting gone-right
Testing gone-right
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
JSR 335 / java 8 - update reference
JSR 335 / java 8 - update referenceJSR 335 / java 8 - update reference
JSR 335 / java 8 - update reference
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
 
Ruby codebases in an entropic universe
Ruby codebases in an entropic universeRuby codebases in an entropic universe
Ruby codebases in an entropic universe
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
 
Clojure's take on concurrency
Clojure's take on concurrencyClojure's take on concurrency
Clojure's take on concurrency
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Code
 
Architecture by Accident
Architecture by AccidentArchitecture by Accident
Architecture by Accident
 

Mehr von Reuven Lerner

Technical training business talk.key
Technical training business talk.keyTechnical training business talk.key
Technical training business talk.keyReuven Lerner
 
Big Data — Your new best friend
Big Data — Your new best friendBig Data — Your new best friend
Big Data — Your new best friendReuven Lerner
 
PostgreSQL, your NoSQL database
PostgreSQL, your NoSQL databasePostgreSQL, your NoSQL database
PostgreSQL, your NoSQL databaseReuven Lerner
 
Python's magic methods
Python's magic methodsPython's magic methods
Python's magic methodsReuven Lerner
 
What can Ruby learn from Python (and vice versa)?
What can Ruby learn from Python (and vice versa)?What can Ruby learn from Python (and vice versa)?
What can Ruby learn from Python (and vice versa)?Reuven Lerner
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Reuven Lerner
 
Web APIs: The future of software
Web APIs: The future of softwareWeb APIs: The future of software
Web APIs: The future of softwareReuven Lerner
 
Intro to cloud computing — MegaCOMM 2013, Jerusalem
Intro to cloud computing — MegaCOMM 2013, JerusalemIntro to cloud computing — MegaCOMM 2013, Jerusalem
Intro to cloud computing — MegaCOMM 2013, JerusalemReuven Lerner
 
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Reuven Lerner
 
Rails development environment talk
Rails development environment talkRails development environment talk
Rails development environment talkReuven Lerner
 
Git talk from Open 2011 conference in Israel
Git talk from Open 2011 conference in IsraelGit talk from Open 2011 conference in Israel
Git talk from Open 2011 conference in IsraelReuven Lerner
 
Dynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupDynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupReuven Lerner
 
Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Reuven Lerner
 
PostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conferencePostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conferenceReuven Lerner
 

Mehr von Reuven Lerner (20)

Technical training business talk.key
Technical training business talk.keyTechnical training business talk.key
Technical training business talk.key
 
Big Data — Your new best friend
Big Data — Your new best friendBig Data — Your new best friend
Big Data — Your new best friend
 
PostgreSQL, your NoSQL database
PostgreSQL, your NoSQL databasePostgreSQL, your NoSQL database
PostgreSQL, your NoSQL database
 
Python's magic methods
Python's magic methodsPython's magic methods
Python's magic methods
 
What can Ruby learn from Python (and vice versa)?
What can Ruby learn from Python (and vice versa)?What can Ruby learn from Python (and vice versa)?
What can Ruby learn from Python (and vice versa)?
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014
 
Web APIs: The future of software
Web APIs: The future of softwareWeb APIs: The future of software
Web APIs: The future of software
 
Rails israel 2013
Rails israel 2013Rails israel 2013
Rails israel 2013
 
Intro to cloud computing — MegaCOMM 2013, Jerusalem
Intro to cloud computing — MegaCOMM 2013, JerusalemIntro to cloud computing — MegaCOMM 2013, Jerusalem
Intro to cloud computing — MegaCOMM 2013, Jerusalem
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
 
Rails development environment talk
Rails development environment talkRails development environment talk
Rails development environment talk
 
Git talk from Open 2011 conference in Israel
Git talk from Open 2011 conference in IsraelGit talk from Open 2011 conference in Israel
Git talk from Open 2011 conference in Israel
 
Dynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupDynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship group
 
Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011
 
PostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conferencePostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conference
 
ActiveRecord 2.3
ActiveRecord 2.3ActiveRecord 2.3
ActiveRecord 2.3
 
Ruby objects
Ruby objectsRuby objects
Ruby objects
 
Rails console
Rails consoleRails console
Rails console
 
Why ruby and rails
Why ruby and railsWhy ruby and rails
Why ruby and rails
 

Kürzlich hochgeladen

Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 

Kürzlich hochgeladen (20)

Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
20150722 - AGV
20150722 - AGV20150722 - AGV
20150722 - AGV
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 

Rails traps

  • 1. Rails Traps Reuven M. Lerner • reuven@lerner.co.il International Rails Conference November 12th, 2012 • Tel Aviv
  • 2. Who am I? • Web developer since 1993 • Linux Journal columnist since 1996 • Long-time developer/trainer/consultant in Ruby, Rails, and PostgreSQL (and many more)
  • 3. What do I do? • Web development, especially in Rails • Teaching/training • 4-day Ruby course in Herzliya next week • Coaching/consulting
  • 4. What is a trap?
  • 6. It looks really good ...
  • 7. ... but it’s not worth the long-term consequences
  • 9. Rails traps • Techniques that look like they’ll benefit us... • ... but which will haunt us down the road • There is some overlap these and “anti- patterns,” a term that people use to describe “design patterns that you should avoid”
  • 11. Trap #1: Always using Rails • Rails is wonderful. Really, it is. • But it’s not the only framework. • (And Ruby isn’t the only language!) • Use Rails because it’s the right framework for the job
  • 12. Alternatives • My favorite: Sinatra • Ridiculously easy to create Web apps, built in Ruby, works with ERb and Haml (and other gems we know and love), and fantastic documentation • But really, you should use what you know, if it gets the job done. (Even PHP...)
  • 13. Trap #2: Ignoring Rails’ opinions • One of the best things about Rails is that it’s opinionated • Files go in specific places • MVC split is (fairly) obvious • ActiveRecord naming is pre-set • If your opinions differ, you may have trouble
  • 14. Don’t be a salmon
  • 15. Opinions may differ... but not in Rails • Yes, you can configure Rails to conform to your opinions • But in doing so, you’re likely to encounter pain — now, and in the future • Different parts of Rails make the same assumptions, so expect extended pain • Stick with the defaults as much as possible
  • 16. Some examples • Always have a synthetic primary key, and always call it “id” • Naming conventions for tables, classes • Fat models and skinny controllers • DRY things up with filters, callbacks
  • 17. Trap #3: Ignoring the logs • Logs are a developer’s best friend • Is there a problem? Check the logs! • Having a problem? Write to the log! • (Check the database log, too!) • Using a debugger should be your last resort, not your first one
  • 18. Trap #4: Ignoring the console • The console (especially with Pry) is one of the most useful parts of Rails • Don’t ignore it, or think that it’s primitive • The console is a critical part of my development and debugging process • It helps me “feel” my way through the app
  • 19. Trap #5: Not being RESTful • For years, Rails has encouraged us to use RESTful resources • You don’t have to; config/routes.rb still contains the following line (in a comment): match ':controller(/:action(/:id))(.:format)' • Maybe you don’t have to be RESTful. But life is far easier when you accept REST.
  • 20. Being RESTful • Think “resources” rather than controllers • Resources should be nouns, and actions operate on those nouns • Those actions can usually be mapped onto the standard seven methods
  • 21. What REST gives you • Convention over configuration • Ease of maintenance/understanding • Routes, forms, and helpers that just work • Scaffolds (yes, I like scaffolds) • Forces you to think (and rethink) the structure of your application, which an definitely improve it
  • 22. Trap #6: Only being RESTful • REST is a technique, not a religion • If you have non-RESTful controller actions, you are not an evil person! • (Well, not too evil) • Add new member or collection methods when you need them — but not more often than that
  • 23. Trap #7: Self authentication • Rails doesn’t have a built-in authentication system. (This drives me crazy.) • There are gems (e.g., Devise) that do a fantastic job, and which have been used and tested by many sites • Security is hard to get right! • Please don’t roll your own. Use a gem.
  • 24. Trap #8: Premature optimization • Cache everything! • Index everything! • Get huge servers! • Use bit masks in the database to avoid overloading the system!
  • 25. Think about Ruby • Ruby isn’t optimized for program speed, but rather for programmer speed • That is an important competitive advantage • Computers are cheap, but people are not • First make sure your software is maintainable. Then worry whether it’s fast. • And then benchmark before optimizing!
  • 26. Trap #9: DB operations in Ruby • Of course, there are some places where you should consider speed • Databases are really smart and efficient about handling data • Plus, the data is far smaller as tuples than as ActiveRecord objects • Let the database do the serious data lifting
  • 27. Don’t do this • Please don’t do this: Person.all.select {|p| p.age > 40} • This will be incredibly slow. • Serious databases (e.g., PostgreSQL) let you create server-side functions and views, which can manipulate the data before it ever gets to Ruby, at database speeds
  • 28. Trap #10: Different databases • I often see people using SQLite in development, and PostgreSQL in production • Don’t do this, especially if you’re using an open-source database in production • Databases are different — different data types, integrity solutions, and transactional behavior.
  • 29. Trap #11: NoSQL or Bust • I hear many people say that SQL is slow, bad, complex, or otherwise • All of their problems will disappear if they switch to NoSQL! • Really? • That’s like saying, “Hashes are great, so I’ll use them instead of arrays.”
  • 30. Trap #12: The cloud! The cloud! • Cloud solutions are great ... • ... in certain circumstances. • I’m using Heroku with three of my clients right now, and I’m very pleased with their product and support. • But that doesn’t mean it’s a better, or even cheaper, option for everyone out there.
  • 31. My cloudy questions • How much direct control do I want/need over my company’s servers? • Is the cost of cloud services lower than the cost of configuring and maintaining these myself? • How likely am I to have to scale up massively, at a moment’s notice?
  • 32. Trap #13: Long controller actions • It’s so tempting! You want to do so much in your controller action. • But the user doesn’t want to wait for your e-mail to be sent. • Use Resque, Delayed Job, or whatever you want... but put such things in the background, and answer the user ASAP.
  • 33. Trap #14: No DB constraints • Is your Rails application the only program that will ever touch the database? • (Answer: Almost certainly not.) • So why are your constraints only in Rails? • A good database will let you specify not only NOT NULLs, but also valid values to protect your data
  • 34. Trap #15: Lack of callbacks, filters • Callbacks (on ActiveRecord models) and filters (on controller actions) let you separate the “real work” from the less- important work • Keep code short, readable, and DRY
  • 35. Trap #16: Not using “lib” • We talk a lot about MVC in Rails • But don’t forget the “lib” directory • It’s a great place to place modules and classes that are used across your app, or that aren’t specifically tied to MVC • (Remember that lib isn’t automatically in the load path, as of Rails 3.x.)
  • 36. Trap #17: Modifying migrations • Migrations are one of my favorite parts of Rails. • Migrations are reversible: If you make a mistake, you can migrate down, change the migration file, and then migrate up. • Only do this before you have shared your migration with other people
  • 37. Why not? • Once you have pushed your migration, someone might have applied them • Maybe you know to migrate backward, revert the old version, git pull, and then migrate forward... but does everyone?
  • 38. Trap #18: Using class variables • When I teach Ruby, everyone loves class variables! (They also love to call them “static variables.”) • There is a place for class variables, but it’s a very limited one: • Data that needs to be shared across all instances of a class
  • 39. Class variable alternatives • Class-level constants • Class methods • A module with methods or constants • A model (for some types of constants)
  • 40. And don’t forget • Class variables vs. class instance variables • This is where your understanding of the Ruby object model really comes in handy
  • 41. class Confusion @x = 11 # class instance variable @@x = 22 # class variable def initialize @x = 999 # instance variable end def return_x @x # returns 999 end def self.return_x @x # returns 11 end def return_double_x @@x # returns 22 end def self.return_double_x @@x # returns 22 end end
  • 42. class Confusion @x = 11 # class instance variable @@x = 22 # class variable def initialize @x = 999 # instance variable end def return_x @x # returns 999 end def self.return_x @x # returns 11 end def return_double_x @@x # returns 22 end def self.return_double_x @@x # returns 22 end end
  • 43. class Confusion @x = 11 # class instance variable @@x = 22 # class variable def initialize @x = 999 # instance variable end def return_x @x # returns 999 end def self.return_x @x # returns 11 end def return_double_x @@x # returns 22 end def self.return_double_x @@x # returns 22 end end
  • 44. class Confusion @x = 11 # class instance variable @@x = 22 # class variable def initialize @x = 999 # instance variable end def return_x @x # returns 999 end def self.return_x @x # returns 11 end def return_double_x @@x # returns 22 end def self.return_double_x @@x # returns 22 end end
  • 45. class Confusion @x = 11 # class instance variable @@x = 22 # class variable def initialize @x = 999 # instance variable end def return_x @x # returns 999 end def self.return_x @x # returns 11 end def return_double_x @@x # returns 22 end def self.return_double_x @@x # returns 22 end end
  • 46. class Confusion @x = 11 # class instance variable @@x = 22 # class variable def initialize @x = 999 # instance variable end def return_x @x # returns 999 end def self.return_x @x # returns 11 end def return_double_x @@x # returns 22 end def self.return_double_x @@x # returns 22 end end
  • 47. class Confusion @x = 11 # class instance variable @@x = 22 # class variable def initialize @x = 999 # instance variable end def return_x @x # returns 999 end def self.return_x @x # returns 11 end def return_double_x @@x # returns 22 end def self.return_double_x @@x # returns 22 end end
  • 48. Trap #19: Cool technology • Don’t use technology because it’s cool. Use technology because it gets the job done, and helps the business. • So it might be cool to use a pub-sub system written in a .NET port of Clojure that transfers XML using EBCDIC... • ... but is it necessary? Is it the easiest and best solution? Is it maintainable?
  • 49. Choosing technologies • My preference is to use things that are proven, stable, and a little boring • (Like me) • The stability and viability of a project is far more important than the latest cool hack • (Which I’ll install on my own machine to play with, but not use on clients’ projects)
  • 50. Trap #20: Not learning, improving • You cannot survive in our business without learning new techniques all of the time • The Ruby and Rails worlds move especially fast, with new gems released all of the time
  • 51. What to do? • Podcasts (Ruby Rogues, Ruby show, Ruby 5) • Newsletters (Ruby Weekly) • Blogs • Books (remember those?) • Learn other languages! You can pick up a lot of ideas and techniques from them
  • 52. Bonus trap: Not enjoying yourself • Ruby and Rails make programming fun • If you’re not having fun, then maybe you’re not doing it right • (Or go join the wild party that I hear .NET developers are having!)
  • 53. Thanks! (Any questions?) reuven@lerner.co.il http://www.lerner.co.il/ 054-496-8405 “reuvenlerner” on Skype/AIM

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n