Category Archives: Uncategorized

Development Methodology “Renovation”

I was watching Holmes on Homes today and got to thinking about parallels (or lack of them) between renovating and building a home vs developing a software application. Some of what I was thinking ties in with the “software development as craftsmanship” discussion that pops up every now and then, but mainly this is just a brain dump.

First off, let’s set this up. What I’m thinking is that a “software application” maps directly to a “home renovation project” or just actually building a new home. Here are some of the obvious parallels:

  • In a home building project you have a general contractor who oversees the entire operation, hiring other contractors or bringing in employees as needed. Software projects will generally have some sort of equivalent “lead/architect” role.
  • Both projects would have clients that most likely have no idea what’s going on. They see the end result and have no way of knowing whether that result will last 2 months or 2o years. Generally, as long as the finished product looks pretty and seems to do what’s required the developers/renovators will get paid.
  • Size matters in both professions. Anybody can hack their way through installing new countertops or building a simple CRUD web app, but the bigger and more complex the problem gets, the more professionalism and care is required. I’ll take a calculated leap and say that many of the problem projects in both professions happen because somebody who can “install a new faucet” gets it in their heads that they can also “renovate the entire basement” using the same amount of knowledge and preparation.

I’m sure there’s a few more parallels, but you get the idea. Now let’s focus on some differences…

  • A general contractor doing a home renovation will bring in “specialists” to work on individual pieces of the project, whereas many software projects are made up of “generalists”. As programmers, we are often tasked with developing business logic, writing html, security, usability, system administration, deployment, testing, etc. Just look at the range of what’s required in the average job posting for a software developer. This is obviously not true in all cases, but in general this is what I think is happening out there.
  • The home building industry has these crazy things called “inspections” that are supposed to ensure the builders don’t pull some crazy shit that could endanger the client. Imagine code inspections (by an honest inspector we hope) and how they could protect a client… An inspector could obviously not be expected to know if the application is running a calculation correctly, but that is the one thing that a client can confirm. An inspector could, however, check for signs of incompetence such as SQL injection vulnerabilities, obvious spaghetti code, code duplication, lack of tests, poorly designed databases, mangled HTML, obvious usability disasters and so on. Once notified of the issues, the client would be able to take action as they see fit.

My thinking is that, at least in my own experience, the best project I’ve worked on (in terms of happiness of the client and its likelihood of being maintainable for years to come) was the project that was most similar to a successful home reno project. We had a UI specialist, a bunch of developers focused mainly on the business logic, and testers. Other “specialists” were brought in as needed for database tuning and system admin type work. An “inspector” even came to the project at one stage. This inspector was Eric Evans, who showed up and observed the project for a week and delivered a report on what was good and what could be improved. This visit seemed to be largely positive (I showed up a few months after he came so I can’t know for sure) in the sense that it gave the development team some reassurance and some focus on what could be improved. I’m not sure what the clients got out of it.

I’m wondering if a working on a new project with the goal of running it consciously more like a home reno project could improve things. For example:

  • Think of hiring for “concerns” instead of technologies (ie; security & usability instead of Java & Struts).
  • Have a defined “general contractor” role that is in place to hire and fit all the pieces together.
  • Allow the specialists to come and go. There’s no need to keep everyone around all the time. Call them in when there is a need.

Based on my own limited experience I think doing software development as a home renovation could really work, especially when it comes to focusing on roles and quality. Should any project be open to an SQL injection attack ever? Hell no, there’s just no excuse for that kind of incompetence. But so many are… Inspections and specialists might be one way to mitigate this.

In closing, I’d like to hear other people’s thoughts on this. If it’s already been said before (better hopefully), I’d like to read more about it.

Rails, Nested Forms and collection_select

I spent a bit of time on this tonight and thought I’d post about it.

I have a Property model that has a has_one relationship with an Address model. I want to be able to CRUD these two objects as one single entity, however. Luckily, nested forms were recently introduced into Rails 2.3.2 and they seemed like they’d simplify the whole process. Which they did once I put everything together. First, my models:

class Property < ActiveRecord::Base
  def initialize(attributes=nil)
    super
    #we override the initialize method and ensure we always have 
    #an address instance created. This makes sure we don't get any
    #nil reference errors in our forms
    self.build_address unless self.address
  end
 
  #accepts_nested_attributes_for enables the property's address object
  #to be updated at the same time as the property object
  accepts_nested_attributes_for :address, :allow_destroy => true
end
 
class Address < ActiveRecord::Base
  has_one :property
  # the Address model has a :city string column that will get populated from
  # a drop-down list in our view
end

This is the basic stuff that any tutorial on nested forms in Rails will tell you about, with the exception of creating a default instance of the address in the property constructor. This is most likely specific to the has_one case. Most of the examples I have seen deal only with has_many, where behind the scenes rails creates an empty list and we’ll never end up with nil reference errors in our view. This was the first issue I had to solve. Now let’s move on to our new.html.erb view:

<% form_for @property do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name, "Title" %>
    <%= f.text_field :name %>
  </p>
  <p>
    <% f.fields_for :address do |af| %>
      <p>
        <%= af.label :address_one, "Address One" %>
        <%= af.text_field :address_one %>
      </p>
      <p>
        <%= af.label :city, "City" %>
        <%= af.collection_select(:city, City.all, :name, :name) %>
      </p>
    <% end %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

The trick here is to make sure you qualify the collection_select for cities with the specific fields_for instance. If you don’t, the form will render and appear to be ok until you post to the create action or view the generated source:

