Things Well Made

Craftsmanship excellence and the design of beautiful things
Browsing Software development

Thing well made: detecting changes in Rails objects

March7

I’m a pretty big fan of Ruby on Rails and occasionally I come across some particularly well-made parts. Rails’ ability to track changes in objects in memory has been around well over a year but its staggering how much thought goes into what people might want.

How do I check the value of an object before it was changed?

Let’s say you want to keep track of the total amount a user has across all orders. Rails has built-in not only a convenient place to update the value, but it has the previous value so you can find out the difference.

So nice!

In the example below,

  • there is a field in the order table called ‘order_total’.
  • order_total_change is Rails-generated array returning e.g. [20.53, 21.52] if say, the order_total had gone up by 1.99.
  • order_total_changed? is also Rails-autogenerated.
class Order
...
before_update :update_total_user_spend
...
def update_total_user_spend
  if order_total_changed?
    previous_value, new_value = self.order_total_change
    self.user.total_spend += new_value - previous_value
    self.user.save
  end
end

Rock on!

(yes and I do need to adjust this blog template so it has a little more room for CONTENT :)

Bruce on Rails

Thing well made: fast physics modeling of 2D cloth in javascript

March5

Andrew Hoyer’s javascript skills are awesome as you can see with this 2D cloth physics simulation with gravity written in Javascript. He uses Taylor expansion to avoid square roots in javascript (which are expensive). The code he’s written is also a thing well made — mostly very clear, concise code (except the occasional curiosity like ‘inv_m’) which by and large doesn’t need any docs.

What makes this simulation special is the speed at which everything is computed. Javascript (the language this is written in) is not exactly the most efficient language for this type of computation. This being said, much time was spent squeezing out every little detail that slows things down.

One of his inspirations is the 2D cloth simulation in a java applet using the Processing library by JRC313.com

NOTE if the simulation below is jerky, try some of the faster browsers such as Google Chrome * or Safari as their javascript support is faster than Firefox / IE currently as of March 2010.

* In the interests of full disclosure this is my own opinion but Google is my employer. But Chrome is still faster :)

Thing well made: insanely simple background tasks with delayed_job

January18

If you’re doing web development of any substance the first thing you’ll hit is how to avoid users having to wait for things that take ‘a long time’. Like sending emails.

A naive implementation for reset password could be:

  1. User requests ‘place order’
  2. Server processes request and sends order details to user’s email account
  3. User is presented message to this effect

Thing is, (2) could take ages (particularly if you’re using free SMTP remailer services like Gmail where it typically takes 2-4s by design).

What would be nicer is something like this:

  1. User requests ‘place order’
  2. Server creates a task to do this in the background
  3. Server immediately responds with a ‘order details will be sent very shortly’
  4. User receives order details more or less the same time as in first scenario

Because (3) is shown to the user faster, they can get on and do other things, and also your server’s request processor isn’t held up.

This seems obvious but its amazing how hard this queuing / background processing is to get right (e.g. if its in the background, how do you make sure the context is set up when its run later, etc). I think for a large number of cases, the Ruby on Rails gem ‘delayed_job’ does an incredible job. Check this out:

Original code:

Order.place_order(order_request)

or 100% equivalently in ruby:

Order.send(:place_order, order_request)

With delayed_job, here’s the new code that is run in the background:

Order.send_later( :place_order, order_request )

delayed_job adds this feature to every class in Rails! So sending email is the same:

Mailer.send_later( :deliver_order_confirmation, self )

It’s incredibly well designed because it is so simple. A thing well made indeed. Other parts are simple as well:

  • delayed_job only needs a single server but supports more than one very easily (just create new instances)
  • As its a plugin adding support to your Rails system is a single command (script/plugin install delayed_job).

Notes

Right now delayed_job is heavily forked on github — there seem to be about 20+ variations. The one that most seem to use is collectiveidea’s branch:

Delated_job or DJ encapsulates the common pattern of asynchronously executing longer tasks in the background.

via collectiveidea’s delayed_job at master – GitHub.

… although for automated testing there is a great feature in hashrocket’s variation that allows you to force the background job to be, well, not background any more with a ’synchrony’ flag. So I’ve used a combination of both. I’m sure this will settle down the coming months.

What Beautiful HTML Code Looks Like | CSS-Tricks

January5

This is what this site is all about: excellence in craftsmanship. Things well made, like HTML pages:

It gets me to thinking, what makes beautiful code? In HTML, it comes down to craftsmanship. Let’s take a look at some markup written they way markup should be written and see how beautiful it can be.

via What Beautiful HTML Code Looks Like | CSS-Tricks.

I’ve always appreciated things that have had love, care and attention put into them. Where its obvious that the thing that was created was created for reasons above purely commercial gain.

Don McGlashan says it best in ‘Thing Well Made’ by the Mutton Birds:

To make a thing like that you’d need to know what you were about.
You’d have to know where you were going and go there in a straight line.
And everything else you’d have to shut right out.
Can you see the man who made that?
Can you see him putting it down and standing back?
Can you see the moment when he said “That’s it. That’s perfect.”?
At a time like that you wouldn’t care about your job,
Or your mortgage, or the fight you had with your wife.
‘Cause when a man holds a thing well made,
There’s connection,
There’s completeness when a man holds a thing well made.

Watch live recording of A Thing Well Made by the Mutton Birds

This site is dedicated to this song and to all those people in the world who create Things Well Made.



 

You need to log in to vote

The blog owner requires users to be logged in to be able to vote for this post.

Alternatively, if you do not have an account yet you can create one here.

Powered by Vote It Up