Archive for December, 2005

Pandora.com

Just had to throw this one out there, if you haven’t tried pandora.com yet.  Pandora.com uses the Music Genome Project to suggest music that you probably like based on your current musical interests.  The flash app starts off by asking you for a favorite artist or song.  It then starts playing music selections back to you, music that has similar musical qualities as the suggestion that you gave them.  For any song thats playing you can ask pandora.com why it played that particular song, and it will tell you how it relates to your musical preferences.  You can also tell it more music that you like, so it can refine its suggestions back to you, the more you tell it, the better then suggestions can be.  One thing I’m not sure about it how they can play all this music back without violating copyright restrictions, I’ll have to look into that.  But in any case, check it out, its a pretty cool website.

[Update]: They have annual and quarterly subscriptions, as well as a free version with commercials.

Making a Trigger Fire On Column Change

I was working with some SQL Server triggers today at work, the triggers were used to track changes to a Price column on a table.  Whenever the Price column changed, we wanted to track that in a separate table so we could have a price history.  So I setup the trigger on the Price column and set it to fire on INSERTs and UPDATEs.  Here was the trigger at this point:

CREATE TRIGGER trg_SavePriceHistory ON myTable
FOR INSERT, UPDATE
AS
IF UPDATE(Price)
BEGIN
       DECLARE @newPrice decimal(18,2)
       DECLARE @itemId int
       SET @newPrice = (SELECT Price FROM Inserted)
       SET @itemId = (SELECT ItemID FROM Inserted)
       INSERT INTO PriceHistory (NewPrice, ItemID) VALUES (@newPrice, @itemId)
END

Its pretty straightforward, check to see if the Price column was updated and if it was then make a new entry in the PriceHistory table with the new price and the item’s id.  After a little while, I realized that every update statement that included the Price column was setting off the trigger.  It made sense, I guess I assumed that it would only fire when the value of the Price column actually changed, not if it simply got written with the same value.  For instance, if I ran the following INSERT statement:

INSERT INTO myTable (Price) VALUES (10);

Then the trigger would fire and the price would get logged.  Now suppose I update the record that I just inserted with the same value for price (assume the id = 1):

UPDATE myTable SET Price = 10 WHERE ItemID = 1;

Now the trigger will fire again, which is what I don’t want.  I only wanted the trigger to fire if the value had changed.  I was kind of scratching my head, being new to triggers and all, about how I could get that to work.  I started writing an email to an internal mailing list, when it hit me in one of those “Aha!” moments.  Inside each trigger are 2 special tables called “Inserted” and “Deleted”.  The Deleted table holds the values of the record before its state was changed by my UPDATE statement and the Inserted table holds the values of record after my UPDATE statement.  All I had to do was compare the Price columns from each table and see if they were different; if they were the same, then I could just exit my trigger.  So heres what the trigger looked like after I modified it (modifications in red):

CREATE TRIGGER trg_SavePriceHistory ON myTable
FOR INSERT, UPDATE
AS
IF UPDATE(Price)
BEGIN
     DECLARE @newPrice decimal(18,2)
     DECLARE @oldPrice decimal(18,2)
     DEClARE @itemId int
     SET @newPrice = (SELECT Price FROM Inserted)
     SET @oldPrice = (SELECT Price FROM Deleted)
     IF @newPrice != @oldPrice
     BEGIN

        SET @itemId = (SELECT ItemID FROM Inserted)
        INSERT INTO PriceHistory (NewPrice, ItemID) VALUES (@newPrice, @itemId)
     END
END

This is all probably very obvious to someone familiar with triggers, just thought I would help someone else out if they were looking for this.

[Edit: Please read the first comment for a description on updating more than one row]

Does Generated Code Lead to Poor Design?

I was using a code generator to create my data layer recently and I wanted to change how the data layer was working.  Each entity inherited from a base class that provided some plumbing-type functionality and the change that I wanted to make was in the base class.  The problem was that I didn’t want to change the base class, because that would mean changing a bunch of other classes that also used the base class, as it would have changed a public interface, so it would have been messy.  Even though the change that I wanted to make might be beneficial in the future, and it certainly wouldn’t be detrimental, I still avoided making the change out of laziness.  What I did instead was changed my generated class template file to modify the functionality in each generated class, thereby introducing a ton of duplicate code, but since it was generated, I didn’t really care.  I mean, who cares how bloated your code is, if its no harder to maintain than a single template file, right?  Right? 

This makes me wonder if code generation leads to poorly designed libraries.  My first foray into code generation was at my previous job where we made a code generator for our custom o/r mapping library.  Because we had built the o/r mapping functionality without thinking of code generation, our generated classes were very lean, because we used to hand code them and that gave us the incentive to put as much functionality as possible into the base class to save our hands from extra typing.  By the time we got the code generator up and running, our templates were pretty trivial, the generator didn’t have to do much, it was basically just writing out getters and setters for some properties.

