# Friday, June 05, 2009

Read till the end, because nothing is what it seems

What is equality?

Equality means different things in different programming languages. In most modern languages (C# for example), you can define your own terms of what equality means. In others you can't.

Take javascript for example. There are actually two kinds of equality: normal equality and being identical.

To check if two variables or values are equal, you use:

if (a == b) {
  // ...
}


To check if two variables are identical, you use:

if (a === b) {
  // ...
}


Most articles (and even supposedly good books) define the latter comparison as being equal and of the same type. While this over-simplification may be true in the majority of cases, it is very inaccurate, because you have to know what "equal" means. I’m not going to talk about javascript’s normal equality operator (==), because enough has been written about that. I’ll focus on the === operator.

The correct meaning of === is that the 2 operands should be identical. This means they should reference the same object, or, in the case of value types, the values should be the same. Only numbers and booleans are value types. Strings behave as value types, but they are actually reference types.

Bringing this all together, let's look at some examples of equality in javascript:

var a = 1;
var b = 1;

alert(a == b); // true
alert(a === b); // true

b = "1";

alert(a == b); // true
alert(a === b); // false


These are the obvious ones. Now the more interesting stuff:

var a = [1,2,3];
var b = [1,2,3];
var c = a;

alert(a === b); // false (these look equal to me, and of the same type)
alert(a === c); // true


Remember I mentioned that strings behave like value types? Now it becomes interesting:

var a = "12" + "3";
var b = "123";

alert(a === b); // returns true, because strings behave like value types


And to make it really interesting:

var a = new String("123");
var b = "123";

alert(a === b); // returns false !! (but they are equal and of the same type)


I thought strings behave like value types? Well, it depends who you ask...

EDIT: As one of the commenters (zihotki) points out, the last example adds another twist to the story. Creating a string using the String() object constructor actually doesn't create a variable of type "string", but of type "object". But you can use the object as a string. If you are unaware of this behavior, you can easily shoot yourself in the foot without knowing it.

kick it on DotNetKicks.com
Friday, June 05, 2009 10:45:55 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [4] -

# Saturday, May 30, 2009

Even before there was talk about ASP.NET MVC, there were a few open source MVC frameworks available for .NET. Among them, MonoRail was best known, but there was also ProMesh.NET, a .NET MVC web framework that evolved from a small personal project to a full-blown MVC framework for building web applications in .NET, without using ASP.NET WebForms.

Vici Project

A few months ago, just before the release of ProMesh.NET version 2.0, we decided to move the project to the Vici Project, a project that bundles several open-source frameworks for .NET 2.0. The idea of the Vici Project is to provide .NET developers with a collection of lightweight libraries and frameworks, and at the same time get the community involved in the development and support of these libraries.

To make it easier to get the community involved, a complete system was created with the following features:

  • Central SubVersion repository with online browsing (using WebSVN)
  • Automated build server (using JetBrains TeamCity)
  • Wiki infrastructure for maintaining documentation
  • Support forum

After several months of testing all of this, the project is finally ready to go live. There is still a lot of work to be done, especially on documentation, but I think there is no point in postponing the release.

The first sub-project of the Vici Project to be released is Vici MVC, formerly known as ProMesh.NET. Later this week, 2 other projects will be released as well: Vici Parser (formerly LazyParser.NET/SharpTemplate.NET) and Vici CoolStorage (formerly CoolStorage.NET)

Vici MVC 2.0 new features (compared to ProMesh.NET 1.2)

  • New powerful URL routing engine. Also supports "extension-less" URLs with IIS 7.0 or IIS 6.0 (with wildcard mapping or URL rewriting)
  • Support for view components ("inline" controllers with templated views)
  • Support for sub-templates with parameters
  • Support for template macros
  • Configurable template syntax
  • Full C# 2.0 expression supported in templates
  • New template language syntax (the old one is still supported)

Anyone interested in contributing to the Vici Project, please let me know by posting on the forum (or by sending me a private message on the forum. My username is activa)

kick it on DotNetKicks.com
Saturday, May 30, 2009 1:59:04 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [2] -

# Tuesday, December 16, 2008

Early next year, a cool new service will go online targeted at web developers and designers. It will go by the name of dStyler.com but it's a little hard to explain what the service will do, but I'll start with a simple example:

It turns thisinto this
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel arcu eget lorem dapibus molestie.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel arcu eget lorem dapibus molestie.
With just a single line of CSS

What dStyler actually provides is real-time online image generation. It is a highly scalable REST-style webservice that generates images based on a custom-built URL. For example, the box on the right is generated by the following url:

http://dstyler.com/ufnqfr/204x254/bo-333/ib-fff-2/ro-5/gr-eef-b8e/gr-fff9-fff0-210x260/sh-0008-0-r3.jpg.

It is in a way similar to the Google Charts API and the Google static maps API.

Some of the things dStyler can generate:

  • Solid backgrounds
  • Gradients
  • Single or double borders
  • Drop shadows
  • Image embedding (useful for watermarks)
  • Special effects
  • Rounded corners
  • Slicing
  • ...

The online administration interface allows you to:

  • Build images in an interactive way (to generate the URL for you)
  • Upload images to your account that can be used for generating images

Best of all, dStyler.com will be a free service

We are currently looking for people interested in beta testing this service. So if you're interested, contact me at beta -at- dstyler -dot- com (I know, I hate these obfuscated e-mail addresses more than anyone, but sadly enough, the world is a nasty world filled with f**king spammers)

kick it on DotNetKicks.com
Tuesday, December 16, 2008 9:39:10 AM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] -

