Friday, October 31, 2003

MONAD - the Next Generation Command Line




I’ve been living on the command line for a while now, mostly in Cygwin’s
Bash shell. I like it, but it’s not perfect. Nothing is, of course, but maybe this will
be a bit closer.  



Monday, October 27, 2003

Sometimes It Makes You Proud to Be an Alumni


This
CNN article
talks about how some MIT students have figured out a a legal way to
stream MP3s and cut the record companies out...fantastic! Just because
I don't like it when people copy music without paying for it doesn't mean that I side
with the RIAA. Quite the opposite. So the fact that they figured out how to have it
both ways makes me happy.

A Retratction

Recently, I mentioned that I thought the Microsoft Application Blocks were not mature. I sited a general documentation bug and a Configuration Management Application Block threading bug as key indicators that the software isn’t ready for prime time. And I said that I hadn’t heard back from them after reporting the bug.

Well, I’m going to eat my words on two of those: the threading bug doesn’t exist, and not only did the Patterns and Practices group get back to me, the email was written before I posted the blog entry…I just hadn’t checked that particular email inbox that day. And the team is aware of the documentation bug and plans to fix it.

For the record, the threading bug I thought I saw was related to a Hashtable maintained in the MemoryCache class. I noticed two things about it: First, that they weren’t using the Synchronized wrapper. Second that even though they were taking a lock on writes, they weren’t locking on reads. This is not, in general, safe to do. In particular, it’s not safe to enumerate over a Hashtable at the same time a write is occurring.

As it turns out, the CMAB implementation is safe because they never enumerate over the collection. And since the Hashtable is a private field, no one else can, either. Not using a Synchronized Hashtable is fine in their case because they do the lock on write (which is all the Synchronized wrapper does anyway). Locks on simple reads through the indexer are not necessary with a Hashtable – only on enumeration.

On top of all this, the response from the Patterns and Practices team was really good. I think I got about four phone calls and twenty or so emails from Microsoft people in response to my feedback. And the dev lead himself walked through the code, took the time to verify that the problem I thought I saw wasn’t there, and let me know…over the weekend. Kudos!

Now I just have to figure out if it’s worth it for me to go in and change my code around to use the Configuration Management Application Block again. I wrote my own version that I’m employing, and of course it’s slightly different, and tailored specifically to my view of the configuration management problem. Microsoft’s stuff is obviously superior in many respects, as it has been more carefully architected and more thoroughly tested…and they’ve clearly got a great team backing it up. The only thing stopping me from leveraging their stuff is that what I’ve got now works. I think. :)

Friday, October 24, 2003

C# 2.0 Spec

Niels reports that the C# 2.0 (Whidbey) spec is now online here. Guess I need to find some time this weekend!

Wednesday, October 22, 2003

Microsoft Application Blocks Not Mature




So, I’ve spent some time over the past couple of weeks looking at Microsoft’s
Application Blocks. If you’re not familiar with these, you should know that
they’re basically software written by the Patterns
& Practices group
that’s aimed at solving common problems. For example,
the Data
Access Application Block
is a set of helper classes aimed at making data access
easier. They’re generally the sort of thing that you write yourself after you’ve
written the same two hundred lines of code three or four times. Only now Microsoft
writes it for you. They even give you the source code.



Unfortunately, as much as I hate to say it, I think these aren’t quite ready
for prime time. I really wanted to like them, not least because I had a nice conversation
with Ron Jacobs from the Patterns & Practices group about them, and he seemed
like a smart, helpful guy. But there are a bunch of little things that just tell me
I’m not likely to get much use from them. Like:



·        The
Data Access Application Block currently only works with SQL Server. You can get bits
from gotdotnet that make it work with Oracle, but it doesn’t do anything to
make the differences between the two databases go away. Not that I necessarily think
they could – but at that point, what value am I getting from the code?



·        The Exception
Management Application Block
only does some of what the Enterprise
Instrumentation Framework
does. Granted, it was written sort of simultaneously
with EIF, but again, where’s the value if there’s something better available?



The really telling ones, though, are things like:



·        The
documentation contains factual errors, claiming at one point that the CLR will load
the assembly with the highest build and revision number where the major and minor
version numbers match the requested version. This was true briefly in one of the betas,
but has never been true in the released CLR.



·        The Configuration
Management Application Block
– which at first looked to be one of the most
useful of the bunch – contains some fairly rookie-type threading bugs.



These last two just kill me. If I’m really going to use this code in my products,
it has to inspire confidence that it was written by someone who knows the problem
domain at least as well as I do. Demonstrating a misunderstanding of the platform
and of basic threading principles don’t achieve that. I emailed the team about
the thread bug, hoping to hear back from them, but it’s been over a week and
nary a word, so here I blog.



