Software dev, tech, mind hacks and the occasional personal bit

Category: Technical Page 13 of 14

Improve Rails Performance Through Eternal Browser Caching of Assets

I’ve been working on a rails app which has got quite a number of pages that share the same two css files and 3 javascript files. However, every time I visited any page of the app, all of javascripts and css files were being loaded from the server. Not good – site was very slow. Mucking around with ‘about:cache’ command in Firefox revealed that the css and javascript files had expiry dates set in the past – ie, no caching of them at all. Also, all the links to sylesheets and javascript files generated by rails had ?[some long number] after them. Some research on the web revealed that this is a new rails feature for caching – the long number is a timestamp for when the asset was last modified.

Okay, so why were these assets not being cached? A quick check with wget –save-headers revealed that the web server was sending a nocache directive to the browser. This seems to be the default setup for webrick and also for my shared apache hosting on railsplayground. Considering the new rails asset management system with the ?[last modified timestamp] in the URLs, nocache seems wrong. The browser should never expire the cache since rails will handle cache invalidation by updating the asset url with a new timestamp.

So, how can we implement no/very long cache expiry? In apache, you can use mod_expires or mod_headers to do this. My shared hosting does not support mod_expires, so I went for mod_headers in my .htaccess file.

Using mod_headers:

<FilesMatch "\.*$">
Header set Cache-Control "max-age=29030400"
</FilesMatch>

OR using mod_expires:

ExpiresActive On
ExpiresDefault "access plus 1 year"

Either of the the above will set up a cache expiry time of one year for all content (best you only do this for your rails app directories).

With a cache expiry time of one year in place, my rails apps run much much faster.

Naked Economics by Charles Wheelan

Just finished reading ‘Naked Economics: Undressing the dismal science’. It was a present from a friend, and I’ve been meaning to read it for a while. Glad I finally got around to it.

From the title, I assumed the book aimed to point out the failures of economics as a science. Not so at all – it was written by an economist and provides a high level overview of capitalism in layman’s terms.

Here’s some interesting questions and explanations from the book:

  • Why do we have money? So that we can indirectly swap our labour or goods for the things we want, even if the person with the things we want is not interested in our labour or goods. Without money, we would need to barter. That’s fine if you’re swapping chickens for rice. But what happens if you do web design, and you want meat for dinner, and the butcher does not want a website?
  • Money has value only because we all believe it does. We have faith that if we sell something (ie, convert it to the common value unit), we will then be able to swap that money for something we want.
  • Why have markets anyway? Markets produce what people want – ie, what people are willing to pay for (or at least what we are convinced into wanting through advertising etc).
  • Why not set the price of everything rather than letting it get worked out in a market? Well, it would be an enormous job, and things would not reflect the cost of production. Eg, bird flu wipes out half of the chickens in the world. There are now less chickens to go around so chicken becomes more expensive. People who really want chicken can still get it, but it costs them more. People who don’t care as much or can’t afford it eat fish or beef instead. If the cost of chicken was a constant mandated by the state, chicken distribution would need to be mandated in some other way. If there’s not enough chicken for everyone who wants it, who should get it? First come first served? Political clout? Personal connections?
  • Markets destroy. A new way to mechanize weaving may make thousands unemployed and destroy towns and communities. But according to Wheelan, the country as a whole is better off as we are able to produce more for less cost, hence increasing our standard of living. Ie, as a consumer, you may now be able to buy a shirt for half the price.
  • In politics, small motivated groups often drive policy. Eg, the general population does not care much one way or other on a subsidy on growing alfalfa. It might cost each person in Australia 0.01c per year. However, if the subsidy was to be removed, and the alfalfa farmers would care a lot. It may well mean their livelihoods so they would demonstrate, make campaign donations, vote as a block and generally make as much fuss as possible to make sure the subsidy was not removed.
  • Why do people work in sweatshops? According to Wheelan, the pay is generally better than for other jobs available – ie, sweatshops are not the cause of the problem, rather a symptom of the general poverty and lack of opportunity in the area.
  • Why is free trade good? So that everyone can do what they do best – ie, specialise to the max. The idea being that if everyone works on what they are most good at, then productivity overall is higher.
  • Why are tariffs bad? Because they support local industries that are not viable – ie, a poor uses of resources.
  • Why do we need governments? To provide the rails for capitalism. Eg, to enforce laws (so you can’t just kill me and take my stuff), to regulate the excesses of the free market and to provide goods and services that people need but that free markets will never provide. Also, to do things that individuals cannot do alone, but are in the interest of the population as a whole – eg, build infrastructure.
  • Care for the environment is a luxury good – ie, if you and your family are starving, cutting down trees to sell for food seems like a pretty good idea.
  • Companies destroy the environment because the current monetary cost of environmental destruction is usually minimal. If the full cost of environmental destruction was factored into the market (eg, companies have to pay for pollution and destruction), then environmental destruction would slow dramatically.
  • Who can create new money? The reserve bank, and it does it by buying bonds from banks with money that did not previously exist.
  • How can the reserve bank change interest rates? It can sell government bonds at its target rate and it can buy bonds (with brand new money) or sell bonds from/to trading banks to influence the amount of cash the banks have to lend. Eg, lots of cash at a trading bank means they’ll lower the interest rates so as to rent out the money.
  • How come the economy can go into recession for no real reason? If people are worried, they don’t spend. If people don’t spend, then companies can’t afford new/current investment. People get sacked and then can spend even less. People get more worried and the cycle continues.
  • Why is the level of savings in a country important? Money in the bank means it can be lent out to people who want to use it to create new businesses or expand current businesses. Access to capital allows growth.