If we hadn’t had the incentive to offload as much functionality as possible into the base class, our resulting classes would have been much more cumbersome and potentially harder to debug.  As it was, our base class was very well tested because it was put through the motions many, many times by each business object, it was nice to know that we had a solid base class to derive our simple business objects.

Loose vs. Lose

Okay, I need to point this out ’cause its been bothering me for awhile. There are 2 words in the English language: loose and lose.

(from dictionary.com)
loose:

  1. Not fastened, restrained, or contained: loose bricks.
  2. Not taut, fixed, or rigid: a loose anchor line; a loose chair leg.
  3. Free from confinement or imprisonment; unfettered: criminals loose in the neighborhood; dogs that are loose on the streets.
  4. Not tight-fitting or tightly fitted: loose shoes.
  5. Not bound, bundled, stapled, or gathered together: loose papers.
  6. Not compact or dense in arrangement or structure: loose gravel.
  7. Lacking a sense of restraint or responsibility; idle: loose talk.
  8. Not formal; relaxed: a loose atmosphere at the club.
  9. Lacking conventional moral restraint in sexual behavior.
  10. Not literal or exact: a loose translation.
  11. Characterized by a free movement of fluids in the body: a loose cough; loose bowels.

lose:

  1. To be unsuccessful in retaining possession of; mislay: He’s always losing his car keys.
    1. To be deprived of (something one has had): lost her art collection in the fire; lost her job.
    2. To be left alone or desolate because of the death of: lost his wife.
    3. To be unable to keep alive: a doctor who has lost very few patients.
  2. To be unable to keep control or allegiance of: lost his temper at the meeting; is losing supporters by changing his mind.
  3. To fail to win; fail in: lost the game; lost the court case.
  4. To fail to use or take advantage of: Don’t lose a chance to improve your position.
  5. To fail to hear, see, or understand: We lost the plane in the fog. I lost her when she started speaking about thermodynamics.
    1. To let (oneself) become unable to find the way.
    2. To remove (oneself), as from everyday reality into a fantasy world.
  6. To rid oneself of: lost five pounds.

Lets all try to use these words in the appropriate context :)

Not Being Evil Covers a Lot of Ground

Google’s slogan has always kind of bothered me.  “Don’t Be Evil” does not necessarily imply “Be Great” or even “Be Good”, its an exclusive thing to ‘not be evil’, they can still ‘be very bad’ without being evil.  I think their slogan should read “Always Be Great”.  Just an opinion.

Live.com Mail Beta

Don’t know if a bunch of people just got invited or what, but I received my Live.com Mail beta invitation today.  You have to agree to the EULA thingy and then you’re in!  It says that your account will get updated within two weeks, but mine was switched over immediately :)  Its pretty cool, its got calendar and contacts in addition to email, however the calendar and contacts are driven less by ajax and more by an iframe that does postbacks.  Still works really well, looks very similar to Outlook, but with a “webby” feel to it.  I hope they implement contact import/export, and not just to Outlook, I would like some csv/xml support.

One thing I wish they would do is lose the graphical ads, they’re annoying, take up too much horizontal space, and the ads are for Windows Live Mail Beta Feedback.  They should be less conspicuous.

Good job so far, however, I’m diggin it :)

Magenic Christmas Gift

So I just got a company wide email about the year-in-review at Magenic.  Of course, the big announcement was that the company Christmas gift this year is going to be an XBox 360.  People had been speculating if we would get them, given how hard they are to get, but Greg and Paul are gonna make it happen. They just made a lot of geeks happy!  I’m looking forward to getting one now, even though I’m not much of a gamer, I figure maybe now I can make some friends :-)

Looks like a duck, sounds like a duck……….

I read somewhere recently (and of course I can’t recall the link now, otherwise I would cite them) about how corporate “blogging” is becoming popular.  Now this is nothing new, the more current companies realize that blogging can be a great marketing tool and a way to get information out there very quickly.  Along with corporate blogs, there has also been a rise in media companies blogging the news.  My local paper, the Strib, has an entire section devoted to blogs.

For some reason, I’m not drawn to these corporate and media blogs the same way that I am with an individual person’s blog.  Just the fact that you know that anything being written there is filtered and approved by a marketing or PR department really turns me off.  To me, the whole point of a blog is to be able to get an unfiltered, transparent perspective and if thats not what I’m getting, then its not a blog.  Its just another story or opinion piece or marketing propaganda.

I also don’t like that much polish on the blogs that I read.  I want that rough-around-the-edges feel that makes posts seem more real that something that went through an editing process, it makes reading it more fun and authentic, and you know you’re getting the straight dope.  Scoble does this really well, he writes like he talks (I would guess), and you feel like you’re part of a conversation, not just being spoken to.

I’m all about news sites having rss feeds, don’t get me wrong, but theres a difference between having rss feeds and having “bloggers” publishing content.  When I’m reading a blog I have a certain connotation about the information I’m getting, which is vastly different than if I know I’m reading the news.  I hope that these companies and media organizations eventually get off the blogging phenomena and instead stick to publishing their regular content.