That said, I should point out four things. First, that these blocks are the group’s
first attempt, and no one gets anything right on the first try. I expect their next
version will be significantly better. Second, that I didn’t get elbow-deep into
all the blocks, so I might be missing a cool one. Third, that what doesn’t work
for me might work for you. And finally, that the group produces a lot more than just
these blocks, including some fairly decent whitepapers on things like security and team
development with VS.NET
– all stuff I’ve heard my clients beg Microsoft
to do for years. So they’ve moving in the right direction.



Friday, October 17, 2003

New Direct3D Tutorial Available - Dealing With Device Loss

 The Direct3D device can become lost due to a variety of events - screen savers kicking in, computers being locked, or machines going into standby. In this tutorial, we'll see how to gracefully deal with these events.

This Weblog Unavailable Saturday and Sunday

Because of a scheduled outage by the power company that services the DevelopMentor offices in California, this weblog (and all other staff.develop.com resources) will be unavailable from 11PM PST, Saturday, October 18th, 2003 through about noon PST on Sunday the 19th.

Note that this outage will not affect the main DevelopMentor website, nor will it affect the discuss.develop.com mailing lists.

DataSet Computed Columns

My client asked me about a slightly tricky problem the other day. I thought I’d share the answer here, since I thought it was fairly clever.

The problem stems from the fact that they have a database that has a table with columns that can hold values like ‘Y’, ‘N’, ‘0’, or ‘1’ to indicate true/false. Yes, they know this was probably not the best way to do it. No, they can’t change it, because there’s still a ton of existing code that both reads and writes this data. So the question was, “When writing new code, how do I get this to show up as a Boolean value in the DataSet?”

My first thought was to simply take care of the conversion in the SQL. If we knew which database we were using, we could fairly easily write our query to simply turn ‘Y’ and ‘1’ into true, and everything else into false. Unfortunately, the application has to work against both SQL Server and Oracle. While I actually have a way to deal with the differences in SQL between the two (more on that another time), I was still hoping to find some magic in the System.Data stuff that would take care of it for me.

Bob Beauchemin – database guru for DevelopMentor and all-around nice guy – turned me on to a nice way to handle it. He pointed out that the DataSet has the ability to create derived columns, where the value of the new column is computed using the value of existing columns and/or some simple operators. Perfect! A quick test showed that the code for creating my Boolean column looks something like this:

// Retrieves a DataSet from the database that has a Y/N/0/1 column called tfCol
DataSet ds = GetDataSet();

DataColumn dc2 = new DataColumn("derivedCol", typeof(bool), "(tfCol = 'Y') OR (tfCol = '1')");

ds.Tables[0].Columns.Add(dc2);

 

That’s really all there is to it – every row will now have a Boolean column that will be true when the original column is ‘Y’ or ‘1’, and false otherwise.

 

The important part is the expression in the new DataColumn (which I’ve called derivedCol). I’ve bolded the expression. Notice that I have the ability to use comparison (=) and logical (OR) operators. There are a bunch more things you can do, too. They’re listed under the DataColumn.Expression property in the docs.

 

One other cool idea that Bob had was that I could suppress the original column (the one with the Y/N values) from the serialized representation of the DataSet by also setting the ColumnMapping property of the DataColumn to MappingType.Hidden, like so:

 

ds.Tables[0].Columns["tfCol"].ColumnMapping = MappingType.Hidden;

 

If I do this, when serializing the DataSet to XML – as happens when returning it from a WebMethod call – the tfCol column will not be included in the data at all…but my derived column will. Which means that this is a fairly reasonable and easy way to clean up the data before sending it off to the client.

 

Wednesday, October 15, 2003

Web Of Trust - Blog as Authentication

I ran across Thawte’s Web of Trust page the other day. It’s an interesting idea, and appealing to a cheap bastard like me since it’s free. And as email signing has been suggested as a technology that could help combat spam, it appears to be worth looking into.

The part I don’t like, though, is that I have to appear before a Notary in person. See, I feel I have a fairly strong online identity – most people reading this probably feel pretty confident that I am in fact Craig Andera, who works at DevelopMentor and maintains this blog. So I’d like the opportunity to be awarded a cert based on readers of this web log vouching for my identity. Basically, I’m viewing my blog as an authentication mechanism – a sort of voiceprint if you will.

Now, I certainly understand why they haven’t set it up this way – in general blogs are a pretty weak form of authentication, and anyone could claim to be someone else by setting up a weblog. But since I’m lazy and selfish, I just think about my own particular case. The fact that I’m on the develop.com domain and have been writing about generally the same things for months now could in theory act as a reasonable assurance of my identity.

Monday, October 13, 2003