I wouldn’t take these on face value, and I would certainly question some of the assumptions on which they are based. However, they provide some interesting areas for further thought.

Starting at ThoughtWorks: First Five Weeks

Well, I’ve been with ThoughtWorks for just over 5 weeks now, and I thought I’d write down some thoughts before the hiring and joining process got lost in the misty swamp of my memory.

Hiring process
To cut a long story short, I did a phone screen with HR, a coding test and then some fairly quick aptitude and personality tests and finally 3 interviews. You have something like a week to do the coding test and then submit your code for review. I had the other tests and interviews on a single day. Although this sounds pretty horrendous, it actually wasn’t too bad. Tests were pretty quick and the interviewers were astoundingly friendly. I finished by something like 3pm in the afternoon, including a lunch break, and surprisingly didn’t feel too bad or stressed afterwards.

Induction
As a fairly impressive start, I had 2 days of induction in Melbourne (I live in Sydney). ThoughtWorks arranged drivers, hotel and flight so it was all very smooth. This was lucky as I was pretty jet lagged and confused – I’d just flown back from an overseas holiday not long before. Induction was largely getting a company provided laptop, meeting people and getting an introduction to various internal systems and procedures. As an aside, I’ve heard that there is now an “immersion” process where you get sent to India for a week or two for induction but can’t comment on that.

A few weeks on the beach
When you’re not assigned to a client project, you are “on the beach”. This means you go into your local office with your laptop. It’s really great – there’s no particular tasks assigned to you, but the opportunity is there to get involved in a lot of interesting stuff. To give you some examples, here’s some of the stuff I’ve had the chance to do:

  • Write an open source plug-in for Mephisto for ThoughtWorks Studios
  • Be involved in scoping out and estimating for a RFI from a new client
  • Pair with another developer to do code reviews of potential new recruits
  • Help out briefly with a fun project to develop a driver for a USB build light for continuous integration servers (red for broken build, green for good build, etc)
  • Help out on client projects – I was asked to whip up a little proof of concept for JRuby and Java integration and learnt a bit getting this set up
  • Do a little bit of Google Maps integration
  • Meet colleagues and learn more about procedures etc
  • Get invited to lunch with the managing director – this is something that happens for all new hires and I think it’s really great
  • Almost go out on a pre-sales call (I’ve got to go back to Melbourne and will miss this unfortunately)
  • Catch up on tech reading such as blogs, books etc
  • Go to a swanky talk given by Martin Fowler and Kristan Vingrys
  • Eat lots of free lunches (usually twice a week) and attend various talks at the office given by other consultants
  • Drink lots of free coffee (ThoughtWorks has a coffee tab with a local cafe)

