Bug in C#

Apparently Microsoft has no easily accessible place to report bugs in their programming languages, so I’m posting it here and hopefully someone who can fix it will eventually read it (and maybe even put an easy-to-find link up to report bugs on MSDN?)

If it’s not a bug with .NET itself, perhaps someone can tell me what I’m doing wrote here, C# newbie that I still am.

I have a DataGridView bound to an ArrayList of objects of the following class:

public class SeriesPoint : IComparable {
    private double _price;
    public DateTime? Timestamp;

    public double Price {
        get { return _price; }
        set { _price = value; }
    }

    public string Date {
        get { return Timestamp.ToString(); }
        set { Timestamp = DateTime.Parse(value); }
    }

    public SeriesPoint() { _price = 0; Timestamp = null; }
    public SeriesPoint(double p, DateTime? d) { _price = p; Timestamp = d; }

    //These are compared only based on date.
    public static bool operator<(SeriesPoint lhs, SeriesPoint rhs) { return lhs.Timestamp < rhs.Timestamp; }
    public static bool operator>(SeriesPoint lhs, SeriesPoint rhs) { return lhs.Timestamp > rhs.Timestamp; }

    //Equivalent in C++: return rhs.timestamp == timestamp;
    public override bool Equals(object obj) {
        if (obj == null || !(obj is SeriesPoint)) return false;
        if (obj == (Object) this) return true;

        return ((SeriesPoint) obj).Timestamp.Equals(Timestamp);
    }

    //Nothing special about how it's hashed.
    public override int GetHashCode() {
        return base.GetHashCode();
    }

    public int CompareTo(object other) {
        //Nulls to the beginning.
        if (this == null && other != null) return -1;
        if (this != null && other == null) return 1;

        if (this < (SeriesPoint) other) return -1;
        if (this > (SeriesPoint) other) return 1;
        return 0;
    }
}

And I run the following to attempt to find the last null value in a sorted list:

int lastnull = sortedpoints.LastIndexOf(new SeriesPoint(0.00, null));

This actually works, but when the function exits and control presumably returns to the main loop, the program suddenly crashes with the following exception in Application.Run() (not even running on the same thread as my code!):

“An unhandled exception of type ‘System.InvalidOperationException’ occurred in System.Windows.Forms.dll

Additional information: Operation is not valid due to the current state of the object.”

I checked all of my code, put try blocks around things, made sure nothing was throwing when it shouldn’t be, removed functions, rewrote functions, reordered functions, checked boundary conditions… nothing. My code is running fine and returning correct results. .NET is then crashing. I have no idea why, but the data binding is definitely a possibility.

I suppose my best course of action is to remove the Equals() method (this fixes the problem) and rewrite my code without it.

Type safety in newer programming languages

Am I the only one who finds newer programming languages, such as Java and C#, to take strong typing to a very undesireable extreme? It seems like a reaction to most programmers not being able to get type safety right in C++, but it ends up making the code look awful, obscuring its function, and requiring a lot of extra work to explicitly cast things around. Especially when working with arrays, it oftentimes requires creating entirely new loops dedicated solely to the purpose of casting. This is stupid in many different ways. ConvertAll sort of helps, but it’s clunky and doesn’t work for collections. Also, why does the cast operator have lower precedence than the dot operator? I would think the other usage would be more common.

In Perl, which I now consider my favorite language (because it doesn’t presume to impose seemingly arbitrary restrictions on the programmer), you can join an array of doubles into a string in one line of code. You can do it in two in C++, which isn’t bad, since the stream operators have overloads for numeric types. Try it in C# or Java and you’ll need at least four or five lines.

I think I prefer the flexibility of weak typing despite the added risk. It’s a pity that “hot” languages are all following the “programming with mittens” paradigm right now.

Sci-Fi has it wrong: ET would not have the same senses as us.

Everything we can directly perceive with our sight is contained in a band of light about 400nm across. This band of light happens to coincide with the type of light achieving penetration through earth’s atmosphere, which, of course, is not a coincidence at all. Our sense of sight grew up around this band of light and it would be foolish to expect extraterrestrial sources of life, should they exist, to see the same band. This throws another wrench into sci-fi cliches 🙂

