Always Get Better

Archive for the ‘Design Patterns’ Category

Rely on Continuous Integration

Friday, April 22nd, 2011

If your testing process involves copying files over to a development environment and hoping you have the most up-to-date version of everything, stop now. There is a better way, and it is continuous integration.

CI works by constantly and automatically testing and publishing everyone’s code into a staging environment. Any missing files or major problems are caught immediately so the team doesn’t run into major conflicts at the end of the development cycle. Although continuous integration does not eliminate code bugs, it improves the software quality by allowing developers to identify and fix problems immediately, resulting in a final product with much fewer defects.

Although there are a lot of different parts to a successful CI process, it is possible to start up slowly and add to the regime as your team becomes more comfortable with their improved success rates.

1. Establish a centralized source code repository
A solid source code manager (SCM) is the backbone of a professional development setup. There are so many tools available for every possible organization, both paid and free. I like git, but Subversion is used in a lot of environments. The key thing to look for is something that can track and rollback code changes over time and is shared between everyone working on the project.

2. Automate the build
After a good SCM has become part of development procedure, automate the build What this means, is every time someone commits to the SCM, have a tool like buildbot pull the fills and deploy them to a testing environment. From this stage on disallow anyone from making changes directly to the testing environment.

3. Automate Testing
Next add unit testing. When the build agent runs the unit tests, it should prevent the source code from going into the testing environment if any error conditions exist in the code. This will let the team make more daring changes to the source code without having to worry about time-consuming reversion testing.

4. Automate Deployment
At this point we’re already have an automated way of deploying our code to a testing area – take it a step further and automate your deployment process. This will let you get your work into the hands of your customers faster and involve them in the planning process earlier – all resulting in an improved product and higher likelihood of staying on-time and on-budget.

Displaying Production-Only Markup in Rails

Thursday, March 3rd, 2011

If you are running something like Google Analytics on your website, you probably don’t want its associated JavaScript code to appear in your web browser while you’re developing (it would skew your statistics). In Rails, it is incredibly simple to block off a segment of markup for specific environments by using the Rails.env variable.

If you are using Passenger, your rails environment is set to ‘production’ by default, making it very easy to do something like this:

Implementing Lazy Load Using a Proxy Class

Saturday, February 28th, 2009

Lazy load is a design pattern wherein an object is not instantiated until the last possible minute. This is very handy when working with lists of items whose contents are expensive to retrieve from the data store.

There are typically three ways of implementing lazy load:
1. Lazy Initialization – The object is set to null and checked/loaded when data is needed
2. Proxy – A proxy class for the object is created using the same interface as the original class; whenever a property is called, the proxy creates the object and returns the correct data
3. Ghost – The class loads only a partial set of its information until more is needed

Example Situation

In my example situation, we handling a catalogue of artists owned by a fictional record label. For each artist, we will store a name, musical genre, web site address, and number of albums.

UML for Artist Class

UML for Artist Class

Let’s pretend we have thousands of artists on our roster, and we need to print a catalogue containing all of their information. Rather than loading all of that data into memory right away and having to wait until that process is done before we can begin printing, it makes more sense to get a list of how many artists will be printed (so our software knows how many pages to print) but to only load the actual information when we are ready to print it.

The solution is to create an ArtistProxy class. ArtistProxy has an _artist variable who is set to null when it is initialized. Whenever we try to access the artist’s name, web site, etc from ArtistProxy, the class creates an Artist (only if not already done) and returns the property from _artist.

Our print function is never aware of ArtistProxy – as far as it is concerned it only ever deals with Artist. We accomplish this by creating an interface – IArtist – which acts as a contract for both ArtistProxy and Artist. If we add more properties to Artist later on, IArtist will keep us honest by forcing us to also update ArtistProxy.

Artist, ArtistProxy, and IArtist UML Diagram

Artist, ArtistProxy, and IArtist UML Diagram

Now that we understand how our classes relate to each other, it’s time to use them in code:

[source:csharp]
// Our printArtists() function looks something like this.
// Notice how we are unaware whether the artist is
// an actual object, or a whether it is a proxy.
public function printArtists( IArtist [] artistList )
{
foreach ( IArtist artist in artistList )
{
printOneArtist( artist );
}
}

// Implementation of the IArtist interface
public interface IArtist
{
string Name { get; set; }
string Genre { get; set; }
string Website { get; set; }
int getNumberOfAlbums();
}

// Implementation of Artist class
public class Artist : IArtist
{
private int _id;
private string _name;
private string _genre;
private string _website;

public Artist( id )
{
_id = id;
}

public string Name
{
get { return _name; }
set { _name = value; }
}

public string Genre
{
get { return _genre; }
set { _genre = value; }
}

public string Website
{
get { return _website; }
set { _website = value; }
}

public int getNumberOfAlbums()
{
return fictionalDataConnection->getNumberOfAlbums( _id );
}
}

// Implementation of ArtistProxy class
public class ArtistProxy : IArtist
{
private Artist _artist;
private int _id;

public ArtistProxy( id )
{
_artist = null;
_id = id;
}

public string Name
{
get
{
if ( null == _artist ) _artist = new Artist( _id );
return _artist.Name;
}
set
{
if ( null == _artist ) _artist = new Artist( _id );
_artist.Name = value;
}
}

public string Genre
{
get
{
if ( null == _artist ) _artist = new Artist( _id );
return _artist.Genre;
}
set
{
if ( null == _artist ) _artist = new Artist( _id );
_artist.Genre = value;
}
}

public string Website
{
get
{
if ( null == _artist ) _artist = new Artist( _id );
return _artist.Website;
}
set
{
if ( null == _artist ) _artist = new Artist( _id );
_artist.Website = value;
}
}

public int getNumberOfAlbums()
{
if ( null == _artist ) _artist = new Artist( _id );
return _artist.getNumberOfAlbums();
}
}
[/source]

Of course for the sake of convenience a few things are missing from my example:
1. An actual data source
2. Delegates (presumably one would include a ‘LoadArtist’ delegate so the proxy will be able to pass the actual loading of its artist to the data layer)