First project
Much fun as it is on the beach, after a few weeks, I was itching to join the big boys and go on a project. Getting assigned to a project is the purview of your professional services manager, and can be pretty changeable. The saying is that “you don’t really know what project you’re on till you walk in the door of the client site” and I’ve even heard “you don’t really know what project you’re on till you’re on the plane home”. There’s a grain of truth in these – it can look like you are going to go on a project and then it doesn’t come through, or some other project becomes more important or whatever. I almost went on several different projects before finally ending up on quite a cool Ruby / Rails project with a startup in Melbourne. So, I got to join the jet set and have been flying down to Melbourne during the week, and back for the weekends. This is a bit tiring, but ThoughtWorks does its best to make things comfortable. I’m staying in a really nice corporate apartment in Melbourne, flights are arranged and paid for and drivers are scheduled for pickup and drop off to the airport. There’s also a generous per diem allowance for food. The project is really cool, and I’m enjoying it, but can’t say more as it is under a NDA.

Back on the beach.. but only for two days!
My first project was two weeks, so after that I returned to the beach. Today is my second day on the beach. However, it turns out that the client was very happy with our first two weeks work and they’ve invited us back again until Easter. This means I need to fly back to Melbourne tomorrow. This won’t continue indefinitely though – when I was discussing the project with my professional services manager, we agreed that I would not need to stay on a Melbourne project for more than 6 weeks. And clearly this is in ThoughtWorks interest as well – it costs a lot more to fly somebody in from Sydney every week and provide accommodation etc. I’m going to be transitioning off the project by Easter and a Melbourne based consultant is going to take over from me if the project continues further. It’ll be good to be on a Sydney based project again but I feel it would be unfair not to say that ThoughtWorks has done a really good job in making working in another city as convenient and pleasant as possible.

Various benefits
ThoughtWorks is pretty generous in the expenses department. They cover your mobile phone, home internet, per diem when away, give an allowance for training courses and books, etc. There’s also lots of free lunches, food, coffee and catered events.

Transparency and knowing what’s going on
I’ve been quite impressed to get a monthly update email that talks about ThoughtWorks plans, goals and financials, headcount etc in significant detail. There’s also various update meeting where you get to hear how projects are going and what’s happening with various clients. Personally I’m really glad to see this type of thing, as at previous jobs, this has been privileged information, and most of the time, I have not really had any idea how well the company is doing financially as a whole, or what the future plans and directions are.

Variety and Unpredictability
These are really two faces of the same coin, and depending on your character and experience, I think you might either love or hate this. You really don’t know what project you’re going to be working on, what your role will be, what industry the client is in, what type of development they need or for what platform or in what language. In fact, you don’t even know what city you’re going to be in during a given week. I’m enjoying this at this point as my last job was always in the same office, with the same technologies etc. However, I can see it may be trying in the long term, and it does make it difficult to do the shopping or organise things with friends during the week. On the other hand, I have heard that most of the work in Sydney is for big companies like banks and telcos in the city CBD within walking distance to the office, and the majority in Java. So perhaps my short experience so far is not the norm.

In conclusion…
So far, I can honestly say that it’s been really great working at ThoughtWorks. I’ve had a chance to do some of the stuff I’ve wanted to do for ages like work on a bit of open source and do some commercial Ruby on Rails work. My colleagues have been friendly and welcoming, and I’ve been wowed by the level of care that ThoughtWorks takes of its employees.

