Category: Programming


Throw Exceptions Responsibly

May 26th, 2008 — 10:41 am

Recently I had to spend some time to fix a crash in the application I’m working on: I kept getting an OutOfMemoryException with no relation to the amount of free memory on the computer.

Eventually I found the bug: turns out the DrawArc method doesn’t like to be called with a very small angle, so it goes BOOM.

Another tidbit I found while searching for the cause was the fact this behavior is not unique to this method:

“The thing I can add about this is that GDI+ tends to throw OutOfMemoryException when something else is going on. I’ve seen people complaining about this in the newsgroups and elsewhere on the internet.

So we have here a framework with the habit to fail very un-responsibly.

Hmm, why does that sounds familiar?

Somehow I get the feeling that while Microsoft employ 50 people to handle a single feature, no one is left to take care of exceptions and errors in their products.

(in the photo: Windows Vista security exception)

1 comment » | Programming

.Net Performance Pointers

March 14th, 2008 — 05:11 am

After going this week to the Microsoft performance open house, here are few things to consider:

  • Create performance counters of your own to measure various statistics.
  • Try to avoid using interfaces and virtual methods to supports inlining.
  • If you use a “Contains” method of a collection on structs, be sure to override the “Equals” method, since the default Object.Eqauls method used boxing twice - once for the parameter and the once for “this“.
  • Similarly, you should override the GetHashCode methods for structs, since the default implementation for a struct is very inefficient.
  • Use Perfmon.exe to monitor the “% time in GC” - a high value may indicate mid-life crisis.

Comment » | Performance, Programming

The Problem With Crutches

March 1st, 2008 — 02:20 pm

Coffee is good for you, as long as you don’t drink too much.
But how many of you can’t pass the day without drinking too many cups?
How many of you are addicted to coffee?

In the recent bloggers meeting in Microsoft Israel I complained about the pain it is to add a picture to a blog post using the Community server.
The reply?
Why don’t you use Live Writer to publish posts instead of the online control panel?”

In a recent post Doron wrote about his love for ReSharper:
I honestly can’t work without it anymore. It has changed the way I code, and I will never attempt a serious refactoring without it

There are many great tools out there that can make you more productive. No argument about that. The problem starts when you become dependant of those tools.

That’s the problem with crutches - if you ever lose them, you become handicapped.

Comment » | Programming, Tools

David Platt on Why Software Sucks

November 14th, 2007 — 01:28 pm

I went to a lecture by David Platt on the subject of “Why software sucks”, which discusses what is wrong with software today (hint: it’s mostly designed by programmers) and how we can improve it.
From listening to a previous ARcast with him I already knew he is a very entertaining speaker, and he proved me right (in fact, I kept thinking I could bring my wife to one of his lectures and she would have enjoyed it as well).

David began his presentation with a screenshot from the Better Business Bureau in the US, listing complaints by industry, and the amazing thing about it - people dislike software more than they dislike used-cars salesmen!

The main idea of the presentation (backup up with all sorts of amusing anecdotes):

  • People don’t buy software for the sake of the software itself - they buy software to get something done. They are not interested in using the software, they want to have used it.
  • Programmers tend to design software for people like them, while most users are very different.

Platt’s solution resolved around 5 key points:

  1. Add a virgin to the team - add someone to the team with zero knowledge about the inner workings of your software.
  2. Break convention if needed - don’t behave in a certain way just because all every application behaves the same way.
  3. Do not let edge cases complicate the main stream - extra features aimed at edge cases complicate your software and make it counter productive.
  4. Instrument (Carefully) - Try to gather information about the way users use your software without harassing them.
  5. Question each decision - ask if each decision is taking your project closer to the result or further away from it.

The lecture itself was followed with a Q&A session, in which I asked him a question (which landed me a signed copy of his book - thank again, David!):
If programmers are so bad at designing UI, why not bring in a specialist to do it?
His response (after correcting me for saying “Graphics designer” instead of “User interface designer”) was it’s actually a good idea, since most of the industry is already headed for specialization in various subjects.

I found another podcast on ITConversions network, and here are two short videos from the presentation:


Comment » | Design, Programming, Websites design

Three wrong assumptions

November 11th, 2007 — 11:52 am

Did some blog reading today, and found out several assumptions I had which were wrong:

  • Assumption: “Protected Internal” means both protected and internal
    Wrong: It means Protected OR Internal.
  • Assumption: 32bit Windows is limited to 2/4gb ram
    Wrong: It’s limited to 2gb of ram per application.
  • Assumption: OpenFileDialog is harmless
    Wrong: It loads a mini-explorer, including all the extensions you have installed.

Comment » | Programming

Every n matters

October 13th, 2007 — 01:58 pm

When you study computer science and you start learning algorithms, you are introduces to the O(n) concept, meaning there is a different between an algorithm that takes linear time (1 second for 1000 items, 2 seconds for 2000 items and so on) and an algorithm that takes exponential (2 to the power of n) time, meaning it’s a very inefficient algorithm.

Although this lesson is important, there is a darker side to this kind of thinking - you tend to ignore efficiencies with the same order of magnitude, meaning an algorithm with 10n efficiency and 80n efficiency are the same to you, even the first is 8 time faster than the second. This means that if you choose the first a certain procedure may cause the user to wait 250 milliseconds, while the second will delay the user for 2 whole seconds - big difference.

So don’t consider to n values to be similar just because they are within the same order of magnitude.

Comment » | Programming

Tainted if you do, tainted if you don’t

October 6th, 2007 — 04:10 pm

Microsoft’s latest move, to release the source code for the .Net framework, has triggered a variety of reactions due to the licencing model used in this release.

Frans Bouma thinks that looking at the source code makes you liable to legal action:

Take for example the new ReaderWriterLockSlim class introduced in .NET 3.5. It’s in the System.Threading namespace which will be released in the pack of sourcecode-you-can-look-at. This class is a replacement for the flawed ReaderWriterLock in the current versions of .NET. This new lock is based on a patent, which (I’m told) is developed by Jeffrey Richter and sold to MS. This new class has its weaknesses as well (nothing is perfect). If you want to bend this class to meet your particular locking needs by writing a new one based on the ideas in that class’ sourcecode, you’re liable for a lawsuit as your code is a derivative work based on a patented class which is available in sourcecode form.

Phil Haack partly disagrees, and sees no problem as long as you exercise caution.

I think both of them may be missing another side of this mess:
Suppose you read the source code, and some time later, after you forgot all about it, you write your own ReaderWriterLock which violates Microsoft’s patent.
Now, the hotshot lawyer working for Microsoft has no way of proving you read the source code prior to writing your implementation - but he doesn’t need to. He only needs to claim that since your code works in a similar way to the MS code, and since MS code is already out there, it’s highly probable you read it.

In other words, this move gives Microsoft an extra advantage in future patent lawsuits, even if no one reads the thing.

The conclusion: read the source code, even for the single purpose of being sure your code doesn’t violate any patent.

(thanks to dbraaten for the photo)

Comment » | Programming

« Previous Entries     Next Entries »