Archive for the ‘ Uncategorized ’ Category

Scoping rules in c#

What a dull subject. Exactly so because it’s supposed to be a done deal, I don’t hear many people arguing about scope rules anymore. That is why I was so suprised when the compiler complained about some perfectly valid code I had written.  At first I thought it was only a peculiarity of the way closures are implemented in c#, but as I dug deeper, the scoping rules of c# seems to be… well just annoyingly stupid. Lets start with a simple case:

OleDbCommand command = SQLServer.CreateCommand();

if (true)

{

OleDbCommand command = SQLServer.CreateCommand();

}

I don’t think many people would argue that is just bad programming practice, but look what happens if we just change it a little:

if (true)

{

OleDbCommand command = SQLServer.CreateCommand();

}

OleDbCommand command = SQLServer.CreateCommand();

This fails with the exact same error. Apparently the command of the if statement prevents the other command from being declared, but since you can use command anymore this is just plainly wrong.  Furthermore closures behave the same way:

VoidFunction t = delegate

{

OleDbCommand command = SQLServer.CreateCommand();

}

OleDbCommand command = SQLServer.CreateCommand();

Please note that the closure has just been declared. For more discussion of the examples see this thread on stackoverflow.  I followed up with a question of why the language guys at microsoft would have made such a bonehead mistake and there wasn’t any good reason in my mind.  I’m still puzzled why the compiler wouldn’t just issue a warning, but maybe warnings is one of those things they hated about C++ and decided it was just easier to swing the big No-hammer and be done with it.

Writing readable regular expressions

I was talking to Ole today about regular expressions, and one of the problems with regular expressions is that it, like SQL, compounds the actual problem into on string, thus making it very hard to read again. Luckily we now have ORM’s to take care of most of the SQL mess, but regular expressions hasn’t received the same kind of limelight. I immediately thought that one could use a similar technique to deal with the regular expression problem, just by expression it as a language (DSL) and running something like LINQ on it. And of course, several people has already been doing this. Nifty stuff!

My Media System 1.1.0 rc9 released

Yesterday MMS 1.1.0 rc9 was released. This has been a long time coming and hopefully will be the last release candidate before MMS hits 1.1.0 final. Changes are all over the map, not that many big new shiny things, but a lot of bugfixes and rewritings of the internals of MMS.

YayArt covering my home

Almost forgot to mention that Yayart did an interview with pictures from my home of the art I bought. You can read it here.

Little Brother

I finished reading the new book Little Brother by Cory Doctorow tonight. It’s a really exciting book, one that leaves you wanted to read just a few more chapters before you fall a sleep. The book is about a not-so-distant future in which San Francisco is attacked by terrorists. And how the whole system is turned upside down because of the event. Besides the main story which is really good, the book is sprinkled with reference to geeky stuff like Tor, Electronic Frontier Foundation and crypto. The book is also stuck full of good humor, like the following quote:

the only person who’d spoken to me was a Jehovah’s Witness and a Scientologist, both trying to convert me. It felt gross, like being hit on by a pervert.

I was a little sceptical about the book at first, since I’ve read some his earlier works and I wasn’t that big of a fan of Down and Out in a Magic Kingdom. So I downloaded the book for free (CC-licensed) and started reading it on my laptop. After the first chapters the book really caught me, and I ordered the book on amazon, thinking I would read the rest of it when it got here in physical form. Of course I couldn’t let go of the book and finished it of before the amazon order has arrived. That doesn’t mean I’ll cancel my order, the book is really good and Cory deserves the praises he has been getting for this book.

Remix: Making art and commerce thrive in the hybrid economy

Just finished reading Lessig’s latest book. I’ve been widely fascinated by his earlier work, but this one falls short compared to his other work on the subject of copyright in modern society. The first two parts is more or less just rehashing of old ideas, while the last part, about the future, is where the book really shines. More specifically it mentions five ways in which we can actually change copyright to make it better suited for the world we live in now.

He presents an interesting story about the problems they had in the southern states of America with racial discrimination. The problem was that if stores opened their doors to African Americans then they would be seen as pro-black and loose a lot of their original business. So in 1964 (!) a law was passed that made discrimination in public restaurants and other related establishments a felony. This changed the game completely. This reminds me of a law that was passed not that long ago in Denmark, that made smoking in the same kind of places a felony.

Google as a spell checker

If you also have been using googles “did you mean” function as a spell checker for words, you might find this funny:

  • Open google
  • Type in exadurate
  • Click I’m feeling lucky

:)

Closures

I had a real “aha moment” today while coding some javascript. Let me first start this post with a quote:

The venerable master Qc Na was walking with his student, Anton.  Hoping to
prompt the master into a discussion, Anton said "Master, I have heard that
objects are a very good thing - is this true?"  Qc Na looked pityingly at
his student and replied, "Foolish pupil - objects are merely a poor man's
closures."

  Chastised, Anton took his leave from his master and returned to his cell,
intent on studying closures.  He carefully read the entire "Lambda: The
Ultimate..." series of papers and its cousins, and implemented a small
Scheme interpreter with a closure-based object system.  He learned much, and
looked forward to informing his master of his progress.

  On his next walk with Qc Na, Anton attempted to impress his master by
saying "Master, I have diligently studied the matter, and now understand
that objects are truly a poor man's closures."  Qc Na responded by hitting
Anton with his stick, saying "When will you learn? Closures are a poor man's
object."  At that moment, Anton became enlightened.

When I first read this some time ago I thought I understood it. I did not I now understand and my aha moment today proved that. I have written way too little pure functional programming to really appreciate and use the power of closures. Not to say that objects are useless. That’s exactly the point of the quoted text. Just that the ability to define closures inside closures inside closures is a really really powerful concept. The idea is that sometimes code gets repeated in a function inside a class. Typically you pull that into a helper function and stick it on the class, but sometimes the function is so specialized that it has no livelihood in the class. Secondly the ability to bind local objects to closures, and use those closures as state is another aspect of closures that is immensely powerful.

Business of Software 2008 talk by Jason Fried of 37signals

This talk got me pumped today.

Location aware web applications

Firefox 3.1 will have geolocation support, allowing some interesting new web applications to come to the web platform. I really like what this will do for devices like the n810. They also announced alpha1 of their mobile brower version, fennec. Sadly its incredibly slow, something that hopefully will be fixed as the browser matures for the first real release.