Nick Kallen's blog
The Law of Demeter is a Piffle
One of The Blabs' most controversial articles was Lovely Demeter, Meter Maid, in which Pivotal and Thoughtworks battle over which Agile consultancy has the better understanding of the Law of Demeter, and which has better hair and music taste (seriously).
I have never found this "law" very persuasive.
- The bizarre, culturally loaded analogy about a paperboy and a wallet says nothing insightful about encapsulation boundaries as they arise in real software systems.
- The blogosphere's endless scholastic hermeneutics of the law's 4 allowances for message sending is a masturbatory philosophical enterprise with nothing relevant to real-world software.
- The Mockist's insistence on easy mockability is of dubious merit--build better mocking frameworks!
- And the few practical, real merits that arise in from following the Law of Demeter are better arrived at using other techniques, such as "Tell, Don't Ask".
Here Are Two Examples, one where I violate the Law of Demeter, and another where I don't.
I wrote this code recently, in flagrant violation of the Law:
cookies[:store_id] = @login.store.id
Suppose @login is not an ActiveRecord object, it does not automatically have a #store_id method. Should I create a delegator for this?
class Login
def store_id
store.id
end
end
This is pretty silly. The store_id is not an attribute of the login; rather it's an attribute of the store, and the store is an attribute of the login. The delegator is needless code cruft to replace a dot with an underscore, it smells of the endless boilerplate Java code of my youth. Demeter be damned.
On the other hand, here is a refactoring I did, incidentally complying with the law of Demeter:
Here is the original, Demeter-violating code:
def find_attribute_given_name(name)
attributes.detect { |a| a.name_or_alias == name }
end
The call to == here is the violation of Demeter. I later replaced this with:
attributes.detect { |a| a.named?(name) }
The latter complies with the "law". And it's much better code. But was I lead to the improvement to this by Demeter? No, I was lead to it by a better understanding of the encapsulation boundaries of the object (#name_or_alias became private) and by a desire to have my code be more terse and clear. a.named?(name) is the most terse explanation of the intended computation that I can think of.
Demeter be damned.
Ruby Pearls vol. 1 - The Splat
Over the next week or so I'll be sharing Ruby idioms and flourishes that I quite like. Today I'd I'll show a few tiny uses of splat! that make me tremble with delight.
Splat! - For Beginners
Splat! is the star (*) operator, typically used in Ruby for defining methods that take an unlimited number of arguments:
def sprintf(string, *args)
end
It can also be used to convert an array to the multiple-argument form when invoking a function:
some_ints = [1,2,3]
sprintf("%i %i %i", *some_ints)
Splat! - For Wizards
Array to Hash Conversion
The best use of splat! for invoking a infinite-arity functions I've ever seen is the recipe for converting an array to a hash. Suppose you have an array of pairs:
array = [[key_1, value_1], [key_2, value_2], ... [key_n, value_n]]
You would like to produce from it the hash: {key1 => value1 ... } You could inject down the array, everybody loves inject, but there is a better way:
Hash[*array.flatten]
Amazing right? This relies on the the fact that the Hash class implements the [] (brackets) operator and behaves thusly:
Hash[key1, value1, ...] = { key1 => value1, ... }
Heads or tails?
Splat! can be used for more than just method definition and invocation. My personal favorite use is destructuring assignment. I read this in Active Record's source code recently:
def sanitize_sql_array(ary)
statement, *values = ary
...
end
This is invoked when you do something like User.find(:all, :conditions => ['first_name = ? and last_name = ?', 'nick', 'kallen']). Splat! is used here is to get the head and tail of the conditions array. Of course, you could use always use shift, but the functional style used here is quite beautiful. Consider another example:
first, second, *rest = ary
One final trivium (#to_splat aka #to_ary)
You can actually customize the behavior of the splat operator. In Ruby 1.8, implement #to_ary and in 1.9 it's #to_splat. For example
class Foo
def to_ary
[1,2,3]
end
end
a, *b = Foo.new
a # => 1
b # => [2,3]
This also works for method invocation:
some_method(*Foo.new) == some_method(1,2,3)
When I first learned this at RubyConf I thought this was mind-blowing. I have since never used it.
Ninja Patching jQuery
Jonathan and I love jQuery's extended psuedo-selectors:
:input- Matches all input, textarea, select and button elements.:text- Matches all input elements of type text.:password- Matches all input elements of type password.:hidden- Matches all elements that are hidden, or input elements of type * "hidden".:visible- Matches all elements that are visible.- and so on
These aren't actually part of the CSS spec, but they're incredibly useful and can be chained:
$(':input:visible') // => finds all visible inputs
We wanted to customize the behaviors of :text and :visible:
- We wanted
:textto return both<input type="text">AND<textarea> - We wanted
:visibleto return elements that aren't directlydisplay:noneorvisibility:hidden, nor are their parents display:none orvisibility:hidden
So, we decided to customize this behavior:
jQuery.extend(jQuery.expr[":"], {
text : "(a.tagName=='INPUT' && a.type=='text') || (a.tagName=='TEXTAREA')",
visible : '"hidden"!=a.type && jQuery.css(a,"display")!="none" && jQuery.css(a,"visibility")!="hidden" && (jQuery(a).parent(":hidden").size() == 0)',
hidden : 'document != a && ("hidden"==a.type || jQuery.css(a,"display")=="none" || jQuery.css(a,"visibility")=="hidden" || (jQuery(a).parent(":hidden").size() > 0))'
});
So how would you like to ninja-patch jQuery's custom pseudo-selectors?
Making Ruby Look Like Smalltalk Haskell Erlang Ruby

Inspired by Haskell
Did you ever want to write Ruby Code like:
x = 1
increment(x).by(6)
Now you can:
def increment(variable)
chain do
by do |delta|
variable + delta
end
end
end
This is an OO version of a technique called Currying:
g = 'hello world'.index_of('o')
h = g.starting_at(6)
Inspired by Smalltalk:
'hello world' indexOf: $o startingAt: 6
Let's do this in Ruby:
class String
def index_of(substring)
chain do
starting_at do |starting_at|
...
end
end
end
end
Now, in Ruby:
'hello world'.index_of('o').starting_at(6)
Inspired by Erlang
Here is pseudo-code for an interesting iteration pattern. If the Actor receives 'lock' it will not respond to any messages until it receives 'unlock':
loop(X) ->
receive
'incr' -> loop(X+1)
'lock' ->
receive
'unlock' ->
loop(X);
end
end.
The Ruby equivalent:
def loop(x)
puts x # added puts just to see what's going on
chain do
incr do
loop(x+1)
end
lock do
unlock do
loop(x)
end
end
end
end
Try this:
loop(1).incr.incr.incr => prints 1, 2, 3, then 4
Now, the finale: We can respond to incr any number of times till we're locked; then, we respond to no messages other than unlock; once we've received unlock we proceed as before.
loop(1).incr.lock.incr => prints 1, 2, then raises an exception.
loop(1).incr.lock.unlock.incr => prints 1, 2, then 3
How does this work?
The call to chain do ... end creates a new Chain object with the block passed in to the constructor. Chain is kind of "blank slate": all methods inherited from Object are undefined so that any messages it receives go through method missing. The block the Chain is instantiated with is instance-eval'd in the chain's context, and all method invocations go through method missing (because of the blank slate). Method missing has two cases. It either dynamically defines a method returning a new link in the Chain (in the case of nested chaining), or it delegates the method back to the object that constructed the chain in the first place. Let's consider examples of these two cases.
Case 1, dynamically defining a new method:
def foo
chain do
a do # define a method named :a on the Chain.
1
end
end
end
foo.a => 1
Case 2, delegating the method back the the creator of the Chain:
def bar
1
end
def foo
chain do
a do
bar # invokes the bar defined above
end
end
end
foo.a => 1
Nested chaining is just a variation on Case 1:
def foo
chain do
a do
b do # create a nested Chain (i.e., a Link)
1
end
end
end
end
foo.a.b => 1
The only gotcha is knowing whether a method invoked with a block belongs to the object that created the chain or is a nested chain:
def b(&block)
end
def foo
chain do
a do
b do # is this the above b, or a nested Chain?
...
end
end
end
end
We prioritize the #b defined on the parent object, rather than created a nested chain (I feel this is more intuitive).
Here is the source code:
require 'rubygems'
require 'active_support'
class Chain
instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|^instance_exec$)/ }
delegate :define_method, :respond_to, :to => :__caller
attr_accessor :__caller
def __has_links?
@__has_links
end
def initialize(*args, &block)
if block_given?
self.__caller = eval("self", block.binding)
instance_exec *args, &block
end
end
def method_missing(method, *args, &block)
if block_given? && !__caller.respond_to?(method)
@__has_links = true
metaclass.module_eval do
define_method method do |*args|
__link(*args, &block)
end
end
else
__caller.send(method, *args, &block)
end
end
private
def __link(*args, &block)
link = Chain.new
link.__caller = __caller
result = link.instance_exec(*args, &block)
link.__has_links?? link : result
end
def metaclass
class << self
self
end
end
end
def chain(&block)
Chain.new &block
end
Testing Views by Not Testing Views: Or, The Presenter Pattern
Err The Blog asks: "What's the best way to test views?"
I think the best way to test views is not to test views. Extract all logic from the view into a model or presenter where it can be unit tested. Your views are then mostly declarative and there's minimal need to test them.
Here's an example of the "presenter pattern".
def create_or_destroy_friendship_link(friend)
if current_user.friends_with?(friend)
destroy_friendship_link(friend)
else
create_friendship_link(friend)
end
end
You don't need a special class to do a Presenter; a good old-fashioned layer of abstraction will do. The basic idea is to write all conditional+iterative view logic in such a way as to never call a Rails helper directly, or generate any HTML directly, or generate any strings directly. The logic merely delegates to other methods closer to the metal.
Tests then become fairly simple. Write tests of the higher-level conditional/iterative logic in terms of the lower-level methods:
describe FriendshipsHelper, '#create_or_destroy...' do
it "renders create link when two users are not friends" do
log_in(users(:bob))
bob.should_not be_friends_with(users(:amy))
create_or_destroy_friendship_link.should == create_friendship_link
end
end
This minimizes the need for view specs. I find in practice that a high percentage of view tests slow development down"”they're implemented not to aid development (since you typically debug views in-browser), but to prevent regression (i.e., they minimize the likelihood of introducing defects later). But since views are one of the most variable parts of a web application, regression tests are of the least value.
As a side note, I love integrate_views -- not because I like to make assertions about the view in my controller tests, but because I hate mocks! I want a controller test to fail if I have a syntax error in my view!
CUDdly Models