Contact / Feedback Form Plugin for Mephisto

Introduction
If you use Mephisto, a content management / blogging system written in Rails, you may well be interested in using this new plug-in. It provides a form that lets visitors to your site leave their contact details and send you messages or feedback via email.

UPDATE
Please check out information about using the contact form plugin with Mephisto Drax 0.8.

License
This plug-in was developed for the new ThoughtWorks Studios site. As I wrote it at and for work, it is copyright ThoughtWorks, 2007. However, ThoughtWorks, being generous souls, is happy for me to open source it under the Apache 2.0 licence, which pretty much means you have free reign to use it as you want.

Requirements

  • Mephisto Edge (the latest stable 0.7.3 release does not have support for Mephisto plugins)
  • Rails Edge (required by Mephisto edge)
  • ActionMailer (comes with Rails) correctly configured with SMTP server etc, so that emails can be delivered. See “Configuration” section here for more details.

Installation

ruby script/plugin install http://github.com/jcrisp/mephisto_contact_form/tree/master

or in your vendor/plugins directory for Mephisto:

git clone http://github.com/jcrisp/mephisto_contact_form/tree/master mephisto_contact_form

Make sure you restart your web server at this point so that the plugin is loaded.

Setup
1. Create a new template called ‘contact_us.liquid’ though the admin web interface (under the ‘Design’ tab).
Paste in the following code:

<H1>Contact Us</H1>
{% contactform %}
<p>{{ form.name }}<label for="author"><small>Your name</small></label></p>
<p>{{ form.email }}<label for="email"><small>Email address</small></label></p>
<p>{{ form.phone}}<label for="phone"><small>Phone number (optional)</small></label></p>
<p>{{ form.subject}}<label for="subject"><small>Subject</small></label></p>
<p>{{ form.body }}</p>
<p>{{ form.submit }}</p>
{% endcontactform %}

Feel free to modify labels, layout etc.

2. Edit

{MEPHISTO_ROOT}/vendor/plugins/mephisto_contact_form/lib/contact_notifier.rb

and put in the email address you want contact form submissions to go to.

3. Link to “/contact_form” from your site.

Any issues / questions / suggestions?
Best to post comments on this blog.

Technical Info
The contact form plugin is actually a combination of a rails plugin, a liquid block plugin and a Mephisto plugin. See this post about developing Mephisto plugins for more information.

The Secrets of Consulting by Gerald Weinberg

The Secrets of Consulting by Gerald Weinberg is one of the most entertaining (largely?) non-fiction books that I have read – a heady mix of How to Win Friends and Influence People, Fear and Loathing in Las Vegas (just look at the illustrations!) , and the 10 Commandments. The book provides general advice, case studies/stories and then derives general “rules” and recommendations from these.

Personally, I found the chapter on the pricing of consulting to be particularly interesting. Thinking about setting a price previously, I would have suggested it should be enough to cover costs and make a bit of a profit. Weinberg points out that price is more than this – it is a big factor in the relationship and the level of respect for the consultant.

