rails is from mars ruby is from venus presentation 1

77
RAILS IS FROM MARS RUBY IS FROM VENUS Relationship Advice For Rails Developers

Post on 12-Sep-2014

4.223 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Rails Is From Mars  Ruby Is From Venus Presentation 1

RAILS IS FROM MARSRUBY IS FROM VENUS

Relationship Advice For Rails Developers

Page 2: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHO AM I?• My name is Rein Henrichs.

• That’s pronounced like “rain”.

• But spelled differently.

• I work at

• I blog at reinh.com

• I twitter @reinh

• I like candlelit dinners and long walks on the beach.

Page 3: Rails Is From Mars  Ruby Is From Venus Presentation 1

YOU KNOW RAILS

insert logohere

:(

Page 4: Rails Is From Mars  Ruby Is From Venus Presentation 1

BUT DO YOU KNOW RUBY?

Page 5: Rails Is From Mars  Ruby Is From Venus Presentation 1

How many people here use Rails?

Page 6: Rails Is From Mars  Ruby Is From Venus Presentation 1

How many of you think you know Ruby as well as you know Rails?

Page 7: Rails Is From Mars  Ruby Is From Venus Presentation 1

How many of you have contributed to an open-source

Ruby project?

Page 8: Rails Is From Mars  Ruby Is From Venus Presentation 1

How many of you have written your own gem?

Page 9: Rails Is From Mars  Ruby Is From Venus Presentation 1

How many of you would be comfortable writing an HTTP

client library in Ruby?

Page 10: Rails Is From Mars  Ruby Is From Venus Presentation 1

How many of you could write your own web framework?

Page 11: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY SHOULD I LEARN RUBY?

• It’s easy.

• It’s fun.

• It will make you better at Rails.

• It will make you a better person.

Page 12: Rails Is From Mars  Ruby Is From Venus Presentation 1

HOW DO I LEARN RUBY?Some resources

Page 13: Rails Is From Mars  Ruby Is From Venus Presentation 1

Free stuff

Page 14: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY’S POIGNANT GUIDEIt has cartoon foxes. Foxen? It has cartoon foxen.

Page 15: Rails Is From Mars  Ruby Is From Venus Presentation 1

PROGRAMMING RUBYa.k.a. The Pickaxe

Page 16: Rails Is From Mars  Ruby Is From Venus Presentation 1

MR. NEIGHBORLY’S HUMBLE LITTLE

RUBY BOOKPragmatically chunky bacon

Page 17: Rails Is From Mars  Ruby Is From Venus Presentation 1

Not so free stuff (but still really good)

Page 18: Rails Is From Mars  Ruby Is From Venus Presentation 1

RUBY IN A NUTSHELL

Come on. Matz wrote it.

Page 19: Rails Is From Mars  Ruby Is From Venus Presentation 1

RUBY IN PRACTICE

Look at that funny looking guy on the cover. At least it’s not a nasty monkey. Sorry, O’Reilly

Page 20: Rails Is From Mars  Ruby Is From Venus Presentation 1

THE RUBY WAY

Page 21: Rails Is From Mars  Ruby Is From Venus Presentation 1

RUBY FOR RAILS

Page 22: Rails Is From Mars  Ruby Is From Venus Presentation 1

THE WELL-GROUNDED

RUBYIST

Page 23: Rails Is From Mars  Ruby Is From Venus Presentation 1

RAILS HAS OPINIONSAnd Ruby Likes To Talk

Page 24: Rails Is From Mars  Ruby Is From Venus Presentation 1

Rails is opinionated software

Page 25: Rails Is From Mars  Ruby Is From Venus Presentation 1

Ruby is a communicative language

Page 26: Rails Is From Mars  Ruby Is From Venus Presentation 1

Sometimes, Ruby just wantssomeone to listen to it.

Page 27: Rails Is From Mars  Ruby Is From Venus Presentation 1

If you’re programming along, doing nicely, and all of a sudden your program gets balky, makes things hard for you, it’s talking. It’s telling you there is something important missing.

– Kent Beck, Smalltalk Best Practice Patterns

Page 28: Rails Is From Mars  Ruby Is From Venus Presentation 1

It is your responsibility to listen to your code and be considerate of its needs.

Page 29: Rails Is From Mars  Ruby Is From Venus Presentation 1

Write Ruby code that communicates well but be respectful of Rails’ opinions

Page 30: Rails Is From Mars  Ruby Is From Venus Presentation 1

Other people who use your code (including six-months-later you)

will thank you.

Page 31: Rails Is From Mars  Ruby Is From Venus Presentation 1

Ruby makes it easy to write simply, clearly and expressively

Page 32: Rails Is From Mars  Ruby Is From Venus Presentation 1

Rails has powerful idioms and conventions

Page 33: Rails Is From Mars  Ruby Is From Venus Presentation 1

Combining the two makes for a happy, fulfilling relationship

Page 34: Rails Is From Mars  Ruby Is From Venus Presentation 1

RUBY LOVES YOUBut Sometimes You Drive Her Crazy

Page 35: Rails Is From Mars  Ruby Is From Venus Presentation 1

These are some of the things you do that drive Ruby crazy.

Page 36: Rails Is From Mars  Ruby Is From Venus Presentation 1

You’re welcome.

Page 37: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Badi = 0; while i < array.size do puts array[i] i += 1end

# Betterfor item in array puts itemend

# Bestarray.each do |item| puts itemend

Page 38: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY?

• Ruby has powerful iterators.

• You don’t need to write your own.

• for ... in ... just calls #each internally.

Page 39: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Badvalue = value ? value : "default"

# Bettervalue = value || "default"

# Bestvalue ||= "default"

Page 40: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY?

• Ternaries (the ? : thing) are ugly.

• Ruby has pretty assignment with operators like += and ||=

Page 41: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Badarray << 42 unless array.include?(42)array = array + [42] unless array.include?(42)

# Betterarray = array | [42]

# Bestarray |= [42]

Page 42: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY?

• Sometimes it just helps to know what set union is.

Page 43: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Bad

if value != nil && value != false

# Good

if value

Page 44: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY?

• Ruby has a sane notion of truthiness

Page 45: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Bad

if value == 1 || value == 12 || value == 42

# Good

if [1,12,42].include? value

Page 46: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY?

• Brevity is not the goal

• Readability is the goal

• But if it is more readable and also shorter, go for it.

Page 47: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Bad

def request

begin

perform_request

rescue RequestError => e

log_error e

end

end

# Good

def request

perform_request

rescue RequestError => e

log_error e

end

Page 48: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY?

• Method definitions are an implied begin block.

Page 49: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Bad

!!value

# Good

value

Page 50: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY?

• Ruby does not not like clarity.

• What you lose in readability you gain in nothing.

Page 51: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Bad

ActiveRecord::Base

# Good

ActiveRecord::Model

Page 52: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY?

• Naming things is important.

• Base? What does that even mean?

• Sorry Rails, you got this one wrong. Better luck next time.

Page 53: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Bad

class PostsController < ApplicationController

def recent

Post.find :all,

:conditions => ['posts.created_at > ?',

1.week.ago]

end

end

Page 54: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Good

class PostsController < ApplicationController

def recent

Post.within 1.week

end

end

Page 55: Rails Is From Mars  Ruby Is From Venus Presentation 1

class Post < ActiveRecord::Base

named_scope :within,

lambda {|seconds| :conditions => ['posts.created_at > ?',

seconds.ago]}

end

Page 56: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY?

• Make your code more expressive

• And more intention revealing.

• In other words, say what you mean to say.

Page 57: Rails Is From Mars  Ruby Is From Venus Presentation 1

url_for(:blog, :posts, @post.id, :comments, :replies => true)

# => http://example.com/blog/posts/19/comments?replies=true

Page 58: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Bad

def url_for(*args)

root + args.map{|arg| parse_arg(arg)}.join('/').

gsub('/?', '?')

end

def parse_arg(arg)

case arg

when Array: arg.join('/')

when Hash

ret = []

each{|k,v| ret << "#{k}=#{v}"}

ret = ret.join('&')

'?' + ret

else: arg.to_s

end

end

Page 59: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Good

def url_for(*args)

root + args.to_params

end

Page 60: Rails Is From Mars  Ruby Is From Venus Presentation 1

class Array

def to_params

map{|a| a.to_params}.join('/').

gsub('/?', '?')

end

end

Page 61: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Array

%w(foo bar bazz).to_params # "/foo/bar/bazz"

# Hash

{:foo => :bar}.to_params # "?foo=bar"

Page 62: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHY?

• Ruby uses coercion in many places

• 1.to_s

• (1..10).to_a

• Writing your own coercion method can help you use Ruby’s ducktyping.

• Separation of concerns.

Page 63: Rails Is From Mars  Ruby Is From Venus Presentation 1

RAILS PERFORMANCEInsert your “scaling” and “premature optimization” jokes here.

Page 64: Rails Is From Mars  Ruby Is From Venus Presentation 1

Yes, I went there.

Page 65: Rails Is From Mars  Ruby Is From Venus Presentation 1

Slow is only meaningfulin comparison.

Page 66: Rails Is From Mars  Ruby Is From Venus Presentation 1

Ruby is slow? Compared to what?

Page 67: Rails Is From Mars  Ruby Is From Venus Presentation 1

Is your database slow?

Page 68: Rails Is From Mars  Ruby Is From Venus Presentation 1

Are your views slow?

Page 69: Rails Is From Mars  Ruby Is From Venus Presentation 1

Is your app server slow?

Page 70: Rails Is From Mars  Ruby Is From Venus Presentation 1

Are you using HTTP via carrier pigeon?

Page 71: Rails Is From Mars  Ruby Is From Venus Presentation 1

If you don’t know where the slow is, you’re not ready to optimize.

Page 72: Rails Is From Mars  Ruby Is From Venus Presentation 1

Don’t optimize prematurely, but don’t pessimize either.

Page 73: Rails Is From Mars  Ruby Is From Venus Presentation 1

Don’t write code you know will never, ever be fast.

Page 74: Rails Is From Mars  Ruby Is From Venus Presentation 1

# Really Bad (Optimally Pessimum)

class Ballot < ActiveRecord::Base

def <=>(other)

votes.count <=> other.votes.count

end

end

# Good (Potentially Optimum)

class Ballot < ActiveRecord::Base

# With a counter_cache on votes

default_scope :order => :votes_count

end

Page 75: Rails Is From Mars  Ruby Is From Venus Presentation 1

IN OTHER WORDS

• Don’t worry about speed until you know where the slow is.

• Worry about writing simply and expressively.

• Well written code is easy to optimize for performance later.

• Don’t write something you know will never, ever be fast.

Page 76: Rails Is From Mars  Ruby Is From Venus Presentation 1

IN CONCLUSION

• Ruby is fun and easy (and friendly!).

• Ruby will make you happy.

• Be more thoughtful in the way you treat Ruby.

• The more Ruby you know, the better you can become at Rails.

• If you love Rails, you should love Ruby too.

• Also, don’t be premature. No one likes that.

Page 77: Rails Is From Mars  Ruby Is From Venus Presentation 1

WHO AM I?• My name is Rein Henrichs.

• That’s pronounced like “rain”.

• But spelled differently.

• I work at

• I blog at reinh.com

• I twitter @reinh

• I like candlelit dinners and long walks on the beach.