Revealing Module Pattern in CoffeeScript

My friend Colin introduced me to CoffeeScript last week through Mindscape’s Web Workbench.  As I started to learn CoffeeScript, the first thing I wanted to do is to hand translate some of my JavaScript files to see how the CoffeeScript compiler would convert them to JavaScript so I would end up with the functionally same code in the end.

I use the Revealing Module Pattern quite a bit. I found an implementation written by Burk Holland and in the comments of Burk’s post Steve Howell and Elijah Manor provided additional implementations.  Although, Burk’s example came close to what I was looking for in simplicity and cleanliness, I felt that there could be some improvements:

  • It defined the module in the global namespace.  I always create my modules in a namespace I define for the my project to further protect against naming collisions.
  • I didn’t like how the module ended with the definition of a variable to contain the  public functions.

I put forward the following:

I use the existential operator to use my project’s namespace if already defined or to create a new one.  Although, the || operator does produce nicer JavaScript output.  I’m sitting on the fence to which way to go.

I create an object as the last expression in the function that will be returned.  This skips the creation of the temporary variable that is never referenced. 

I’m pretty happy with the JavaScript that is produced.  It very closely matches how I implement the Revealing Module pattern in JavaScript, and it upholds the simplicity and cleanliness of CoffeeScript.

Visual Studio 2010 Data Generation Distributions

I hate it when you look up an enum definition and the only help you get is the list of the values.  I already know that!  What does each one do?  This is the case in Data Generation Plan of Visual Studio 2010 with the Column’s Distribution property.

HorizontalRuleWide

In this post, I will look at the possible values of the Column’s Distribution property.  The valid values are Uniform, Normal, NormalInverse, Exponential, ExponentialInverse, but you already know that.

To create these graphs, I used a data-generation plan that generated 10000 values from 0 to 100 for each distribution type.  From this I was able to create idealised graphs that show how the data is shaped.  These graphs (except for the uniform distribution) do not include the noise introduced by the data generator to randomise the generation a bit.

Uniform

The uniform distribution is pretty obvious, but you might think that you get the same value each time.  This is not the case and like all the distributions, the uniform distribution is randomised.

uniform

The mean of the uniform distribution is equal to the total samples divided by the number of samples.  From first glance, the randomised output looks like varies by about 20%.

Normal

The normal distribution is what you would expect:

normal

Normal Inverse

This one has me stumped.  I originally though it was an inverted normal distribution, but it did not match the data.  The best approximation I could make was with a quadratic equation:

normalinverted

This does not fit the sample data well, but its close.  At least you can get an idea of what it will look like.

Exponential

The exponential distribution is actually the reverse of what I originally thought; it is actually an exponential decay.

exponential

Exponential Inverse

This distribution is a mirror of the Exponential distribution.

exponentialinverted

A Flaw in the PRNG

There appears to be a flaw with the pseudo random number generator.  The first and last value of the range will only appear at half the rate that it should.  This should not affect data generation in most cases, but keep it mind if you are analysing test results.  It may lead you on a wild goose chase.

Well, there you have it, how the distribution property on the column will shape the data generation.  I hope this saves you some time.

Entity Framework Many-to-Many Relationships

I was 0wn3d.  Entity Framework was kicking my butt around and I couldn’t figure out why it couldn’t recognise a many-to-many relationship in my database using an association table.  Just a error 3002 over and over…

HorizontalRuleWide

I created my database first, and was generating my model from the database.  I build up the database in Management Studio and a Visual Studio Database project, and I was all set to create my entity model from the database.  This is what I was shown as the model:

broken model

That’s not right!  I wanted a many-to-many relationship between Trip and Location!

After digging around on the internet, the entity model, and my database schema, I discovered a small error in the database that I missed:

baddefinition

I had only two columns that had foreign keys to the related tables (storing addition data in the association table will cause other problems), but I had allowed one association column to allow NULLs.

For the entity framework to recognise an association table, the association columns cannot allow NULL values.

After regenerating the model, things looked a lot better…

partialfix 

…but it is not many-to-many relationship.  I attempted to change the relationship to many-to-many, but that caused an error:

Error 3002: Problem in mapping fragments starting at line 139:Potential runtime violation of table Trips_Locations's keys (Trips_Locations.TripId): Columns (Trips_Locations.TripId) are mapped to EntitySet Trips_Locations's properties (Trips_Locations.Trips.Id) on the conceptual side but they do not form the EntitySet's key properties (Trips_Locations.Locations.Id, Trips_Locations.Trips.Id).

This error is basically telling me that my model does not match my database.  After digging some more, I realised that there was another error in the database schema: I constructed the primary key on only one association column!

In an association table, the primary key must consist of both association columns.

After correcting this error, I finally arrived at what I was looking for:

correct