November 28, 2004

.NET memory management (and ghosts from the past)

Channel9 of Microsoft published a two-part interview with Jason Zander, the Product Unit Manager for CLR. The half-hour discussion revolved around most of the possible .NET-related topics, including an interesting and insightful overview of .NET overall structure. Without rehashing everything said on the interview (part 1, part 2), a couple of performance-related things are worth a note.

Deterministic finalization was just quickly mentioned in the video, but the issue was again picked up in the comments. For those of you who don't know, deterministic finalization is the ability to exactly define when the destruction of an object takes place. This feature is missing in .NET (where the garbage collector finalizes the objects at some point of time), something that caused real panic and anger at one time, especially since people with C++ and VB backgrounds were so used to it.


The classic Brian Harry Manifesto on Deterministic Finalization was mentioned again. It had been a couple of years since I last read that - but it's still great. I mean, even if you don't know that much about reference counting or tracing, you will see a host of problems in a totally different light after reading that. But almost as much fun as Brian's posting are the replies to it (follow the link above and keep clicking "Next in topic").

For example, Brian's "We feel that it is very important to solve the cycle problem without forcing programmers to understand, track down and design around these complex data structure problems." got quickly answered with "The game's over if people are being shielded from having to actually design their software". Further down the thread, Microsoft's approach to .NET memory management was labeled as 'hand-holding, design-free "programming"'.

Later on, somebody says "It's downright *laughable* that Microsoft is worried about performance when their target is the internet!" Allright, that's the spot when at the latest you're going to have to smile. All of this took place in October 2000, only a bit more than four years ago. And what do we have now? Two of the foremost modern programming platforms (Java and .NET) are both based on garbage collection with a non-deterministic finalization scheme. Second, the performance and the Internet is an everyday challenge now, one that many people worry about. Not just network throughput, but the scalability of ASP.NET applications, Web Service endpoints and whatnot.

I won't spoil the fun of reading the thread by quoting more - there are some real gems of human arrogance down there. Sure, not everybody will ever pick up .NET or Java, and some programming tasks are still left to be done with C++. But still, how far down the "New patterns suck" road can you go? It's a pleasure to realize how much less tech-oriented programming tasks are today. I mean, creating enterprise business applications is hard enough as it is, even without worrying about cyclic GC issues or C++ style memory management. I'd much rather spend my time doing class diagrams in Visio than crafting elaborate refcounting architectures.

Posted by Jouni Heikniemi at 03:44 PM | Comments (0) | .net

November 25, 2004

You can't just ignore technology you don't like

Well, it's been a while since the last rant-style post, so let me de-pressurize myself some. :-)

Two incidents lately: One web designer half-jokingly stating "Non-IE browsers are for hippies". Another very open source -oriented freelance .NET software developer not remembering the name of Visual Studio and totally dissing the idea of using an IDE with a debugger - plain ASCII text files with Console.WriteLine statements rock! Even though neither of the speakers were 100% serious, both the comments had enough seeds of the true attitude to cause some agitation on my behalf.

If you're a home user of PCs, you can make whatever decisions on technology you want. You can ignore non-IE browsers, you don't really have to care. Want to close your eyes? Go ahead. If you're a corporate user, you may not be able to choose yourself - but rest assured, your IT department have made the choice. Like it or not, you usually have to accept their chosen technology and ignore the others.

On the other hand, if you work in the IT business, you need to think a bit more. If you're a Microsoft evangelist, slandering the Open Source without facts to back you up will no longer be credible. A Linux guru can launch a fiery burst of flame at Microsoft, but unless he can convince the audience with cold hard facts, he's likely to be a prophet only in the eyes of the fanboys - the choir who really doesn't need the preaching anyway.


Categorical statements about tools or technologies usually reveal the lack of knowledge involved. That's bad, because if you're in the business, you're expected to know. You're expected to maintain your expertise so that you'll be able to judge new technologies and products based on their merits. Any opinions, even rash ones, are acceptable when it comes to technology - the only criteria is that you have to have the balls to back them up.

