Naughty ArgumentException

Writing a function you probably write following checks as a reflex:

void DoSomething(string argument) {
    if (argument == null) {
        throw new ArgumentNullException("argument", "No nulls allowed!");
    }
    if (argument == string.Empty) {
        throw new ArgumentOutOfRangeException("argument", "No empty strings!");
    }
    if (Environment.Foo && (argument == "bar")) {
        throw new ArgumentException("argument", "Something else!");
    }
    ...
}

Yep, there is an error in third “throwing”. For some unknown reason ArgumentException needs parameters in a different order. Correct statement would be:

    ...
        throw new ArgumentException("Something else!", "argument");
    ...
}

What will happen if you mess it up?

Well, nothing much. Exception will still be thrown and your code should be fine. You will see wrong exception text but any stacktrace will probably lead you to correct spot in code.

But it looks ugly.

Reader Is Gone

Few days ago Google made announcement that Google Reader is going the way of dodo. Personally I was quite annoyed with this because it was my RSS reader of choice, but there is enough time to search for alternative.

There were few reactions to this decision but most of them stayed in realm of realism. And then came Dvorak with his suggestion of Google releasing source code to public domain. Regardless of wishful thinking, this is never going to happen.

Google Reader is an old product and I can guess that its backend it very much reliant on Google’s infrastructure. Releasing it into wild would require quite a lot of work to make internal dependencies go away. Company that is closing down a product will not spend their engineers time to prepare product for future competitor.

Nor should it. Nobody expected WordPerfect to become open source as it was dying. And that was an excellent program that had a lot of users. Why would expectation be different for any other program just because it is on web? And don’t give me that crap about being able to use last version indefinitely. When program dies everybody switches to something else regardless of how good the last version was.

Even if it went open source I don’t think it would stay the Reader we know. First it would lose its simplicity. Everybody would add a feature or two to solve their particular problem. Lean Reader would soon start to get some fat and it would go the way of Firefox - nice browser that got way too much configuration options with time.

Google Reader is dead. Face it.

PS: Yes, I think that Google is being a bit evil. But it is not their first time nor it will be the last.

Changing Network Name (Etc.)

Illustration

As I was playing with my wireless router, I noticed that Windows started referring to my text network with number two (2) in suffix. Each visit to Network and Sharing center resulted in annoyance since my good old MedvedAlt network was now named MedvedAlt 2 without any obvious way to change it.

We start adventure by entering gpedit.msc to Windows start menu. This will open Local Group Policy Editor in which we have to navigate to Computer Configuration, Windows Settings, Security Settings, Network List Manager Policies. There we can see our network in all its glory. All that is left at this point is to go into Properties and change Name setting from Not configured to whatever name we prefer.

TextBox With a Cue

Illustration

On web it became very popular to have gray, watermarked, text that offers a cue for filling out a field. As you start writing, cue disappears.

For a while now, Windows have full support of this feature. However, Windows Forms in .NET framework are bit behind. Fortunately implementing this feature is not really hard. All we need is property (e.g. CueText) in a class inherited from TextBox.

Setting value is done by sending a EM_SETCUEBANNER message to TextBox:

NativeMethods.SendMessage(this.Handle,
                          EM_SETCUEBANNER,
                          new IntPtr(1),
                          new StringBuilder(value));

There is no need to check this function for success. If visual styles are turned on, it will not fail. And even if it fails, there is not a thing we can do to fix it. Ignoring result code is as good solution as any.

Retrieving value via EM_GETCUEBANNER is a bit more involved:

var text = new StringBuilder(256);
var res = NativeMethods.SendMessage(this.Handle,
                                    EM_GETCUEBANNER,
                                    text,
                                    new IntPtr(text.Capacity));
if (res.ToInt64() != 0) {
    return text.ToString();
} else {
    return null;
}

This time check for valid result is warranted. If result is good, string will be returned. Otherwise, null gets to mess things up.

These few lines (along with Interop definitions) are all that is needed for this useful functionality.

Sample project is available for download.

Multi-statement Conditions in PostgreSQL

Conditions are really easy in SQL Server. Simple IF at the top and multiple SQL statements in the middle:

IF (SELECT Foo FROM Test WHERE Bar='X') = 42
BEGIN
    DROP TABLE Some;
    CREATE TABLE Some(Thing INT);
END

Unfortunately this is not a standard SQL feature so you cannot count on other databases having it implemented in same manner. One database that does not really support this is PostgreSQL. No matter how you do it, you cannot use pure SQL for conditional execution of multiple SQL statements.

What we can do is fake it:

CREATE OR REPLACE FUNCTION TEMPSQL() RETURNS INT AS '
    DROP TABLE Some;
    CREATE TABLE Some(Thing INT);
    SELECT 0;
' LANGUAGE SQL VOLATILE;

SELECT Foo,
    CASE Foo WHEN 42 THEN
        TEMPSQL()
    END
    FROM Test WHERE Bar='X';

DROP FUNCTION TEMPSQL();

All SQL statements sit inside of a function and condition is modified to use standard SQL 92 CASE statement. SELECT will trigger execution of TEMPSQL function every time Foo is equal to 42.

Notice that this means there should be nothing in function that prevents it from being called more than once. Alternative is to do condition (Bar='X') on field that is unique or just adding LIMIT 1 to statement.

PS: For homework, check why function has INT for return type instead of more logical VOID.