Always Get Better

Posts Tagged ‘efficiency’

VISTA: How to fix SQL Server Express Error – CREATE DATABASE permission denied in database ‘master’

Thursday, November 5th, 2009

If you’re using SQL Server Management Studio Express under Windows Vista and see either of these errors:

CREATE DATABASE permission denied in database 'master'

or

The database [Name] is not accessible. (Microsoft.SqlServer.Express.ObjectExplorer)

Here’s the fix:

  1. Close SQL Server Management Studio Express
  2. Open your start menu and locate that program.
  3. Right-click on the Management Studio and choose ‘Run as Administrator’
  4. Fixed!

I swear the simplest solutions can be the hardest to find – hopefully this saves someone (or my forgetful self!) some aggravation.

Dual Screen Wallpaper

Sunday, October 11th, 2009

If you’re fortunate enough to be using a dual-screen setup (I suggest that everyone should be using dual screens), check out these 45 wallpapers from the constantly amazing Six Revisions.
Birth of the Moon

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)

Six Flavours of Windows 7

Thursday, February 5th, 2009

I’m starting to get enthused about Windows 7. Not so excited that I would stand in a line overnight to be the first to own a copy, mind, but I may actually be putting a PC back in the running for my next computer. One thing I am not so thrilled about is the six different versions to choose between.

Come on, guys. Mac has it right – one version of the OS for desktop use, another for server configurations. Do you really need a separate version for:

  1. Netbooks (essentially, a stripped-down driver-free version that can fit on solid state flash drives)
  2. Home “basic” Version (for emerging markets where piracy is a problem – do people seriously pay for these crippled versions when they can steal the full thing for nothing?)
  3. Home Premium (by all accounts nothing is “premium” about this version, it’s just the standard consumer-level version)
  4. Professional (meaning what, exactly?)
  5. Enterprise (for offices – the difference between this any professional has to do with site licensing)
  6. Ultimate (includes all the above)

I charge that the “Ultimate” version should be the “Only” version. Microsoft should quit playing games and obfuscating its software licenses – just sell the product; don’t make customers research all of the possible variations on features and functionality.

Get Your Boss to Do What You Want

Wednesday, January 14th, 2009

Communication Overtones asks “What do you do when you are sure you are right about something but your boss won’t listen to you?” Any manager worth his salt knows to surround himself with talent that will compliment his own skills – he will draw from the experts around him to formulate his plans and direction.

Experienced managers also learn to trust their own instinct even when the advice from the experts is contrary. So what do you do when your manager has decided to go with their own judgement even though you are sure they’re wrong. After all, you’re being paid for exactly what you are bringing to the table, so where is the sense in overriding your recommendation?

There are a few ways to proceed:

  1. Insist on your course of action and hold your ground until your manager is forced to reconsider.
  2. Back down – the manager is in effect your client, and you can lead a horse to water but not necessarily get them to drink.
  3. Plant the seed.

Plant the Seed
By “planting the seed”, I mean be subtle about your course of action. Let your manager know what you are thinking and leave it at that. It will get at them subconsciously until they come at you weeks later with a great new idea that sounds suspiciously like the one you had brought forward.

Be Patient
When you plant seeds, you need to be aware that it takes time for your point of view to enter your boss’ mindset. Depending on the concept and its complexity, it could take months for this passive approach to take effect. You could say this technique only works for non-critical ideas, but that isn’t necessarily true – it only works if you have enough patience to let your course of action sit.

Let It Go
Because this technique is a passive method for subordinates to get what they want out of higher-ups, you need to be prepared to let your idea get overridden. If your manager truly sees fit not to invest in your idea and you can’t make them come around to your point of view then your two options are either to put your ego aside and accept it so you can move on, or find another manager/company who want to run with it.

Optimize SQL Queries by Using Aliases

Monday, June 30th, 2008

When joining tables in SQL, we often use aliases to shorten table names. Consider this query joining order lines (details) with orders inside the database:

[source:sql]
SELECT O.OrderID, FirstName, LastName, ItemName, Quantity
FROM Orders O
INNER JOIN OrderDetails OD
ON O.OrderID = OD.OrderID;
[/source]

The above may work, but behind the scenes the database’s query analyzer has to associate each of the selected columns with their respective tables. Although this is a fast process, it can be skipped entirely by simply fleshing out the query like this:

[source:sql]
SELECT O.OrderID, O.FirstName, O.LastName, OD.ItemName, OD.Quantity
FROM Orders O
INNER JOIN OrderDetails OD
ON O.OrderID = OD.OrderID;
[/source]