gsinclair/whitestone — Issue #2: Object vs Method
Author: trans
Created: 2012-01-28 12:56:19+00:00
State: open

This is more of a question than and "issue". I'm curious about a design decision you made with Whitestone, b/c I have considered the same approach with my Assay project: Each assertion made in a test creates a new object, an instance of an assertion class. I am wondering why you choose to do this over just using a method call?

In Assay I created classes for each assertion too, but I put the assertion logic in class methods so I didn't have to instantiate an object for each assertion. I sort of prefer the idea of the way you did it, and had originally gone that route myself, but in the end wasn't sure it was worth the speed and memory footprint. For example, I created a benchmark comparing the two:

```ruby
require 'benchmark'

class X
def self.call(a,b)
a == b
end
def initialize(a,b)
@a, @b = a, b
end
def call
@a == @b
end
end

n = 1000000
Benchmark.bm do |x|
x.report("method") { n.times{ X.call(1,1) } }
x.report("object") { n.times{ X.new(1,1).call } }
end
```

The results:

user system total real
method 0.280000 0.000000 0.280000 ( 0.280966)
object 1.200000 0.000000 1.200000 ( 1.200855)

It's about 4 times slower, but maybe it doesn't matter since it can churn through a million of these in just over a second. On the other hand the memory footprint, est. about 100 bytes per object, a million assertion would cost 100MB. That pretty sizable but then how many test suites are really going to have a million assertions? Moreover these are all throw away objects, so they come and go very quickly.

So I am still considering moving back to the object design. But haven't been able to make up my mind either way for sure. I thought it would be good to ask your thoughts on the matter seeing that Whitestone took this approach.