# Friday, November 28, 2008

We all love unit tests (do we?). Yes we do (repeat 100 times). And we all love mocking for making life easier, but sometimes we just want to write some simple unit tests that don't require any mocking at all, because the code you are testing is easy to test without having to resort to all those mocking tricks

But what if you need to test code that relies on the date or some elapsed time? Let's say you wrote a simple caching class that automatically removes items from the cache when a certain amount of time has passed since the last access. It's tempting to slap some Thread.Sleep() calls in there to trigger expiration of cache items, but that will slow down your unit test, and most importantly, it's not accurate. What if you want to test some edge cases? Like, what happens when you access an item at the exact timeout period?

A few years ago I worked with a brilliant Java architect who simply said: "just mock the time!". What he meant was that I should create a fake DateTime class which behaves like the real DateTime class with one important difference: YOU control the time, not the system clock.

How is that done?

Simply create an interface with a single getter property of type DateTime:

public interface ITimeProvider 
{
   DateTime Now { get; }
}

Then you create 2 classes that implement this interface: one that returns the real time, and one that can be used to control a "fake" time:

public class RealTimeProvider : ITimeProvider
{
    public DateTime Now { get { return DateTime.Now; } }
}

public class MockTimeProvider : ITimeProvider
{
    private DateTime _time;

    public DateTime Now { get { return _time; } set { _time = value; } }
}

Of course the class(es) you want to test should be aware of this. For example, our cache class could look like this:

public class Cache
{
     private ITimeProvider _time = new RealTimeProvider();

     public ITimeProvider TimeProvider { get { return _time; } set { _time = value; } }

     // ... the rest of the class implementation
}

In our class implementation, all calls to DateTime.Now should be replaced by TimeProvider.Now.

And now the fun part, faking the time in our unit tests:

[Test]
public void TestCacheTimeout()
{
    ITimeProvider time = new MockTimeProvider();
    Cache cache = new Cache();

    cache.TimeProvider = time; // Tell our cache class to use our fake time

    cache.Add("A" , 1); // add an item to the cache

    Assert.IsTrue(cache.Contains("A")); // check if it's added

    time.Now += TimeSpan.FromHours(1); // let one hour go by...

    Assert.IsFalse(cache.Contains("A")); // check if it was automatically removed
}

That's pretty cool and simple, isn't it? Wouldn't it be nice if we could do the same in real life? ;-)

Next week I'll talk a little about unit testing multithreaded concurrency issues...

kick it on DotNetKicks.com
Friday, November 28, 2008 6:41:29 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] -
 |  |  | 