The Weinberg’s consulting “rules” are quite numerous – my personal favourites are:

  • “If you can’t fix it, feature it.”
  • “It may look like a crisis, but it’s only the end of an illusion.”
  • “You’ll never accomplish anything if you care who gets the credit.”
  • “If something’s faked, it must need fixing.”
  • “The name of the thing [label] is not the thing.”
  • “It tastes better when you add your own egg.”
  • “You don’t get nothin‘ for nothin‘. Moving in one direction incurs a cost in the other.”
  • “Whatever the client is doing, advice something else.”
  • “What you don’t know may not hurt you, but what you don’t remember always does.”
  • “Clients always know how to solve their problems and always tell the solution in the first five minutes.”
  • “When change is inevitable, we struggle most to keep what we value most.”
  • “The biggest and longest lasting changes usually originate in attempts to preserve the very thing ultimately changes most.”
  • “Effective problem-solvers may have many problems, but rarely have a single, dominant problem.”
  • “Make sure they pay you enough so they’ll do what you say. The most important act in consulting is setting the right fee.”
  • “The more they pay you, the more they love you. The less they pay you, the less they respect you.”
  • “Spend at least one day a week getting exposure.” and “Spend at least 1/4 of your time doing nothing.” and make sure your fee covers this.
  • “Set a price so you won’t regret it either way.”
  • “If they don’t like your work, don’t take their money.”
  • “Cucumbers get more pickled than brine gets cucumbered.”
  • “Give away your best ideas.”
  • “Look for what you like in the present situation and comment on it.”
  • “Study for understanding, not for criticism.”
  • “Never promise more than 10% improvement.. if you happen to achieve more than 10% improvement, make sure it isn’t noticed.”
  • “Consultants tend to be the most effective on the third problem you give them.”
  • “The child who receives a hammer for Christmas will discover that everything needs pounding.”

_vimrc for Ruby

In the past I’ve used gvim for Ruby coding. It’s been pretty good, especially with the new tabbed editing and omni complete (bit like intellisense in Visual Studio) introduced in vim 7. However, when I downloaded vim at work, I was missing the secret sauce – a good vimrc. Here’s my usual vimrc for ruby:

set nocompatible
behave xtermset
selectmode=mouse
set nu
set tabstop=2
set shiftwidth=2
set softtabstop=2
set ai
set columns=100
set lines=70
set guifont=Courier:h10
set expandtab
set smarttab
let g:rubycomplete_rails = 1

Among other things, it makes the default window size bigger, uses a prettier font, sets up auto indenting ruby style, and turns on omni-complete.

By the way, on windows, assuming a default install, vimrc is to be found here:

C:\\Program Files\\Vim\\_vimrc

JRuby Setup

Recently got a JRuby/Rails system with Java integration up and running. Unfortunately, it took quite a few hours, as most of the docs and code you find through Google are out of date.

If you use JRuby 0.9.2 from Codehaus, you will get an error similar to this when you try to access a rails application:

[2007-02-26 17:54:59] INFO WEBrick::HTTPServer start: pid=22540508 port=3000
<ArgumentError: Anonymous modules have no name to be referenced by>
[“c:/jruby-0.9.2/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/
active_support/dependencies.rb:402:in `to_constant_name’…

If you’re stuck in this rut, fear not! Nick Sieger has written very helpful instructions which outline how to get and set up the latest development snapshot. Please note that in addition to the instructions, you need to set your JRUBY_HOME environment variable. Under Windows, do something like this:

set JRUBY_HOME=c:\jruby

If you’d prefer not to use the snapshot, you can get the source code through subversion from:

http://svn.codehaus.org/jruby/trunk/jruby

but at the time of this post, you need to run svn checkout or update with “–ignore-externals” to avoid the following error:

Error: URL ‘svn://rubyforge.org/var/svn/bfts/bfts/trunk’ doesn’t exist

Many thanks to Nick Sieger and the JRuby user mailing list for their help.

Using floating point variables to represent money => not a good idea!

I was reading through some code the other day and was surprised to find that it was using doubles to represent dollar amounts. Reason for the alarm bells is that doubles and floats cannot accurately represent many decimal fractions (eg, 0.1), since doubles and floats internally work with powers of 2 rather than powers of 10. These inaccuracies are likely to lead to significant errors, especially when performing arithmetic (eg, adding up a table of dollar amounts). See this IBM article for a more in depth explanation and examples. The solution is to use types that work with powers of ten internally. In C#, you can use ‘decimal’ and in Java or Ruby, ‘BigDecimal‘, to avoid these problems.

AntiPattern: BusinessObjects in the driving seat

When you have a rich domain model with a business object centric design, and a Windows forms GUI, it can be very tempting to start putting significant process logic in the business objects. After continuing along this path a little further, you may realise that the process needs some sort of user input, and you use events or some sort of notifier pattern to gain user input required by the process, while still maintaining layering in terms of referencing. Then additionally you may need to access some sort of external service.

Here is an example:

class Order : BusinessObject
{
  public void SendOrder(INotifier notifier)
  {
    if (ReadyForDelivery ||
        notifier.Confirm("Are you sure you want to send order lines? They are not ready for delivery."))
    {
      OrderLine[] orders = GetLinesToSend();
      foreach(OrderLine line in Lines)
      {
        SendLine(line); // send line using a web service?
      }

      Notify("Lines sent successfully.");
    }
  }
}

interface INotifier
{
  void Notify(string msg);
  bool Confirm(string msg);
  OrderLine[] GetLinesToSend();
}

I would like to suggest that this is an anti-pattern and a trap. Although there is no direct reference from the Business Layer to the GUI layer (INotifier is implemented in GUI and passed down), the Business Layer now requires the ability to stay instantiated, pause while waiting for responses from the notifier, and then continue execution. This will work for rich client applications, but not in a stateless web environment. The ideal of being able to swap in/out the GUI layers on top of the Business layer is now compromised.

Instead, it would be possible to drive form the GUI layer, and call a service to send the Order Lines. In pseudo code below:

void SendMenu_Click(...)
{
  if (Order.ReadyForDelivery ||
      MessageBox.Show(...) == DialogResult.Yes)
  {
    using (ChooseLineForm chooseLineForm = new ChooseLineForm(Order))
    {
      chooseLineForm.ShowDialog()
    }
    SendingSevice.SendLines(chooseLineForm.selectedLines);
    ...
  }
}

If the logic in the GUI layer became much more complex, it may be a good idea to pull it out into its own class (eg, LineSender). This class would be a type of GUI level controller, responsible for orchestrating the send process.

Using this approach, there are a number of benefits:

  • BusinessObjects have no reliance on GUI implementation, so can be used for Rich Client and Web Client indiscriminately.
  • Web developers are free to implement the user input process in stateless way more appropriate to their platform.
  • Functionality for sending Order lines (some sort of integration with a web service?) is pulled out into a service class which can be reused elsewhere (potentially sending other types of objects?) and unclutters the Order business object and removes its dependency on an external service.
  • Code is simpler and easier to follow.

In Defense of Simplicity AND Complexity

Simplicity and complexity seem to have become hot topics for some of my favourite technical bloggers of late. These fine people have taken the view that things should be either simple or complex. Right, seems logical, these are opposites. However, I would like to suggest that in a well designed appliance which addresses a complex process, it should have both a simple AND a complex interface.

A couple of years ago, I bought a LG “Fuzzy Logic” washing machine. It has lots of buttons and settings on the front and one big button that says something like “Start”. 97% of the time, I throw in my washing, some detergent and press the big start button, and the washing machine works out all the settings, displays them and then starts. In the 3% of the time when I want to do something different (eg, just a rinse), I use the more complex part of the interface to change the ‘cycle’ settings.

Recently, I bought an IXUS 65. It’s a lovely digital camera, and it provides both a complex and a simple interface. As soon as I put in the battery, I was able to take pretty nice pictures by just clicking the big button on the top. No problem, I was very happy. Over the next few days, I glanced through the manual and fiddled with more complex settings for ISO, colour etc. However, in 95% of shots, I simply want to click the one big button that takes a nice auto-everything picture. It’s only occasionally that I want to change the settings to achieve a particular effect or to override a mistake in the auto settings.

To conclude, I think that my talented fellow bloggers are all right. People like complex interfaces and simple interfaces, just at different times, for different tasks. The best gadgets and appliances offer both.

Page 13 of 14

Powered by WordPress & Theme by Anders Norén