Better Rails Code through
...ActiveRecords with no public methods that have side-effects--other than Create, Update, and Destroy (CUD).
CUDly Models
In a typical web application, some "triggered actions" or "side-effects" occur in response to various events. Examples: A confirmation email is sent when a User registers. A ping is sent to Technorati when a BlogArticle is published. Cookies are set when a user logs in. Two Friendship objects are created between two users when one approves the other's FriendshipRequest.
Often these side-effects are modeled with public methods on an ActiveRecord. For instance,
class FriendshipRequest < ActiveRecord::Base
def accept!
self.status = ACCEPTED
save!
Friendship.create!(:from => from, :to => to)
Friendship.create!(:to => from, :from => to)
end
end
This is code is dangerous, as shown below. The principal of CUDly models is eliminate all public methods on your ActiveRecord that have any side effects. CUDly models are much safer. Let's replace the dangerous code with the equivalent, more friendly, CUDly code:
class FriendshipRequest
after_update :create_mutual_friendship
private
def send_email_on_accept
if status == ACCEPTED
Friendship.create!(:from => from, :to => to)
Friendship.create!(:to => from, :from => to)
end
end
end
The above CUDly code exploits the richness of ActiveRecord's lifecycle callbacks to trigger the side-effect. This illustrates a general principle: in a perfectly CUDly world, constrain the interface to your models such that no methods have side effects other than Create, Update, and Destroy.
There are several virtues to CUDly models: encapsulation, transactions, consistent interface, and lifecycle power.
Encapsulation
Your side-effects can be hidden behind the standard ActiveRecord interface. And your Controllers will be skinny as can be! Compare:
class FriendshipRequestsController
def update
friendship_request = FriendshipRequest.find(params[:id])
if params[:friendship_request][:state] == ACCEPTED
friendship_request.accept!
else
friendship_request.reject!
end
end
end
Instead, you can write an equivalent, Formulaic Controller:
class FriendshipRequestsController
def update
friendship_request = FriendshipRequest.find(params[:id])
friendship_request.attributes = params[:friendship_request]
friendship_request.save
end
end
Transactions
Thanks to ActiveRecord, the CUDly approach has rich transactional semantics. In the CUDly implementation, if any of the Friendship.create! invocations fails, the entire transaction is rolled back, meaning the you cannot put the world in an incoherent state (where Tom and Dick are only half-friends). The equivalent non-CUDly code is the onerous and obese:
class FriendshipRequest < ActiveRecord::Base
def accept!
self.status = ACCEPTED
self.class.transaction do
save!
Friendship.create!(:from => from, :to => to)
Friendship.create!(:to => from, :from => to)
end
end
end
Who wants to cuddle with code like that?!
A Rich Consistent Interface
ActiveRecord already has a wonderful pattern: build, then test. It's so simple, and yet so powerful. Why not re-use it?
f = Friendship.new
if f.save ...
#Save returns true or false, and it sets errors on the model to be displayed by the user. Using CUDly code, you continue to do that:
class FriendshipRequestsController
def update
friendship_request = FriendshipRequest.find(params[:id])
friendship_request.attributes = params[:friendship_request]
if friendship_request.save
flash[:notice] = ...
else
flash[:error] = ...
render :action => :edit
end
end
end
Try doing that with unCUDly #accept and #reject methods! Surely it will be unCUDly and ungodly!
Leverage the Power of the Lifecycle
A fundamental limitation of public, side-effecting methods is that they can be called at any time for any reason. Suppose we had something like this:
class MyModel < ...
def foo=(bar)
send_an_email!
end
This could be called as part of #attributes=, triggering the email deilvery regardless of whether the model was valid and could be saved! In the CUDly implementation, on the other hand:
class MyModel
after_save :send_an_email
def send_an_email
...
Thanks to the flexibility of #before_validation_on_create, #before_create, #after_update, #after_initialize, #after_find, etc., you can ensure that your triggered action only happens after successful validation, or regardless of validation, or only on update, or only on destroy--you name it! Try enforcing that with a public method!
Make Your Code a CUDly Code
There is a beautiful symmetry in having all side-effecting methods "funneled" through the three "dangerous" methods (create, update, and destroy). It appeals to my sense of elegance and order. I've used this design strategy 100% for the last few months and it's been a smashing success! It truly is the way ActiveRecord was meant to be used. So give it a try!
Advanced Proxy Usage, Part I
One of the more underutilized features of ActiveRecord is the Assocation Proxy. But they are also one of the most powerful weapons in the ActiveRecord armory, and Rails apps that take advantage of them are better organized and easier to maintain.
What is a Proxy?
When in an ActiveRecord you declare an Association:
class Hand < ActiveRecord::Base
has_many :fingers
end
Instances of Hand now have a fingers method. Contrary to appearances, and contrary to the LIE told to you by hand.fingers.class, the fingers method does not return an Array of Fingers. Rather it returns a Proxy object, one that smells and tastes like an Array of fingers but actually has a rich creamy behavior all its own.
Scoped Access
The most basic use of Proxies is to "scope" the reading and writing of your ActiveRecords. For example, if you have a controller that allows CRUD on a User's Assets, you can read and write to the collection of Assets as in the following examples:
@asset = current_user.assets.find(params[:id])
@asset = current_user.assets.create(params[:asset])
@asset = current_user.assets.build(params[:asset]) # equivalent to 'new'-ing an object rather than 'create'-ing it.
There are other ways of doing this, of course:
@asset = Asset.create({:user => current_user}.merge(params[:asset))
But the Proxy Code is much better: not only is the Proxy code terse, but it meaningfully expresses the relationship between objects in your domain: Users have many assets; this Asset is created in the context of this User.
Special Queries (or Custom Finders)
The various Association declarations--has_many, belongs_to, etc.--allow you to express much more than a simple Foreign Key relation. We can richly express in the Proxy Declarations concepts like 'Assets that belong to a User' and 'Assets that don't belong to a User':
current_user.my_assets
current_user.other_assets
simply by declaring:
class User
has_many :my_assets, :class_name => 'Asset', :conditions => 'user_id = #{id}'
has_many :other_assets, :class_name => 'Asset', :conditions => 'user_id != #{id}'
end
Notice, in this last example, something peculiar: the use of single quotes ('') with variable substitution (#{...}). This is not a typo: the use of double-quotes, would perform variable interpolation when the has_many declaration is invoked. This is in the class-context--i.e., there is no instance yet. Rails always calls eval with a Binding of self when a call to one of the Proxy methods is performed, ensuring that this all comes together.
Let's consider an alternative to this approach: declaring finders as instance methods.
class User
def other_assets
assets.find(:conditions => ["user_id != ?", id])
end
end
What's wrong with this approach? Well, if you want to use this query in anything non-trivial--such as selecting the first ten of a User's Assets--you have to write fancy code:
def other_assets(options)
assets.find({:conditions => ...}.merge(options))
end
But good luck using this strategy to do pagination. You need to define my_assets and my_assets_count, too--have fun keeping your code DRY. With a proxy, we can just do something like:
current_user.my_assets.count
current_user.my_assets.sum
current_user.my_assets.average(:price)
In fact, all the richness of ActiveRecord class methods (and any other class methods of the Target type) are available to you here. Want to find all of a User's assets that are in State pending?
current_user.my_assets.find_by_state(State[:pending])
Another example:
class Asset
def self.find_portrait_assets
find(:all, :conditions => 'height > width')
end
end
Then,
current_user.my_assets.find_portrait_assets
returns only those portrait assets owned by a user.
Proxy Options
Proxy declarations accept a number of interesting parameters. There are even "lifecycle" callbacks, like after_add, and before_destroy just like a normal ActiveRecord has before_create and so forth. You can hook into these by using an option.
class User
has_many :assets, :after_add => [:send_email] do
end
def send_email(r)
end
end
This after_add could be defined in the Asset class. But suppose Assets had a Polymorphic association. Both Users and Articles have many Assets. Our Business Rule is only to send email when a User adds an Asset, not an Article. We could write:
class Asset
def after_create
case owner
when User
# send email
when Article
end
end
But this is clumsy! When we have logic to express about the relationship between things, the Proxy is the right place for it. Anywhere else is just smearing logic throughout your code.
Proxy Extensions
Consider the following example:
has_many :assets do
def to_s
self.join(',')
end
end
You can actually extend your Proxy Objects with an Anonymous module! When you have logic that applies to a Collection of ActiveRecords, your has_many Proxy is probably the proper place for it. For example:
class Table
has_many :cells do
def to_matrix
# convert from list to matrix form.
end
end
end
Another example of this technique is the following. Suppose an Asset as many Versions, such as small, medium, etc. We'd prefer a shorter way of finding the proper version of an Asset than saying asset.versions.find_by_name('thumbnail'), we'd like to just say asset.versions[:thumbnail]. Just define the brackets ([]) operator on the Proxy:
class Asset
has_many :versions, :class_name => 'Asset', :foreign_key => :parent_id do
def [](version_name)
find_by_name(version_name)
end
end
end
Suppose we want to go one step further. If a particular version doesn't exist, it shall be created on-the-fly:
def [](version_name)
if version = find_by_name(version_name)
version
else
# create a new version here.
end
end
Advanced Extensions
In some cases, we want to write generic Extensions--these should work regardless of the particular classes involved. In the context of a Proxy there are three methods you should be aware of: proxy_owner, proxy_target, and proxy_reflection.
Suppose we want to implement something like the build method, but one that doesn't have the side effect of adding it to the owner in memory:
has_many :foo do
def new(options = {})
proxy_reflection.klass.new({proxy_reflection.primary_key_name => proxy_owner.id}.merge(options))
end
end
Extensions are so useful--it just requires a little imagination--that I'm going to give one more example, this one apropos of Access Control:
class User
has_many :draft_articles do
def readable_by?(user)
user == proxy_owner
end
end
end
Some Miscellany
The
has_oneandbelongs_toProxies behave a bit oddly: here,cyclops.build_eyeis used rather than the more obviouscyclops.eye.build.In general,
has_oneandbelongs_towill shadow methods on the Target. Don't name any database columnstargetorowner, for instance. This is one of the biggest complaints against the current implementation of Proxies!Both build and create will work even if the Proxy Owner is new. For example,
u = User.new; u.assets.build; u.save. In this example, both objects will be saved with the Foreign Key set correctly.Both build and create can take an array of attributes hashes. For example:
u.assets.build([{...}, {...}]). This will build two assets at once. (This is quite nice where in a Controller you have a form that allows the upload of multiple Assets at once. The Controller code looks identical (in simple cases) regardless of whether the form allows a single or multiple upload!)
That's the basic idea. In part II of this Article (to be released in the coming weeks), I will discuss 'static' Proxy methods and I will release version 0.1 of a new plugin that builds upon a lot of exciting work in this area. In the meantime, check this out.
Access Control & Permissions in Rails
Access Control is a simple idea. We want company employees to be able to delete inappropriate content; but random Users cannot. Here I propose one way to implement Access Control that has the particular advantage of being very general, very concise, and unlikely to be violated. I call it RESTful Access Control.
Creating Multiple Models in One Action
One of the issues in my previous post The Controller Action that sparked some interest is the handling of the creation of multiple Models in one Action. In this post I shall elaborate on this problem in some detail, first considering the cases where in constructing the Dependent Object no data are needed from the querystring/params, and secondly where such data are necessary (and where we have a complicated nested Form). Let's head right in to an example.
With No Extra Params to Worry About
Your site has Groups and Memberships. Our Business Rule is: when a user creates a Group, she should also be a Member of that Group. Following the Controller Formula, we know the solution in advance:
class GroupsController < ActionController::Base
def create
group = logged_in_user.groups.build(params[:group])
raise SecurityTransgressionError.new unless logged_in_user.can_create?(group)
if group.save
...
end
end
end
This leaves unresolved where to put our Business Rule...
The Controller Formula
When I introduce a programmer to Rails I encourage them to read the article Skinny Controller, Fat Model. My only complaint about this article is that it applies the Skinny/Fat Pattern in a vague way, proceeding as if by intuition. I've found instead that there is a formulaic way to produce excellent Controller code--the Controller Formula
Skinny/Fat: Code Complexity vs. Abstraction Boundaries vs. The Formula
The Skinny/Fat pattern is typically stated in terms of Lines of Code. Your Controllers should have few Lines of Code; where there are many, move them to the Model. This rule has more exceptions than it has applications, so we can state the intent of the Pattern more precisely in terms of Abstraction Boundaries. Remove all Business Logic from your Controllers and put it in the model. Your Controllers are only responsible for mapping between URLs (including other HTTP Request data), coordinating with your Models and your Views, and channeling that back to an HTTP response. Along the way, Controllers may do Access Control, but little more. These instructions are precise, but following them requires intuition and subtle reasoning. The purpose of this post is to avoid subtle heuristics like Abstraction and Code Complexity. There is a Formula. Let's see an example that follows the Formula:
def create
model = Model.new(params[:model])
raise SecurityTransgressionError.new unless logged_in_user.can_update?(model)
if model.save
render ...
else
...
end
end
What's going on here? The Action channels user input to the model. It raises an Exception if the user lacks permission to perform this Action. Validation is performed by the model. Given the results, the Controller renders the appropriate Template to the HTTP Response.







