Wednesday, September 13, 2006

One more nail in the coffin for Unchecked exceptions

At my current job, Im fixing bugs in a huge C# system. This system has moved from .NET 1.1 to .NET 2.0, and it was not painless but the move was considered successful. (except that resource handling has changed totally, which messes up everything, but that will be another blog) Today, Im seeing a problem with one of the components that is missbehaving when it fails to send an SMS.

The whole component (which is a stand alone service) crashes because of some unknown exception. After Ive started to investigating the problem I discover that the exception is an AbandonedMutexException. Since Im quite new with .NET/C#, I have no clue what this means. MSDN informs me that it means "In previous versions, the WaitOne method returns true when a mutex is abandoned. An abandoned mutex indicates a serious coding error. The exception contains information useful for debugging.". The exception is new for .NET 2.0 and is there to "help" developer.

So, now we have a service/component that works fine in the old .NET 1.1 but can potentially crash in the new environement. Even if there is a "serious" coding error in the product, it has worked as it should for at least 2 years. But now MS wants to "help" their developers to write better code, but the problem is that they did not tell the developers that something happened. This would not happen if C# had checked exceptions, because this would have been caught when compiling with .NET 2.0. Now it was caught by "misstake" and could have been shipped if I didn't do something wrong to provoke the error. Sure MS will help the people who is developing new software, but they just made it harder to maintain software.

I havent still made up my mind about Checked vs Unchecked exceptions. But I believe that if C# had Checked exception this problem would have been caught when moving to .NET 2.0 and not 6 months later. It was impossible to test every part of this huge system when the move was made and therefore it passed. It has come to my understanding that coding with Unchecked exceptions means catching unnamed exceptions here and there. In one component I saw 120 catch statements, 12 was for named exceptions and the rest was just "catch(Exception)". That is not handling an exception, that is hiding an exception from reaching the user or crashing the application. For me catching unnamed exceptions all over the place is bad code.


Aftermath
Sure, I found the problem. It was not a big one, and is definetly not a "serious" code error that could have horrendous effects. Yes, it was a coding problem. The problem is that it could have reached the customer, and in my eyes MS played a major part in provoking the error that could have reached the customer. Bad PR for the company, no PR change for MS. tsk, tsk, tsk.

Wednesday, September 06, 2006

Label is not JLabel, or Why wont .NET make my job easier?

Once again, I'm struggling with C# .NET 2.0, and finding out that .NET isn't really doing my job easier. My job as a software developer should not involve re-inventing the wheel every time I develop software. MS has always being good on creating tools that are simple to use and build simple things. But every once in a while I see cracks in the facade, sure it is simple to use but it is so much harder to do something advanced.

By "advanced" I mean in this particular case that I want to have a control that displays an icon and a text next to each other. A simple label that has a text AND an image. In Java, it is quite simple; create a JLabel and set horizontal position with setHorizontalTextPosition(). This has existed forever in the Swing framework, and Sun thought of it from the begining. Simple and "advanced".

In .NET it is not quite so simple; my first try was to use a Label for the text and image. The class has both a Text and Image property, so I thought I could set the text and image and it was done.. But unfortunately the control does not have a property to set a alignment relation between the image and the text. So if I set the image and text, the image will be placed behind the text and it is not very pretty. If I set the alignment of the icon and text to Left and Right, the control will still have the image behind the text but now the text looks like crap as it is right aligned. So the result is, I can either set the text OR the image; but not both. C'MON this isn't really advanced, is it?

Last week I had the same problem with a button, but (credit to MS) they actually have implemented a TextImageRelation property that fixes this "problem". The interesting part is that it was introduced in .NET 2.0, so I wonder how people managed to create a button that has text AND an image before December 2005. And the other question is why didn't they think it through and thougt of other .NET Controls that could have this problem and fixed those as well? I got the answer for you, "Because they don't think"

What is the real solution to this problem? Adding a new Label with an Icon, develop my own control?

Thursday, August 31, 2006

Changing Text on SplitContainer, what an interesting idea....

Today I rummaged through thousands lines of C# GUI code to find out why some things happen and some things didnt. Suddenly I see (through content assist) that one of the project specific controls has a TextChanged event. This control has nothing to do with text so it directly grabbed my attention.