Where are the Good Books?




I don’t claim to have read all the DirectX books on the market. These days there’s
such a flood of material that reading everything on any topic would be challenging.
But my DirectX bookshelf has a fair number of books on it now…and none of them
are very good. Most of them are barely any good at all. The one I’ve read most
recently - The
Microsoft DirectX 9 Programmable Graphics Pipeline
– was no exception. And
I had such high hopes for it while I was checking it out in the bookstore.



The big problem I have with most books (true particularly for DirectX, but true in
general) is that they go too far in one direction or the other. Either they have almost
no information in them, or they assume that you already know the topic. The worst
are introductory texts that take an academic approach. If I wanted an academic approach,
I’d read the docs or the spec. If I want introductory, then all the formalism
gets in the way. I reviewed a book recently that seemed to have exactly this problem
– it was only a step better than a BNF description of the thing it was talking
about. It just used English instead of { | and }.



Ultimately, I want something that augments the specification/documentation, not replaces
it. A good book has to have the primary goal of setting up a functional model in my
head, so I can figure out on my own why something might work the way it does
when I encounter it in the API. It doesn’t even matter if the model is completely
accurate if the text is introductory – a good analogy that I can ditch later
when I have a more complete understanding of the domain is still extremely helpful
even when it isn’t 100% accurate.



This is how I learn, and therefore this is how I try to teach and how I try to write.
Of course, different people learn different ways, but I just don’t believe that
many are well-served by things that read like my college textbooks.



Saturday, October 11, 2003

Joel on Unicode




Joel posts another great one, this time on the
basics of Unicode
. He rightly points out that every developer should know this
stuff.



But you all know this already, because you all read Joel, right? Right!?!? 



Friday, October 10, 2003

The Wedding Toast




I had heard them talking about doing this, but seeing
it
is hilarious.  



If you’re wondering why this guy rates a video from Chris and Don at his wedding…it’s
just because he asked. :)



Wednesday, October 8, 2003

XmlSerializer as XPath

You've probably read a lot by now about the tension between the OO purist view of the world and the XML purist view of the world. You might also have picked up that I personally find myself more at the XML end of the spectrum, due in no small part to the influence of Tim Ewald.

One day not too long ago, Tim rang me up to tell me about a revelation he had had. He used to hate XmlSerializer because it embodies the fallacy that there's an isomorphism between objects and XML. But he explained to me that if you looked at it in a different way - simply as a convenient mechanism for populating a set of variables - that it wasn't so bad. The idea being that in order to code against the XML, you're going to have to find a way to pull the data out and get it into variables. XPath is one way to do that, but so is XmlSerializer. I generally followed his argument at the time, but it didn't really sink in until recently.

I was trying to rewrite some of the configuration code I'd done for one of my clients. The issue was that we have a whole bunch of web projects, and having to make changes in 17 different web.config files was annoying. So I wanted to set it up so we could simply point to a single config file in a well-known directory.

I started off using a modified version of my XmlSerializer configuration section handler. But as I was looking at it, I realized that there was a better way. As it stands, the section handler expected XML in the config file that looks like this:

<configuration>

 

  <!-- configSections element goes here -->  

 

  <MyStuff type="SomeNamespace.MyStuff, CraigsConfig">

    <Foo>1.234</Foo>

Saturday, October 4, 2003

Going to Switzerland

It’s great when your hobbies provide you with nice fringe benefits. For example, it’s no surprise to anyone that reads this blog that I’ve been studying DirectX in my spare time and writing up what I learn in the hopes that it’ll help someone else. Well, Sascha Corti saw my writings and has invited me to speak at DevDays in Zurich, Switzerland. I, of course, accepted.

I’m looking forward to meeting any of you that might be there.

BTW, I’m still working on my Managed Direct3D series, but I’ve been busy and the topic I’m currently writing about (how to deal with rude interruptions like screensavers kicking in) is making me do a bit more research than I had planned. I like to try to know what I’m talking about before I commit the ideas to paper. ;)

Wednesday, October 1, 2003

MIT Open CourseWare: Resource for Super-Villains




So MIT has this cool project where they are trying to make as much as possible of
their course material available, for free, on the web. There are even some videotaped
lectures up there. Find it here. They’ve got
over 500 courses up there now, although the quality and amount of material varies
greatly from course to course.



I was talking to Rob Engberg, a friend of mine, and he pointed out the following sequence
of courses that are available:



3.43J Integrated Microelectronic Devices
Fall 2002



22.312 Engineering of Nuclear Reactors Fall
2002



2.75 Precision Machine Design Fall 2001



6.034 Artificial Intelligence Fall 2002



 



In his words: Yes...Everything a good Super-Villain needs to build an army of giant
robots.



 



:)