With the rapid clip of development going on in most sectors of IT, it's very easy to close your eyes from the big picture. It's very easy to ignore Linux, Firefox, OpenOffice and other challengers if you work for a Microsoft-only shop like me. It's very easy to dismiss Microsoft's new products as marketing FUD if you're an Linux/PHP enthusiasts. The only problem is that the real clients and users in the world are having none of your nigh-religious fervor. They will still have the freedom to make whatever technology choices they consider fine in their perspective - and they will ask you why your web site doesn't work with their Opera, and what's the business gain in not integrating the code into a Visual Studio Solution.


It's all about credibility as a professional. Refrain from making overly broad statements about things you don't really understand. Nay, not just that. Refrain from being prejudicious. Wipe your head clean of non-factual suspicion. Be open. Learn more every day. Learn more about things you didn't think to be worth learning. Don't think you know everything because you know Linux/Windows/whatever and you're happy with it.

To sum it up: It's almost never a good idea to signal both prejudice and lack of competence, all in a single sentence.


Edit 2004-11-27: The third paragraph ("If you're a...") lost a sentence in my final edit of the post. After that, it made no sense and certainly didn't convey my original thoughts on the issue. Fixed it now; sorry for my carelessness.

Posted by Jouni Heikniemi at 10:12 PM | Comments (0) | General

November 20, 2004

Hex representations for byte arrays and ints

"4A 4F 55 4E 49 20 48 45 49 4B 4E 49 45 4D 49"? Today, among other things, I wrote a C# method that converts byte arrays to their hex representations. That's very simple actually - ToString does most of the grunt work, but some parameterizations help in customizing things.

public static string ToHexString(
  byte[] bytes, bool spacesBetweenBytes, bool upperCase) {

  StringBuilder sb = 
    new StringBuilder(bytes.Length*(spacesBetweenBytes ? 3 : 2));
  string byteFormat = 
    "{0:" + (upperCase ? 'X' : 'x') + "2}" + (spacesBetweenBytes ? " " : "");

  foreach (Byte b in bytes)
    sb.AppendFormat(byteFormat, b);

  // Cut off the last space if we were using spacesBetweenBytes
  if (spacesBetweenBytes && bytes.Length > 0)
    sb.Length--;

  return sb.ToString();
}

If the bytes parameter is an array of bytes 123 and 234, the hex representations by the combinations of the two boolean params are as follows:

ToHexString
w/ {123, 234}
spacesBetweenBytes
false
spacesBetweenBytes
true
upperCase false 7bea 7b ea
upperCase true 7BEA 7B EA

I believe you have no trouble guessing what this overload does:

public static string ToHexString(int num, bool spacesBetweenBytes, bool upperCase) {

  return ToHexString(
    BitConverter.GetBytes(num), 
    spacesBetweenBytes, 
    upperCase
  );
}

Update 2004-11-21 9:00 UTC+2: I made the int version use BitConverter instead of implicitly setting the byte order (endianness). Sorry about the edit; I must've been asleep when writing the entry.

Posted by Jouni Heikniemi at 08:47 PM | Comments (0) | .net

November 17, 2004

SQL Server 2005 and CLR

Another interesting MSDN article has been published. Using CLR Integration in SQL Server 2005 is a broad (and verbose) description of key aspects of CLR/T-SQL integration. Among the questions answered are:

  • Stored procedures: managed code vs. T-SQL?
  • How do I write stored procedures and functions in C# (or VB or whatever)?
  • How to access and return data to the caller from a CLR SP?
  • What are TVFs (Table valued functions)? (in short: functions that return a table-like stream of data)
  • Writing your user-defined aggregation operators (can you say SELECT THIRD_BIGGEST(Age) FROM Person?)
  • How to use custom-defined types (.net structs/classes) as column types?