The same goes for any sense that depends on properties specific to Earth or the Sun. For that matter, they probably couldn’t breathe our air, either.

Cities and tumors have common shapes

There’s a video of cities at night up on Youtube. As beautiful as they say the cities are, one thing that struck me is the similarity in shape of some of the cities to tumors. This surprised me, since cities grow according to planned rules and tumors do not. Perhaps the conditions required for growth dictate a certain shape? Certainly the individual structures have analogues – blood vessels and roads, for example, both carry vital nutrients for their respective growths.

The interesting question is whether we can use any studies on the growth of cities to come up with tumor growth models as well, or even whether certain factors that plague cities could also be used to fight cancer.

FancyFeast Website: An example of what happens when you forget usability

This is a website critique.

My cat is rather choosy, and FancyFeast is the only brand of cat food she appears to like (and even then, she doesn’t seem to like the seafood flavors). Nevertheless, she is getting rather heavy and I wanted to see what the nutritional content of her food was. So I navigated to fancyfeast.com, expecting to find this information fairly easily.

FancyFeast has a very nice looking site. The graphical content of the site matches their brand image, I suppose. Nevertheless, there are some severe usability problems:

The first thing that greeted me on their website was a Flash intro with audio. Audio-by-default is always a bad idea, but no problem… there’s a skip intro button and it’s not too loud.

Then I get to the main menu of the site. It looks reasonably well-laid out at first. I was looking for information about the food itself, so I clicked “Feast”. Ack, there’s another Flash video! And there’s no skip button this time!

About 10 seconds of pointless video go by, then I’m finally presented with three options: Poultry, Beef, or Seafood.

I click on Seafood, although I was hoping for a table that had the nutrition info. of all of the different types of food on one page like most restaurants have. Another 10 seconds of meaningless video and I’m left at another menu page: “With Gravy”, “Without Gravy”, or “Dry”, with about five different products in each category. *Shrug* Ok, let’s try “Without gravy”, “Flaked”… another video… finally! Individual products! Now I can get the nutrition info for them!

I clicked on the can marked “Flaked Tuna Feast”.

Nothing happened.

Clicked on the other cans.

Nothing.

Clearly, the nutrition info was not here, so I tried the “Satisfaction” link on the main menu. The music came back again, along with a bunch of random verbs that popped up and faded out seemingly at random. One called “Know” popped up, and I immediately dove after it before it could disappear, thinking it might have had information about their products.

A page popped up with the rather bizarre headline: “She lets me know what makes her feel good.”

It was at that point that I chose to leave.

Lessons to learn from this site:

  • Center your design around the information that visitors might be interested in seeing. They visited because they’re looking for something.
  • If you use audio on your site, start it muted.
  • The maximum number of links a user should have to navigate through to get to any useful content on the site is 3. And even that’s a bit generous.
  • The transition between these pages should be instant or near-instant. If the transition takes more than 3 seconds, it is too long.
  • Don’t place usable-looking links to useless content. Clearly separate what is just “fun” on your site with what is actually usable. That means words like “Know” should probably not appear as links unless they point to knowledge.
  • The most important sections of a site should be the ones that are most emphasized. The “Anticipation” and “Satisfaction” links are not important enough to merit top-level headings.

A board of polymaths…

A board of polymaths is quite a powerful thing. I’m beginning to realize that, between just the four of us that are on the board right now, we probably have some degree of coverage of about 3/4 of all major disciplines. By the time we fill the board with the originally-planned 7-9 members, we’re going to essentially have a group that can do almost anything. I’ve never, in my entire life, seen a group with that breadth of ability before.

We still need a lawyer and an accountant, though.

Told 'em!

When I interviewed with Google, I spoke with several people on the Maps team. I mentioned that they needed to address the privacy issues of street view before they were sued. I suggested facial recognition and suggested some possibilities, primarily Eigenfaces. They responded (correctly) that what they were doing was not technically illegal. I argued that it was still going to be perceived as a violation of privacy (this is the company with the motto “Don’t be evil”, after all).

They did not listen. And now they are being sued. It’s definitely only the first of many, too.