<p>
<%= af.label :city, "City" %>
notice how I've removed the af. qualifier on collection select, and added in a
new parameter :address. This is how most tutorials show you how to use it
The problem with this is that the rendered html does not generate properly for
our nested form scenario
<%=collection_select(:address, :city, City.all, :name, :name) %>
</p>

Once you qualify the collection_select helper properly, you should get generated html that looks like this:

<label for="property_address_attributes_city">City</label>
        <select id="property_address_attributes_city" name="property[address_attributes][city]"><option value="Lethbridge">Lethbridge</option></select>

Now when you post the form, Rails nested form magic just works. My controller looks just as you’d expect (ie; no different than the non-nested form scenario)

  def create
    @property = Property.new(params[:property])
    respond_to do |format|
      if @property.save
        flash[:notice] = 'Property was successfully created.'
        format.html { redirect_to(@property) }
      else
        format.html { render :action => "new" }
      end
    end
  end

Hopefully I’ve explained this properly. Feel free to ask questions in the comments if I haven’t.

Extending clearance

I thought I’d try using clearance to handle authentication on the Rails app I’m developing. It seems pretty nice and basic, but I wanted to add a :name property on the user model (not for authentication, just for display purposes).

Once I’d created a migration to add the new column to the user model, I needed to add the field on the corresponding view so it could actually be input by a user. Since clearance is installed as a vendor gem/engine (still not sure what the difference is) the view files are located in the vendor directory. I figured updating these files wouldn’t be good, since they’d be overwritten if I ever had to update and unpack the gem.

It turns out all I had to do was copy the view files into my app under the views/user/ directory. I could then modify the files at will, and they would be used by Rails instead of the vendor files. One other gotcha was to make sure to add the attr_accessible :name declaration in my user.rb file (so Rails can do a mass assignment of the posted form items). My user.rb ended up looking like this:

class User &lt; ActiveRecord::Base
  include Clearance::User
  has_many :properties
  attr_accessible :name
end

Rails Functional Testing Gotcha

I just started writing my first real Rails app this weekend, and as I struggle along I thought I’d document where I’m tripping up.

My first order of business is to write functional tests on my controllers and views. For some reason I thought I’d give the tests (which inherit from ActionController::TestCase) descriptive class names like PropertiesDashboardTest instead of PropertiesControllerTest. I had tests running and passing before I renamed the test class, but then started getting this error:

RuntimeError: @controller is nil: make sure you set it in your test’s setup method.

It took me a while to realize that Rails auto-instantiates a controller instance based on the test name when you inherit from ActionController::TestCase. Since I don’t have a controller named PropertiesDashboard.rb, Rails can’t set one up in the test for me. Of course I’m doing this test-first, so a controller doesn’t even exist at this point. That also explains why my existing test started failing when I renamed it.

Forgetting Craftsmanship

Sergio Pereira¬†has just posted a video of a speech by Bob Martin about¬†Extreme Programming after ten years. It’s a fascinating presentation with a message that resonates with me in particular, since I’ve been a part of a software project for the past 3 1/2 years that has left craftsmanship behind while still keeping to the trappings of Agile development (SCRUM in particular). The result has been tragic (to the code base), and the more people who learn from this, the better. Bob may have just inspired me to try to tell the story. But for now… Just watch the video.

Javascript Syntax Highlighting

A few months ago I toyed with the idea of doing syntax highlighting with javascript. I’m sure there’s several published solutions out there that do the same thing, but I figured I’d give it a go myself, mainly to practice up on my meagre javascript skills. I set out with the following goals:

  1. Highlight the code in a completely unobtrusive manner.
  2. Use absolutely no inline markup.

Basically, I wanted to be able to paste in the following markup…

<code class="csharp">
	private void Page_Load(object sender, System.EventArgs e)
	{
		 LoadEntity();
	}

	private void LoadEntity()
	{
		 User user = new User();

		 /*
			  Get the id of the entity from the request
		 */
		 if( this.RequestedEntityID > 0 )
		 {
			   user.ID = this.RequestedEntityID;
			   BrokerFactory.Fill( user );
		 }

		 //Load the appropriate view
		 EntityControl cont =
			(EntityControl)LoadControl( "EditStaff.ascx" );

		 cont.BoundEntity = user;
		 this.phEntity.Controls.Add( cont );
	}
</code>

…and have the syntax highlighting just work. I currently have
some test code that
is partly working (although not in IE, there seems to be a problem with setting the text to be pre-formatted).
View the source to see what I’m up to. It should be easy to extend it to check for the type
of highlighting that’s required based on the class of the code tag.

In closing, my main reason for posting this is that I’d like to see this working,
but just don’t have the time or energy to finish it off in the near future. So if anyone knows of
an existing solution that works in the same way, or wants to continue on with this implementation, feel free to
let me know.

Rock Invoices with Blinksale

After just recently entering the ranks of self-employed nerds-for-hire, (more on that later) I was faced with the looming issue of billing. Now, when I was last seen firing out invoices (in an entirely different industry), I was utilizing the fantastically esoteric templates found within everybody’s favourite spreadsheet application; Lotus 1-2-3. Since I now know that spreadsheets are for accountants and other forms of masochistic freaks (sorry Kirk), I was at a loss trying to determine how to keep track of my invoices. Time ticks on by, as it is wont to do, and I found myself less than a week away from the end of the month. Colleagues admitted to using Quickbooks, but caught up in my rabid fear of anything that is “accounting”, I balked at that idea. Thankfully, just in the nick of time, I was saved by Blinksale. Go and read about it for yourself.

I think I’m saved.