# Thursday, October 30, 2008

Jeff Atwood (Coding Horror) wrote an interesting blog post about detecting hyperlinks in blocks of "regular" text. I was suprised by the wave of negative comments he received, mainly because Jeff tried to solve a complex problem with a "simple" regular expression. I don't agree with these comments at all. Software developers are too obsessed with borderline cases. What's wrong with "good enough"? What's wrong with solving 99.99% of all possible situations? After all, what's the worst that can happen if the algorithm is not 100% correct? You'll see a bad hyperlink. Big deal...

Anyway, the thing is, as Jeff points out, URL's can contain some pretty weird characters you wouldn't expect, like '(', ')' and ','. This can become quite frustrating when trying to parse something like this:

My website (at http://www.mysite.com/coolpage) is becoming very popular, especially because of traffic coming from http://www.othersite.com/coolestpage, which is a cool site I recently found.

For humans, it's pretty obvious where the hyperlinks are, but what if the links contain some of the non-standard characters I mentioned above? For example:

http://www.mysite.com/coolpage(nice).aspx or http://www.othersite.com/coolest,wildestpage. Both of these are valid URLs.

If you would implement a trivial URL detector, the following URLs would be extracted from the text snippet above:

"http://www.mysite.com/coolpage)" and "http://www.othersite.com/coolestpage,".

Note the trailing parenthesis and comma. Obviously that is not what we want. The fact is that there is no way we can extract valid URLs by following a set of fixed rules, not even for humans. We humans will "parse" the URLs by looking at the context, but that's a pretty hard, if not impossible task for software.
What we can do is use some heuristics to satisfy at least 99.99% of all cases, so I started thinking about this a little bit and came up with a solution that uses regular expressions and requires no code at all.

Let's start with the basics. A URL contains 3 distinct parts, of which the last part is optional:

protocol://host/path

"host" is a hostname (or IP address), optionally followed by a port number, so this is the regex we can use for this:

([a-zA-Z-:@.0-9]+)

Of course, this will not validate the host name part of the URL, but that's not really our goal now.

Next we should specify what the path looks like. According to the specs, the path can contain any of these characters: letters, digits and any of -;:@&=$_.+!*',()

In this example, we will only try to match http and https, so our trivial regex would look like:

\b(https|http)://([a-zA-Z-:@.0-9]+)(/[-;:@&=?a-zA-Z0-9$_.+!*',()])?

That will work in most cases, but it fails miserably in our text snippet example.

Let's make an assumption: we assume that a URL containing parentheses always has matching parentheses, as in "http://www.mysite.com/Some(Nice)Page". This would exclude URLs with a single parenthesis, so "http://www.mysite.com/Some(NicePage" would not be recognized as a URL. That's a sacrifice I'm willing to make.

There are several ways of writing a regex like this, but a simple one is:

\b(https|http)://([a-zA-Z-:@.0-9]+)(/((\([-;:@&=a-zA-Z0-9$_.+!*',]*?\))|[-;:@&=?a-zA-Z0-9$_.+!*',]|%\d\d)+)?

Note that I also added some regex code for escaped characters (%nn). Nested parentheses will not be matched.

This would solve the first match in our text snippet, but not the second one with the trailing ','. Again, we will make an assumption: let's assume that a URL will never end with a period or a comma. I think that's a pretty safe assumption to make (although technically it is allowed).

Our final regex will then look like:

\b(https|http)://([a-zA-Z-:@.0-9]+)(/((\([-;:@&=a-zA-Z0-9$_.+!*',]*?\))|[-;:@&=?a-zA-Z0-9$_.+!*',]|%\d\d)+)?(?<![,.;])

This regex will match any valid URL pattern, except URLs without matching parentheses. It will also exclude URLs with a period, comma or semicolon at the end.

Again, it's not a perfect solution, but I bet it will be very hard to find a real-world example where this regex would fail to extract the URL from a piece of text. The regex can still use some tweaking, but you get the picture.

kick it on DotNetKicks.com
Thursday, October 30, 2008 6:36:30 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [3] -