Grab a cup of coffee (or whichever poison you prefer) and allocate at least half an hour if you intend to read the article in one go. It's heavy but definitely worth it. Some of the examples - such as the RSS reader implemented as a TVF - sound ridiculous at first, but think about it. All of this is going to change the way we program against databases and design our architectures. The start of the revolution is only a year away.

Posted by Jouni Heikniemi at 08:28 PM | Comments (0) | .net

November 15, 2004

REpad is here

I finally finished (yeah right!) my personal Regex testing tool. With REpad, you can easily test both match/capture-type regexps and replacements. And when you've polished your regexp, you can easily do some string conversions (from/to C# strings and regex literals) through the context menu.

If you need this sort of tool, get the binaries or the sources. And once more, feedback is welcome.

Posted by Jouni Heikniemi at 10:54 PM | Comments (0) | .net

November 14, 2004

JHLib is out

I've finally gathered some of the example code I've posted on this blog as well as some other snippets from my code library. They are now available as a free code library, JHLib. The library currently contains the CSV Parser, ProperCase algorithm, HTTP upload code and a Pop3 client with an Rfc822 compliant header parser. Also, there's a demo application for each of those sections.

I'll be adding more code to the library as I have the time (there's still a lot of quick-and-dirty stuff I'd never want to show anybody :-)). You can stay up-to-date with the library changes by reading this blog; I'll be certain to post about any updates.

Posted by Jouni Heikniemi at 12:09 PM | Comments (2) | .net

November 13, 2004

No more target="_blank"

Firefox 1.0 with all its hype is here. Without going into musings about browser markets, let me pick up a single feature that's relatively new in the Firefox family. Go into the advanced configuration in about:config and switch the property called browser.tabs.showSingleWindowModePrefs to true. Now open the Tools/Options/Advanced dialog, and see what you've got:

Now this is what I call fun. Using target="_blank" is a serious pain. If I want something to open in a new window, I will most certainly shift-click or wheel-click myself. I can understand why web authors want to make the choice for me (or more exactly, for the basic user), but it's really irritating. No more: the "Force links that open in new windows to open in a new tab" alternative is exactly what I've always done manually.

Not to bash IE at all, small configurability details like this really make the difference. They tune the experience to my tastes, they make using the browser more natural. While MSFT Australia director Steve Vamos is correct when pointing out "I don't agree that just because a (competing) product has a feature that we don't have, that feature is important" (see the full Cnet article), it's also a question of a feeling. With IE, your customization abilities are fairly limited. The Mozilla family of browsers represents a totally different feeling, and the open source image is just one part of it. For IE development, that's not really a challenge of picking the correct features - that's a much broader challenge for UI design. I hope Microsoft is forced to respond soon - both Microsoft and Mozilla camps could use it.

Cripes, I didn't manage to stick to just presenting the feature. Oh well. :-)

Posted by Jouni Heikniemi at 10:24 AM | Comments (3) |

November 04, 2004

Often overlooked specialized collections

The lost treasures of .net class library, part I: The System.Collections.Specialized namespace has a few excellent classes that can help you (at least until you get generics, that is). Too bad many coders have never even heard of them. Here's a short introduction:

HybridDictionary and ListDictionary are just perf-tweaked Hashtable implementations. Fine, but not earth-shattering. But it gets better: StringCollection is essentially an typed arraylist, or an flexible-size string array, if you will. StringDictionary is an Hashtable with the key strongly typed as a string. NameValueCollection is excellent for configurations and some other situations (by the way, it's actually used for this purpose by .net).

For some do-it-yourself-spirit, there's also NameObjectCollectionBase, a base class for your own string-keyed typed Hashtables. See the help for an example. And last but not least, CollectionsUtil with static method shortcuts for creating case-insensitive SortedLists and Hashtables.

Posted by Jouni Heikniemi at 08:25 PM | Comments (0) | .net