Then I find out that it really isnt the specific control that implements the event, it is a control further up in the hiearchy. I keep going up the inheritance tree, to find that System.Windows.Forms.Control has a property named Text (and a bunch of events for text manipulations). That is odd I think, why would a base Control class have a text property when the deriving classes are not necessary text orientated controls.
  • What happens if I change the Text property on a PictureBox? Would it show the text on top of the picture?
  • What happens if I do it on a SplitContainer?
  • Or on a NumericUpDown control?
This is one of the problems with the .NET framework. It carries to much bagage from C++ and VB into the C# (and .NET) world. The old MFC libraries probably had a SetText() method, so therefore it must exist one in .NET. I wonder why they would have such functionality, and the only idea I can come up with is: It is simple if all text specific properties are named the same, and thus every control should have one. "If we keep it in Control, then we dont have to write code for text handling in every control". But what about all those controls that dont have a text in them? They must override this property and do nothing, so now we have code that hides other code just to remove a functionality. This does not make sense to me.

The MSDN will of course answer the question if the Text property actually works or not. On those controls that do not support them, it will say "This property supports the .NET Framework infrastructure and is not intended to be used directly from your code.". Sheeesh, that makes we want to think twice before using the Text property on a Label or Button. Perhaps the Text property is not intended to be used directly in my code on a button? The best explaination is (of course all these overriden properties have different wording) "The Text has no affect on the appearance of the NumericUpDown control; therefore, it is hidden in the designer and from IntelliSense.".. I would like to see that code in the IntelliSense.

doPopupMenu(Control)
if (control is SplitContainer) || (control is NumbericUpDown)
{
menu.removeTextProperty()
}




Or is there any valid reason that every Control has a Text property?

Friday, August 25, 2006

Nant is definetly not Ant (build MSBuild is)

As a newly born .NET/C# developer, I have trouble finding those must-have-tools. At my current job, they are running VS2005 in a huge system with over 70 seperate components/solution files with each having around 2000-10000 LOC. This gives me a huge headache as it takes forever to build, and everything is built with batch files. (so 90-ish)

But as a java developer, I have come to love Ant. There is a .NET version of it called NANT that I downloaded and was going to test and hopefully replace the batch files. 10 mins after downloading I stumble into the first problem. NANT does not support (as of 25 aug 2006) Visual Studio 2005. NANT can not handle the new solution files as they have change completly (not surprisingly). Apparently it is quite a job to add VS2005 support, so it seems that it is not going to happen. So NANT is definetly not ant, as it will probably cease to be used.

The new king of building MS code is MSBuild, which seems to be a total rip-off from Ant.

Wednesday, August 16, 2006

Its 2006 and MS has still learned nothing about installations!?

Due to a contractor job, I need to get some Visual C#/.NET/MSSQL experience. So I was going to install Visual C# Express (or as it is called Visual Studio 8, or other). I got the 3 mb installation file and started the installation (I hoped). The installation program did what they always do, Greetings&Saluations, Accept eula, Install MSDN and MSSQL?. Sure I could go for those as well. The program told me that I needed 1.7++ GBs of hard drive for this installation. Fine, I have plenty of space on my D-drive (data&Programs), which I told the program.

But wait a minute! That isnt how Ms wants it!

If I choose to install everything onto the d-drive, it would install 60 MBs on the D-drive and a whooping 1.7 GBs on the C-drive. But I thought I told VS to install on the d-drive and not onto the c-drive. Of course, I understand that VS requires some files to be installed on the c-drive. But 1.7 GBs, that is ridiculos.

Ok, regroup. Lets just choose the VSC# package, and I still want to install on the d-drive. Now it still wants to install 60 MBs on the d-drive and only 314 MBs on the c-drive.

When I left the MS development environment for six years ago, they still had this idea that I as a user shouldnt be allowed to make decisions like this. Still, they order me around and forces me to install everything on the c-drive. (The c-drive is already packed as every other program/driver wants to install onto the c-drive). I guess I can solve this the MS-user way, do a re-install every two years.

At least with Java, I'm old enough to choose where to put things.