Dan Byström’s Bwain

Blog without an interesting name

Nine While Nine

Posted by Dan Byström on May 22, 2005

This week I attended the Nordic Software Developer Summit, or NorDev 2005 for short and I’m back with a lot of impressions and mixed emotions.

Listening to and seeing Jimmy Nilsson and Eric Doernenburg doing "pair programming" while demonstrating TDD (Test Driven Development) was one of the more inspiring experiences. When I later talked to Eric on the evening I noticed I got a little dizzy, assuming it was from the wine and hunger. But I later understood that the real reason was because he informed me that doing pair programming means that you ALWAYS are two programmers in from of one computer. Not just occasionally as I had thought. This is an almost incomprehensible concept for a lone wolf like me. But I see no reason not trusting him on this one. Therefore, I just got dizzy.

The three speeches in a row on SOA may have been just great. However, with not a single line of code in sight, they were not the thing for me. I consider my goal to be cranking out code that works better and are easier to use than my customer had dared to hope for (that’s my goal anyway) – not trying to tell them what they need, by drawing nicely looking diagrams describing their business processes and make them say "ahh!", "ooh!", "really?" and "we didn’t know that!". Other people can do that.

Listening to David S Platt’s talk on Web Service security was indeed very entertaining, but he pulled off such an embarrassingly large number of jokes about sexually frustrated geeks, that I arrived at the conclusion that surely it takes one to know one.

Johan Lindfors from Microsoft demonstrated what’s to come in Avalon and the impact it will have on today’s GUIs can probably be compared to going from gray dull dialog boxes to cool web pages in the late 90’s. But remember: just because someone shows you a GUI that is so cool that your brain gets drained of blood, DOESN’T MEAN THAT THEY KNOW HOW TO MAKE EVEN A TINY HELLO WORLD PROGRAM TICK. It is like assuming that someone being good at airbrushing cars is just as great at building engines.

I consider myself, for a programmer that is, to be really good at GUI issues, but most probably, the graphical skills required to make use of Avalon’s full potential will bring me to my knees.

The world also turned out to be small as always. At lunch I sat down beside a couple of guys I’d never seen before. When I asked them were they were from, I found out that they work less than a 100 meters from my office in Växjö (460 km away).

I regret not leaving the .NET track in favor of the Java track and listening to Rickard Öberg’s speech on AOP (Aspect Oriented Programming) though, which was great I’m told. But until I learn of any good AOP tools for the environments I’m working with, I guess I can live without knowing more about it anyway. It would be just like waving a candy bar in front of a child and not letting it take the candy.

Finally I really must say I don’t agree with the expert panel’s conclusion that choosing between .NET and Java would be a "religious thing". It shouldn’t work that way for professionals. I’d use Java any time a customer wanted me to and/or it would look like it was the best choice. I’d use Fortran or Cobol on that same ground also (although I’d probably not be their best choice for a job like that).

  It’s a poor craftsman who blames his tools.

I traveled by train. On the way up the train was 55 minutes late. On the way back I tried to rebook my ticket to an earlier train because I got away an hour earlier than expected because I found Dino Espositio’s speech on ADO.NET totally uninspiring. SJ refused to rebook my first class ticket because I hadn’t paid for the most expensive flavor of it. That’s exactly in SJ’s spirit of aiming to be one of the worst run companies ever seen. Come on, their tickets are more expensive than the cost of driving the same distance by car (with just one person in it) or by airplane??? I mean, flying is almost pure science fiction, while running train along pre-laid out iron bars can be (and obviously are) done by idiots.

Goodbye train! Hello airplane, here I come!

Posted in Uncategorized | Leave a Comment »

More on nulls

Posted by Dan Byström on April 19, 2005

I’ve previously written a couple of blog posts on an idea of mine: the possibility to have language support for avoiding null reference exceptions.

I had planned to close the matter down in an article claiming that a “with” keyword could be implemented in C# with the ability to take a different path when the “with”-expession couldn’t be evaluated because a null reference exception would occur trying to do so.

Example:

with ( Order order = a.b.c.d.e )
{
    ...
}
else
{
   // either a or b or c or d or e is null and therefore
   // it would be pointless to enter the "with" block
}

Well, now it turns out that I’m not the only one having felt the need for something like this. It is implemented in the Cω research language:

  Cω takes this one step further with the behavior of returning null instead of throwing a NullReferenceException on accessing a field or property of a nullable type whose value is null.

I knew it was a good idea!!! 🙂

Posted in Programming | Leave a Comment »

Still alive

Posted by Dan Byström on April 19, 2005

…although my absence from here might have lead you to believe otherwise.

The answer is simple: I have way too much work for finding the right "blog mode".

I’m working on an exciting project (in C#) where I’ve come up with a number of interesting things to write about in the future, when I have more time. For example, a vast amount of new GUI UserControls, as well as an interesting solution for synchronizing data between clients by using direct socket communication instead of bogging down the database.

Posted in Uncategorized | Leave a Comment »

Happy New Year

Posted by Dan Byström on January 3, 2005

…to all you out there. Especially to my six(!) subscribers on Bloglines – whoever and wherever you may be.

Although Joel on Software, with almost 8,000 subscribers doesn’t need any promotion, I simply must quote this:

  I don’t care how much you know about continuations and closures and exception handling: if you can’t explain why while (*s++ = *t++); copies a string, or if that isn’t the most natural thing in the world to you, well, you’re programming based on superstition, as far as I’m concerned: a medical doctor who doesn’t know basic anatomy, passing out prescriptions based on what the pharma sales babe said would work.
— Joel Spolsky

Well spoken, what can I add to that? Maybe just the plain observation that in these days when security issues become increasingly important, the above statement is a prime example of code vulnerable to the "buffer overrun" security attack. OK, that was just a lame attempt trying to show off.

Hope we’ll all get a great 2005!

Posted in Uncategorized | Leave a Comment »

.PNG + .NET

Posted by Dan Byström on December 14, 2004

The choice of .PNG as the file format for your Windows Forms graphics is superior to .BMP, .GIF, .JPG and .ICO.

The proof of this statement is left as an exercise to the reader. (Hint: Alpha channel.)

Posted in Programming | Leave a Comment »

Quest Completed

Posted by Dan Byström on December 8, 2004

10,000 experience points gained.

I’ve done some serious searching in the .NET help files as well as hard core googling to find out how to do design time interaction with a UserControl. I thought this would be one of the first tings everyone would like to try out in .NET. 😉 But it was amazingly hard to come up with the relevant keywords. I found numerous of really really exciting stuff along the way, so this time was by no means wasted.

This is a reminder of how easily we (normally) can find information these days. Back in the eighties information was found in magazine articles, in books arriving three months after ordering them and by disassembling core products. Not a day passed without tracing and disassembling parts of MS-DOS and the BIOS. Those were the days.

In the nineties, all of a sudden, it was possible to use CompuServe to get in touch with other people to find answers! Then came news servers and finally the whole thing just exploded.

It was way back then, when I got a strange thing tossed in my hands, called MFC. It was called a framework and used some buzz-word technology called object orientation. Surely nothing a real programmer can benefit from I thought (and was proven right, right?), but anyway it was raining and I had nothing better to do so I did something that I had wanted to do for more than ten years: I wrote my own Qix-clone. What? Did I notice you shake your head and smacking your tongue??? You’re obviously no geek at all and shouldn’t be here! Now; just leave! Go on! 😉

The concepts of inheritance, abstract classes, virtual methods, overloading, overriding, aggregation and so on were all completely new to me, but I immediately felt totally at ease with them. I was thrilled realizing that, after creating my monster class, I could just inherit from it in order to get my fuse! Way cool I thought! I created my game vector-based and had to come up with a lot of vector manipulation routines. How do you calculate the area of a (closed) polygon? How do you find out if a point is inside or outside a (closed) polygon? I found the answer to the latter question and was immensely proud of it. I later found out that it was a well known algorithm, but anyway… nothing beats doing it yourself.

Qix remained on some backup disk for some years until another buzz-word appeared: Java Applets. Converting the MFC C++ code into Java wasn’t that hard, and boy was I lucky that I hadn’t relied on the Windows API to help me with regions. How do you fill a polygon with a bitmap without any framework support? Go figure! 😉 vdQix can still be played here, at some periods it has had more than 10,000 hits a month. But this story is not about Qix, instead it is the story of my “point inside a polygon” algorithm.

At roughly the same time, I was working on a VB project where we wanted nicer looking buttons than Sheridan’s controls bundled with VB3 could accomplish. We bought a package calls VB-Tools (more popularly called VB-Fools) which contained a vast number of controls. Each being so poorly designed and full of bugs that I got irritated enough to roll my own.

I ended up with a button control, a tooltip control, a control to monitor processes and a hotspot control (DBPush.vbx, DBTTip.vbx, DBAppMon.vbx and DBHots.vbx). They came out pretty well and I uploaded them to various places and they spread like wildfire. For years I had daily conversations with happy users. A few quotes can be found here. Very rewarding indeed. They almost even made it onto the MSDN CDs!

The DBHots.vbx was directly inspired by my “point inside a polygon” algorithm developed for my Qix game. It relied upon the fact that a VBX was able to interact with a user at design time and that a VBX could be transparent. VBXs were just great in their simplicity! Then along came ActiveX with 32-bit VB4 and both these features were gone! With VB5 and the OCX96 revision these features were put back in, but then it was too late. No ActiveX version of DBHots ever saw the light of day.

Now we’ve come full circle in explaining why I would like to do mouse interaction with a UserControl in .NET. Some things may lurk for years, waiting for the right moment. Creating a .NET version of DBHots is something that simply must be done before I can rest. The next thing I’d like to know if it is possible to have my UserControl not being created as a control at all at run-time, but rather become a component so I don’t have to waste a Window handle on it.

The class I was looking for? It had the most obvious name imaginable: ControlDesigner.

Posted in Nostalgia, Programming | Leave a Comment »

With Zwei.Drei.Vier.Links()

Posted by Dan Byström on December 1, 2004

In preparation for a continued discussion of how a language could assist us in checking for null references, I’ll state what I see as drawbacks with the With-statement in VB (or Pascal for that matter, where it has its origin). Eh, say that again? Well, it’ll become clear later on.

In VB6 I used With a lot. In the world of COM it was not merely a convenience, but also an optimization technique, since the programs ran faster when calls to AddRef/ReleaseRef were reduced. In .NET that’s not an issue anymore.

Anyway I still miss it in C#.

Consider the following piece of VB code:

With Customer(42)
  With .Order(3)
    .Lock()
    Process .Order(3)
    Customer(42).Update
    .Unlock()
  End With
End With

Never mind the object names or why you’d like to do exactly something like this. I’m only interested in the implications made by the With statements. We can observe that:

  1. We can only have one “active” With at a time, so nesting them won’t help us access the outer With. Nested Withs also makes the code hard to read.
  2. If we have the need to pass the “withed” (I don’t have a better name) object down to a method (like the Process method in the example above) we have to reevaluate the “withed” object.
  3. Entering a With block with a null/Nothing reference is completely and utterly useless, yet it is allowed, and the exception won’t occur until we try to access the first method/property of the “withed” object.

Given those drawbacks, it may be understandable that the With statement wasn’t made part of the C# language. I think 1) and 2) are quite obvious, but 3) is more my own personal reflection and something I want to improve upon!

What if we could overcome all three issues? Stay tuned…

Posted in Uncategorized | Leave a Comment »

Before the idea gets stored in /dev/null …

Posted by Dan Byström on November 25, 2004

…I’d like to share it with you. Actually it’s some minor observations that suddenly came clashing together quite nicely.

Three state if-statements

The other day I read a discussion regarding the nullable object pattern. At first I thought it was mostly an academic idea – too much code for little or certainly questionable gain. Then all of a sudden I realized that I’ve been doing almost the same thing regularly for years. But I haven’t actually had a name for it. If I were to call it something I’d rather chose “default objects”. I too have made use of a construct that saves me repeated testing for null.

Yesterday I chatted with Mats Helander and we had the same feeling regarding expressions like this:

if ( x!=null && x.p==0 )

It is SO much simpler than having to write:

if ( x!=null )
  if ( x.p==0 )

In this trivial case it is not really a big deal, but consider:

if ( x!=null && x.p!=0 && y!=null && y.p!=0 && z!=null && z.p!=0 )
  // do something
else
  // do something else

It gets really messy – especially the else part!

So what’s wrong with the first statement then? Well, it is mostly a feeling that it’s a hack. I really don’t like the idea that the order of evaluation of an expression should matter at all. If the compiler should like to evaluate my expression from right to left, it should by all means do so (although it, luckily, doesn’t). And again, this construct is really convenient.

I noticed that when working with strings, this type of expression:

if ( strName!=null && strName.Length!=0 )

kept reoccurring so often that I suggested to one of the members of the C# language team that a new keyword could be introduced, possibly called “empty”. It could look like this:

if ( !empty(strName) )

My idea was that the compiler would just produce the expanded version silently with no fuzz. One objection certainly is that a complier shouldn’t care about naming of properties – why would a compiler have something so say about a property being called “Length”? But as I see it, C# really does that already with the “using” statement! Apart from strings, this would work just as well with arrays. Maybe if a “Length” property wasn’t present, a “Count” property could be used…

He didn’t like the idea, saying that introducing new keywords may break existing code (when recompiling code where someone called a variable “empty”). That could easily be solved by not making the keyword reserved or by having compiler switches depending on what version of the language you wanted to compile, but of course, I agree that it probably wouldn’t be worth the trouble.

In C/C++ I wouldn’t hesitate a second before solving the problem like this:

#define empty(x) (x==null || x->Length==0)

But that little gem were left out of C#, probably for the better, considering all the creative stuff that has been accomplished using #define. (One of the weirdest uses I ever saw was from the Obfuscated C contest where someone had managed to write a program looking, except for the very first code line, like a circle!!! What it did? Computed pi, of course!)

So, were did all this take us? To the conclusion that checking for null is quite a big deal in modern OO languages, I’d say! The fact that the cumbersome nullable object pattern has been invented certainly speaks in favor this conclusion.

During my chat with Mats, when I was recapitulating all this, I happened to type an idea that I at first didn’t think much of, but now it has kept growing on me.

So, how would you, fellow programmers, feel about not having to worry about all this null reference checking when writing your if-statements? How about:

ifnotnull ( strName.Length!=0 )

This specialized if-statement would always be false if we were trying to access a property or method through a null reference! There would be not exceptions involved, just silently expanded code at compile time. For example:

ifnotnull ( a.b.c.d!=0 )

would expand to:

if ( a!=null && a.b!=null && a.b.c!=null && a.b.c.d!=0 )

Wouldn’t that be cool??? You almost start to wonder why it isn’t working that way by default!

(In practice, it wouldn’t be exactly the same thing; since the compiler would certainly be smart enough not to “start from the beginning” each time and ask “a” about “b” over and over for each part of the expression, giving rise to unnecessary method calls.)

Knowing myself, I think that I would start to write “ifnotnull” always, by routine. And seriously, isn’t that almost the same thing as the good old “On Error Resume Next” in plain old VB? Enough said.

Is the idea flawed for the beginning then? No, now comes the part that I (as of this writing) think is really brilliant. What if we gave the if-statement THREE states, instead of just “true” and “false”? The third state would be “null ref”. Well come on, you may say, “true”, “false” and “null” – what’s new in that? I see it everyday in my database, among others…

Well, consider this statement once more:

if ( a.b.c.d!=0 )

If we were to say that if is “true” or “false” if can be evaluated, but it is “null ref” if evaluating it would throw a null reference exception and we explicitly TEST for this condition. Or if we “catch” it, we could say – but then I don’t mean the “normal catch” that is used in conjunction with the try keyword.

I repeat this one more time, since it is important. The “null ref” state does not mean that the expression evaluates to “null”. It means that the expression can not be evaluated, because of a null reference!

So if we wrote:

if ( a.b.c.d!=0 )
  // do something
else
  // do something else
else null ref
  // do yet something else

If the “else null ref” part is missing, then an exception would be thrown, just as usual. This way we explicitly state our intentions to the compiler and we aren’t severely violating any software engineering rules as I see it.

This could be precompiled into the following:

if ( a!=null && a.b!=null && a.b.c!=null )
{
  if ( a.b.c.d!=0 )
    // do something
  else
    // do something else
}
else
  // do yet something else

Once again: this would have nothing to do with exceptions! No exception (due to null references) will be thrown, simply because they won’t happen! The compiler will test for them before they happen. Let’s say that a null reference exception was thrown from within the “a.b” property – then that won’t be “catched” by this new construct, simply because they are two totally different tings!

Being able to stuff together two of the three parts into one would be useful. This takes us dangerously close to “On Error Resume Next” again – not testing for abnormal conditions (if they really ARE abnormal, that can be argued), but I think we can get away with it. 😉

Some other possible language constructs:

if ( a.b.c.d==0 ) || null ref
  // do what should be done when the expression is true
  // or cannot be evaluated due to null references
else
  // false part

As well as:

if ( a.b.c.d!=0 )
  // do what should be done when the expression is true
else || null ref
  // false part or null references

With this extension to the language I don’t have any use for my imaginary “empty” keyword anymore, since I could write:

if ( strName.Length!=0 )
// do what should be need to be done when the string isn't empty
else null ref; // tell the compiler that I'm really aware of what I'm doing!

Another possible syntax could be:

if ( strName.Length!=0 ) && !null ref
  // do what should be need to be done when the string isn't empty

It would surprise me tremendously if this idea hasn’t been thought already. It may even have been implemented in other languages. But that doesn’t matter at all, as long as I can get it in C#!
(Note that I managed to do this without any new keyword! The language syntax graph grew more complex, however.)

To summarize: I think that “three state if-statements” would be a nice addition to just about any OO language. And as always: used correctly it could help writing more elegant and safer code. Used incorrectly, it would do just the opposite.

Posted in Programming | Leave a Comment »

Clarifying

Posted by Dan Byström on November 22, 2004

Jimmy Nilsson posted a blog the other day that I’d like to comment upon.

Do you expect a Volvo car salesman to tell you that a Saab is best for you?

Do you expect your bank’s economical "advisor" to recommend that you invest your money in another bank? Obviously not, yet they are allowed to call themselves "advisors"!

Do you expect the folks at Microsoft to recommend using something that’s not built in-house? It really happens from time to time, but I find it equally surprising each time.

Posted in Uncategorized | Leave a Comment »

Stupid is as stupid does

Posted by Dan Byström on November 10, 2004

…as Forrest Gump’s mother used to say.

Last week a large number of people did something stupid (59,017,382 persons to be exact). Analysts say that it was “moral values” that settled the outcome. To understand this, it may help to recall that “the land of freedom” was built by religious fanatics, outcast from England three hundred years ago. Just slightly more than ten generations ago!

  Morality is simply the attitude we adopt towards people whom we personally dislike.
— Oscar Wilde

I would otherwise have guessed that the main reason for the outcome was that when fearing terrorists, you would probably feel safer when lead by one, just as companies today hire hackers as security guys, I mean.

But a much simpler explanation exists: US Election 2004 Results Listed by Average IQ.

Time for a little quiz. Who has said “The homosexual agenda is the greatest threat to our freedom we face today”?

  1. John Cleese, in the Spanish Inquisition sketch
  2. Adolf Hitler
  3. Usama bin Ladin
  4. Tom Coburn, republican senator of Oklahoma

No prize for guessing the answer, but don’t miss to locate Oklahoma on the average IQ table.

Posted in Uncategorized | Leave a Comment »