Ivan Krivyakov's Blog

Premature optimization is the root of all evil

May 6, 2010

Lost your thumbnails? Refresh Windows Components

For real. When you install a program that associates itself with image files, you may lose your thumbnails capability in Windows XP. It shows a giant icon of the program instead of the thumbnail. How to fix it? It’s elementary, Watson, Just go to Control Panel->Add or Remove Programs -> Windows components, don’t change anything and click “next” a couple of times. That’s it! The rumor goes, that if while doing that you three times say loudly “Microsoft rules”, it works even better. :)

May 4, 2010

Webdav – a nice alternative to FTP

Recently I needed to let remote users to transfer several hunderd megabytes of data to my machine. In the old days I would just go ahead and setup an FTP server, but it is so-o 1975. It is insecure, requires pinching wholes in the firewall, et cetera, et cetera.

So I googled for the alternatives and chose WebDav. I setup a WebDav folder on my Apache with relative ease, and then they used built-in Windows client (“My Network Places”) to access it. Of course, for it to be secure one needs to have https, and it is not very easy to setup, but fortunately I’ve already got it.

Of course, we used manual uploads. The user just opens my webdav folder in Windows Explorer and drags files to/from it. if automated uploads are required, webdav may be harder to tackle then FTP, since there are fewer command line clients. But you need only one that works, right?

Bottom line – if you have your own web server with HTTPS, webdav is highly recommended for secure file sharing.

BTW, I ran afoul of Windows data redirection again – Apache simply would not pick up my configuration changes, until I realized I am making them in a legacy editor (FAR) without elevated permissions, and all changes go into my local profile.

May 3, 2010

Live+Press: Victory!

I have downloaded Live+Press plugin for WordPress that can synchronize my WordPress-based blog and my LiveJournal. Of course, with my luck, nothing worked out of the box. After several debugging attempts I found that

1. PHP will automatically encode special characters in any posted form field, whereas “foo’bar” becomes “foo\’bar”.

2. This happens even to passwords.

3. The password hash then comes out wrong and LJ won’t accept it.

4. Needless to say, my password contained a special character.

Moral: never, ever do good deeds prematurely. If someone wants to write strings to the database, let the database worry about the special characters. Not everything POSTed is intended to a database, so there is no point (and it is even harmful) to automatically escape it.

PS. Why does the trackback link look so ugly? Well, I will deal with it later. Meanwhile, sorry for its appearance.

April 8, 2010

Test of cross-post to Live Journal

This is a post to my mostly programming blog, let’s see if it shows up in LJ

March 19, 2010

Temporary Measures

Temporary measures of today tend to become the legacy of tomorrow.

February 5, 2010

IE 8: Where did that tab go?

I am now using Internet Explorer 8 at work (corporate policy), and I noticed that sometimes I cannot find the tabs I open with “open link in new tab”. Firefox always puts new tabs in the right end of the page. This is not very convenient if you have many tabs, but at least I know where to find it.

IE authors appearantly decided to improve on that. A new tab will open immediately to the right of the current tab. But there is a catch. If I open several links in rapid succession, they will appear in the order I open them. I.e.

CURRENT_TAB | LINK1 | LINK2 | LINK3

However, if I pause a little before opening the fourth link, it will appear immediately to the right of the current tab, like this:

CURRENT_TAB | LINK4 | LINK1 | LINK2 | LINK3

It all depends on time between the opening of two links. If I open the links quickly enough one after the other, new link will go to the right of the previous link. If I am not quick enough, new link will go to the right of the current tab.

Since, unlike my computer, I do not have a built-in clock that measures “quickness”, the position of the opened link becomes pretty much random. This is especially annoying if you have many tabs, much more annoying the Firefox. At least it is consistent.

I started with a feeling of general dissatisfaction, such as “why sometimes I cannot find my tabs?”, until I finally took time to reverse engineer their approach.

I imagine IE design team had countless meetings on the subject and this is the compromise solution they arrived to. Frankly, it’s a mess. If I could, I would disable this feature and always have it open new link at the same place, no timers invovled. This way, I at least would no where my tabs went.

December 12, 2009

Notwithstanding The Above…

I was downloading C# 4.0 feature list, and Microsoft presented me with a several page license document. Somehow, when I see words “nonwithstanding the above” in a text, I don’t expect anything good to come from it.

December 11, 2009

Public Classes, Protected Methods and Limitations of C# Visibility Control

I had an interesting problem today. I had a public class in my assembly called Session. It was a public class, but it used a lot of internal stuff in the implementation. It went something like this:

public class Session
{
    (public methods and properties here)

    private void HandleResponse(Response response) // Response class is internal
    {
        …
    }
}

Later I needed to split it into two classes: MarketDataSession and TradeSession. They have a lot in common, but they need to handle responses slightly differently. I decided to keep shared functionality in Session making it a base class. So, I modified Session like this:

public class Session
{
    (public methods and properties here)

    protected virtual void HandleResponse(Response response) // Response class is internal
    {
        …
    }
}

public class TradeSession : Session …
public class MarketDataSession : Session …

I immediately ran into trouble: inconsistent accessibility, Response class is less accessible then method HandleResponse. Indeed, HandleResponse is now a protected method of a public class, so theoretically it can be overriden outside of the assembly.

I could make Session an internal class, but public classes TradeSession and MarketDataSession are not allowed to derive from an internal class.

The only solution that allows me to keep Response class internal is to mark HandleResponse method as internal. This makes it invisible from outside of the assembly, but now it is wide open inside the assembly, which is also not so stellar.

There is another, rarely used access control option, protected internal, but it actually means protected OR internal, i.e. the method is accessible to anyone inside the assembly and to derived classes outside of the assembly.

What I need here is protected AND internal: accessible only to derived classes within the same assembly. AFAIK, this option exists in IL and in C++/CLI as private protected . Here’s a real example where it would be useful in C#.

Parsing .NET Enums: some unit tests

Of course, I had to verify that enum parsing really works as they say it does (see previous post). So I wrote a little unit test. All tests in this class pass:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTests
{
    [TestClass]
    public class EnumParsingTest
    {
        enum Test
        {
            Foo = 10,
            Bar = 20
        }

        [TestMethod]
        public void ParseStringValue()
        {
            Assert.AreEqual(Test.Foo, Enum.Parse(typeof(Test), "Foo"));
        }

        [TestMethod]
        public void ParseIntValue()
        {
            Assert.AreEqual(Test.Foo, Enum.Parse(typeof(Test), "10"));
        }

        [TestMethod]
        public void ParseUnrelatedIntValue()
        {
            Assert.AreEqual((Test)40, Enum.Parse(typeof(Test), "40"));
        }

    }
}

Parsing .NET Enums

I needed the following functionality:
- class MarketSession has property MarketType (options, futures, …)
- this property is an enum, but in the underlying protocol it is actually a number
- in my Spring.NET configuration I wanted to specify a string which would be either a symbolic name or a number; one would use symbolic names for defined market types and numbers for the market types that did not make it yet into the enum

I was pleasantly surprised to find out, that the built-in Enum.Parse() provides exactly what I need. You can give it either a symbolic name, or a numeric value, and it will work fine, even if the value is out of range.

enum MarketType
{
    Futures = 10,
    Options = 20
}

public class MyClass
{
    MarketType _marketType;

    public string MarketType
    {
        get { return _marketType.ToString(); }
        set { _marketType = Parse<MarketType>(value); }
    }

    private static T Parse<T>(string s)
    {
        return (T)Enum.Parse(typeof(T), s);
    }
}