By Jim, on March 4th, 2011 Life is funny. So are home repair projects. At least that’s what I tell myself. Otherwise I’d just go crazy.
Last weekend I had a friend come over and help me with replacing the … whatever it’s called … that goes on the bottom of the outside door. It’s the thing that closes the gap between the bottom of the door and the threshold. I went to Home Depot and, although I couldn’t find exactly what I wanted, got something close. Mike came over and helped me take the door off (it’s a heavy door and a two-man job). We took off the old strip, screwed the new one on, and re-hung the door.
Satisfied with a job well done, we grabbed a couple of beers and went to sit outside. Debra came out a while later to inform us that there was a “huge gap” under the door. Sure enough, the thing I picked up at Home Depot wasn’t big enough to cover the space between the door and the threshold. Debra picked up a new one at Lowes, so I’ll have to install that one this weekend.
Yesterday afternoon, Debra noticed that one of the valves outside had a pinhole leak. We’ve had trouble with this valve freezing before. It’s on the north side of the house and if we get a few days of freezing temperatures in a row, the valve freezes and cracks. We’ve tried lots of ways to prevent that, none of which have worked well. So one of the things I’ll need to do before next winter is build a box with a little heater (probably a 100 watt light bulb) in it. But that’s next year. The valve needs to be replaced now.
The intent was for me to take the valve off this morning, go get the replacement, and install it. But then my truck started running rough last night when I was on my way home. So the plan this morning was for me to turn off the water, remove the valve, have Debra to follow me to the shop where I’d drop off my truck, and then have me drop her off at work so that I could have the car to go back home and install the new valve. A little wrinkle but simple enough, right?
At first light this morning I walked up to the meter (about 300 feet from the house) and turned off our valve. I went back to the house, turned on the faucet to drain the lines, and … the water kept running. Looks like my water shutoff valve at the street is broken. Okay, so it’s 20+ years old. I’ll have to replace that next. But there’s another valve: the service valve on the other side of the meter (the one that the water company can use to shut off my water and then lock).
So back up to the meter to turn off the service valve. I really need to get one of those keys, because turning the thing with a pair of pliers is a pain. I turned it perpendicular like I’m supposed to, trudged back to the house, turned on the faucet and … the water kept running. Great. I can slow the water flow, but can’t stop it.
With no way to turn off the water, we took the truck to the shop and then headed out to take Debra to work. I called the water company to inform them of the broken valve. They didn’t believe me, but agreed to send somebody out “sometime today.”
Debra works for a pipe company, so I talked to one of the guys in the warehouse. After a bit of discussion I hit on the idea of taking the valve off (with the water still on) and then capping the pipe. Should work, provided I can reduce the water flow enough so that the pressure doesn’t prevent me from putting the cap on.
Back at home I turned off both valves at the meter, attached a hose to the faucet that’s also attached to where the valve is (there’s a “T”), ran the hose to the middle of the yard, and turned on the water. Then, armed with a pipe wrench, I began removing the broken ball valve, knowing full well that I was going to get wet. And I did. Drenched. But I was successful in removing the valve and attaching the cap. So at least now there’s no leak.
What was going to be a simple 1-hour job (including running for parts) turned out to be about three hours, and I’m only half done. Plus, there’s another job I have to do–replace the valve at the meter. On the plus side, if my valve hadn’t gone out I wouldn’t have discovered that the service valve was faulty.
The water company guy called me this afternoon to say that their valve at the meter was indeed broken, and that he’d replaced it. It was nice to know that I’m not totally crazy. And there really was something wrong with the truck. One of the ignition coils went out, so only half of the cylinders were firing. I had them replace both coils, figuring that if one went out after 15 years then the other is likely to go soon, too. Most of the cost is labor, anyway, and most of that is removing all the shrouds and such so you can get to the dang coils.
So, yeah, I laugh. I could get angry or sad, but that wouldn’t help anything. At least laughing means that something good comes from these unexpected events.
By Jim, on March 3rd, 2011 One of the problems with GUIs that provide a “user friendly” interface to a command-line-oriented system is that if the GUI designer decided not to implement a particular feature then you’re left scratching your head.
I created a user-defined table in SQL Server today, and tried to use it from a stored procedure. SQL Server didn’t like that. It said:
The EXECUTE permission was denied on the object 'MyTableType', database 'MyDatabase', schema 'dbo'.
Okay, fine. I’ve been here before with stored procedures. So I pull up the properties for the user-defined table and … there’s no checkbox for the Execute permission. Huh?
Now I know that SQL Server gurus do all their configuration with T-SQL commands. That’s cool. Really. But I’m just a lowly programmer, not a white-coated DBA. I stay as far away from databases as I can most of the time ’cause whenever I mess with them I end up with bites and scars. But we don’t have the luxury of hiring a DBA, so I get to consult Google for how to set permissions.
It turns out not to be too difficult. Just a few minutes’ looking around gave me this command:
grant execute on type::dbo.MyTableType to MyAppName
And now when I look at that object in the GUI, the Execute permission is shown. Again I say, “Huh?”
Moral: You can’t administer SQL Server with just the GUI. You’re going to learn some T-SQL whether you want to or not.
By Jim, on February 21st, 2011 Today I tried to use the Windows FINDSTR command to find occurrences of a particular string in a large (33 gigabyte) text file. Simple enough, right?
findstr /L "xyzzy" bigfile.xml
FINDSTR immediately started giving me errors:
FINDSTR: Line 408555 is too long.
FINDSTR: Line 432128 is too long.
FINDSTR: Line 801201 is too long.
FINDSTR: Line 927897 is too long.
FINDSTR: Line 939189 is too long.
FINDSTR: Line 939189 is too long.
FINDSTR: Line 939189 is too long.
FINDSTR: Line 1006538 is too long
FINDSTR: Line 1579088 is too long.
I couldn’t imagine why it would tell me that the lines are too long. Unfortunately, I’m not able to view the file in-place because after all this time there still isn’t a decent text file viewer that can handle a file that large. I can get kind of close with less, although there are problems displaying non-US character sets in a Windows console program.
In any case, if I extract those lines to a file (by writing a program that scans the big file and pulls out the lines in question), I was able to determine that the shortest of the lines listed above is about 3,500 characters long and the longest is about 25,000 characters. And here’s the kicker: running the same FINDSTR command on that file results in no errors.
I also noticed that FINDSTR told me three different times that line number 939,189 is too long.
Obviously, “line is too long” is a catch-all message for a number of different errors. FINDSTR has some issues. Some time ago, I said that FINDSTR was marginally useful. After today, I’d say it’s even less useful than I thought it was then.
GNU grep for Windows, by the way, has no problems with the file. The only reason I used FINDSTR is because I don’t have GNU grep installed on the server where the file exists.
Oh, and Microsoft still hasn’t fixed that idiotic file caching bug.
By Jim, on February 19th, 2011 Michael Abrash famously said, “The best optimizer is between your ears.” His point is that no amount of machine-specific optimization is going to make a difference in the running time of a bad algorithm. A poorly written Quicksort, for example, will outperform the most highly optimized Bubble sort. Selecting the right algorithm can give you orders of magnitude (or even more!) improvements in performance. Hand optimizing code usually provides small percentage improvements after an efficient algorithm is selected.
In most of what I do, performance is important, but not super-critical. That is, performance is usually “good enough” if I select an efficient algorithm. As such, I don’t worry too much about micro-level optimizations. In this environment, the .NET Framework and the C# language are very nice. I’m incredibly productive with C#. The language and the runtime library give me many tools with which to quickly and (reasonably) efficiently implement my algorithms. I know that I’m paying a performance penalty. C# applications that do a lot of processing are typically around 10% less efficient than programs compiled to native machine code. But that’s okay. Very little of what I do is so performance sensitive that I’m willing to forego the productivity of .NET for the efficiency of native code.
But when performance is critical, I get very annoyed because the C# way of doing things is often very sub-optimal.
I’ve recently been working on a system that finds similar videos based on Web links. That is, if I like a particular video that Alice linked to on her blog, then it’s likely that I’ll also like other videos that Alice linked to. If 10 people linked to the video, then I might be interested in the other videos that those people linked to. So imagine a search facility that, given a search term (say, “bluegrass”), will find all videos that have that term in the title and then does a spreading activation by activating all of the videos that are linked to by people who linked to the videos in the initial result set.
So, searching for “bluegrass” might find 3,000 videos that have the term in their titles. Those 3,000 videos are in turn linked to by a total of, say, 4,000 different people. Combined, those 4,000 people can have many millions of different links, although there will be duplicates (that is, Alice, Bob, and George could all link the same video). We can score a video by relevance and popularity by taking into account the number of links it gets and the relative weight of each referrer’s links. But to do that I have to:
- Create a referrer activation record for each referrer that links to any of the videos in the initial result set.
- Group the referrer activation records by referrer, and compute a referrer score.
- Create a video activation record for each video that the activated referrers link to.
- Group the video activation records by video, and compute the video’s score.
This isn’t difficult to do, even when the scoring calculation is involved. It’s just creating lists, sorting, and then grouping. It’s what we programmers do all too much of every day. And it shouldn’t take very long, either: sorting and grouping tens of millions of items should be a matter of a second or two on modern hardware, even without taking advantage of muliple cores.
My first working cut of this took about 20 seconds, and a relatively few minutes’ time optimizing cut that in half. And then I was stuck. The first three steps happen in well under a second. But no matter what I did, the sorting and grouping portion (step 4) would take almost 10 seconds on a typical query that had about 10 million video references. And that just seemed like way too much time. I tried lots of different things before I got it into my head to try something extreme.
My video reference record was a simple KeyValuePair<int, double>, where the Key is the video id, and the Value is the score from a particular referrer. Those were being placed into a List that I then sorted by video id (Key). The sort was taking most of the time. And that’s just way too much time to sort 10 million items.
In retrospect, I should have thought of this earlier. But things are always obvious after the fact.
You see, I’ve run into this kind of thing before. My sort was written like this:
VideoReferences.Sort((r1, r2) => r1.Key.CompareTo(r2.Key));
I’ll let you look into the details of what exactly that syntax means, but in a nutshell it ends up creating a comparison function that is called through a delegate for each comparison. And, the structures I’m sorting (KeyValuePair) occupy 16 bytes each, meaning that for each swap that occurs during the sort I’m moving 48 bytes around. Like I said, I don’t normally think about this stuff because performance is usually good enough.
But benchmarking results showed conclusively that the sort was taking way more time than I thought it should. So, I thought, what if I could make a list of a native type and sort that? What, indeed.
I changed my reference structure so that it takes only 8 bytes. It now looks like this:
[StructLayout(LayoutKind.Explicit)]
struct VidVote
{
[FieldOffset(0)]
public ulong Combined;
[FieldOffset(0)]
public float VoteScore;
[FieldOffset(4)]
public int VideoId;
}
For those of you who are unfamiliar with C#, that’s the C# way of declaring a union. It packs the integer video id and the floating point score (float is fine here for individual values) into a single 8-byte structure. The Combined field treats the two values as a single unsigned long integer, with the video id in the high 32 bits.
What’s that do for me? Well, I can then write this:
VideoReferences = new List<ulong>(NumberOfReferences);
// ... populate the list with
var v = new VidVote();
v.VideoId = id;
v.VoteScore = score;
VideoReferences.Add(v.Combined);
// and then sort it.
VideoReferences.Sort();
With this structure, Sort uses the default comparison for ulong, which internally does a native 64-bit integer comparison directly rather than calling a comparison delegate.
The result? Sort time went from almost eight seconds to less than two seconds, and the subsequent grouping code is also faster than in the previous version. I’m a little bit annoyed that I had to resort to these bit twiddling tricks, but I can’t complain too much about a 70% decrease in run time.
And the really nice thing is that given this new structure I can see another improvement that should knock another big chunk off the run time.
By Jim, on January 31st, 2011 A few new carvings recently. Debra wanted a new coffee scoop, so I made her two of them.
The scoop is carved from basswood, and finished with something called “butcher block conditioner”–a mix of beeswax and food-grade mineral oil. I figured there wasn’t much sense in using an exotic wood for the scoop, as it’s going to get blackened from the coffee. The bowl holds about a tablespoon. The size comparison object is one of the new Presidential dollar coins.
This little hillbilly (about 3½ inches tall) is carved from a maple scrap. Very hard wood. I think I’ll stick to softer woods for my caricatures in the future and save the harder woods for things like dolphins and other forms that have fewer fine details.

By Jim, on January 26th, 2011 The idea behind data format standards is to ensure that disparate tools can communicate using a common “language.” When the standard includes potential ambiguity, things get messy.
A case in point is JSON (Javascript Object Notation). The standard for escaping characters in strings looks like this:
In English, that says that the only characters that must be escaped are quote (“), backslash¹ (\), and control characters. An escaped character starts with backslash and the following character(s) must be one of those identified. That’s all good except for one item: the “solidus” (/). That character can be escaped, but it does not have to be. So in a string, “/” and “\/” mean the same thing.
Why? The potential ambiguity here is the exception to the rule. Somebody implementing a JSON processor might easily interpret that to mean that the slash (I’m sorry, “solidus”) must be escaped.
¹”solidus”? Learn something new every day. It’s too bad that they didn’t come up with a unique name for “\”. ”Reverse solidus” is harder to say than “backslash.”
By Jim, on January 15th, 2011 Somebody gave me a piece of black walnut that had a dolphin outline drawn on it. She didn’t want to work with the walnut, which is somewhat harder than most carvers are comfortable with. I cut out the pattern at the beginning of December, but didn’t get around to carving on it until last week.

Although the carving is done, I still have a bit of work to do. I need to make a final pass with fine sandpaper, apply the finish (probably an oil/wax mixture), and mount it on the permanent base. That little disk of walnut is just temporary. The glue joint is Elmer’s Glue (the white stuff), so it should come off pretty easily if I soak it in water for a few minutes.
I goofed a bit on the proportions here. The poor dolphin looks like everything behind the dorsal fin has atrophied. The pattern was fine–I just took too much wood off and didn’t maintain the curve of the back end. Live and learn. The next one will be better.
I have three dolphin cutouts cut from mahogany that should be beautiful when I get around to carving them. The largest one is three inches thick and about eight inches long. But I’m going to do a few more small practice pieces from this walnut, and maybe a slightly larger one from basswood or mesquite before I attack the mahogany. I want to make sure I have my proportions right so I don’t goof up the larger pieces.
By Jim, on January 12th, 2011 The HttpListener class in .NET lets you create a lightweight HTTP server without having to go through all the rigmarole¹ of installing and managing IIS. It’s incredibly easy to get a simple HTTP server up and working with HttpListener
But when it comes to handling query parameters, things break in a very strange way. The server I’m working on currently accepts requests to search for strings in video titles. Last night I got a bug report saying that it didn’t work when searching for Kanji (Japanese) characters. David was looking for the string “尺八”², and got no results back even though he knew that there were matching titles in the database.
When a browser sends a query string to the server, it encodes the string using the UTF-8 character encoding. So David’s search for “尺八” resulted in this request to my server: “/?q=%E5%B0%BA%E5%85%AB”. Which is correct.
Then things get weird.
The HttpListenerContext.Request object contains all the information about the request that came to the server. If I look at the relevant properties, I see the following:
Request.RawUrl = "/?q=%E5%B0%BA%E5%85%AB"
Request.Query["q"] = "尺八"
Request.Url = {http://localhost:8080/?q=尺八}
The problem here is that the Request.Query property is apparently interpreting the encoded query string parameter using something other than UTF-8. And, looking at the code for HttpListenerRequest.QueryString (part of the .NET runtime library) confirms that:
public NameValueCollection QueryString
{
get
{
NameValueCollection nvc = new NameValueCollection();
Helpers.FillFromString(nvc, this.Url.Query, true, this.ContentEncoding);
return nvc;
}
}
The problem is the this.ContentEncoding, which says, “use the Request object’s encoding to interpret this string.” That’s pretty strange. It’s hard to be sure, but I think that the current standard (RFC3986) says that query strings should be UTF-8 encoded. If that’s true, then this is a bug in the HttpListenerRequest implementation.
Fortunately, there’s an easy workaround. The Request.Url property is properly formed, so I can use its Query property to construct my own queryString collection and ignore Request.QueryString:
var queryString = HttpUtility.ParseQueryString(context.Request.Url.Query);
string q = queryString["q"];
As far as I know, this is the only way to properly handle encoded query strings in HttpListener. If you know of some way to make Request.QueryString work as expected (or can tell me why the current behavior isn’t a bug), I’d sure like to hear about it.
¹I always pronounced that word “rig-a-ma-role.” But the word is “rig-ma-role”. Learn something new every day.
²WordPress lets you add Unicode characters when adding a new post, but if you pull the post up to edit it afterwards, the Unicode characters get turned into question marks. Also, the “new post” editor accepts Unicode characters directly but any editing done after that requires you to input the characters using HTML Unicode escapes, like <code>尺八</code>.
By Jim, on January 11th, 2011 This dog is carved from a piece of wood that I picked up from the discard pile at the local Woodcraft store. I thought it was red oak, but it might instead be padauk. Or something else. Whatever it is, it was a pleasure to carve. A little on the hard side, but no match for a sharp knife.
Update 01/15: The wood is called Lyptus. It’s a commercial (plantation-grown) hybrid of two types of Eucalyptus tree.
For the eyes, I carved a thin dowel from basswood, rounded the end, cut off a thin slice, and glued it onto the carving.

By Jim, on January 6th, 2011 I’ve said before that regular expressions make my brain hurt. I’ve also been rather outspoken on a number of occasions regarding the misuse of regular expressions. All too often, programmers faced with any kind of parsing problem immediately reach for their regex hammer and then spend an inordinate amount of time trying to use it like a pair of pliers.
That said, regular expressions do have their uses, especially when throwing together a quick and dirty prototype. In my case, I have a list of about 40 million titles I want to search for occurrences of user-input text strings. I’m building a prototype, so I’m not terribly worried at the moment with things like stemming or fuzzy matches. I just want to know how many of the titles in my collection contain the terms “the beatles” or “stairway to heaven”, for example.
I could use naive string searching. That is, just use the built in String.IndexOf method to do a case-insensitive search of each title for each text string. So, for instance, I’d have:
if ((title.IndexOf("the beatles", StringComparison.InvariantCultureIgnoreCase) != -1) ||
(title.IndexOf("stairway to heaven", StringComparison.InvariantCultureIgnoreCase) != -1))
{
// found a match
}
That mostly works, and would probably be “good enough” for most cases. But if I were searching for “the who”, it would match “the whosits”, as well. In addition, if I’m searching for multiple terms, I have to do each search individually.
Regular expressions let me solve both of those problems. I can search for multiple terms with a single call by creating this regular expression:
(the beatles)|(stairway to heaven)
which says, “match ‘the beatles’ or ‘stairway to heaven’”.
That doesn’t solve the “whosits” problem, though. So I need to specify that I want to match only on word boundaries.
In the language of regular expressions, the character escape “\W” says, “match a non-word character.” For simplicity, let’s just say that a “word character” is an alphanumeric character: A through Z, a through z, and 0 through 9. Everything else–white space, punctuation, and special characters–are “non-word characters”. So my regular expression becomes:
\W((the beatles)|(stairway to heaven))\W
which you can read as, “match a non-word character, followed by ‘the beatles’ or ‘stairway to heaven’, followed by a non-word character.”
All well and good, right? Not quite. It won’t match if the string is found at the beginning or end of the title. So you’d get a match if the title was, “Nowhere Man by The Beatles (1965)”, but it won’t match “The Beatles – Nowhere Man”.
That’s easily fixed. We can say, “match at the beginning of the string OR a non-word character,” and something similar for the end:
(^|\W)((the beatles)|(stairway to heaven))(\W|$)
The character “^” is regex-ese for “match at the beginning of the string,” and “$” is for matching at the end of the string.
That regular expression works. Slowly. It takes about 90 seconds to search 10 million titles.
It turns out that there’s another way to search only on word boundaries. The metasequence “\b” says, “match a word boundary.” Note that this is a metasequence rather than a character escape. In a sense, “\b” matches the transitions between words and non-words. My regular expression above can be rewritten as:
\b((the beatles)|(stairway to heaven))\b
That’s right, the beginning of the string is one of those word/non-word transitions, as is the end of the string.
In .NET, the new regular expression produces exactly the same result as the old one. The difference is in how long it takes: 15 seconds rather than 90 seconds. That’s six times as fast!
Moral of the story: avoid alternation in regular expressions whenever possible.
By the way, “\b” doesn’t work the same in all regex flavors. In PHP, Ruby, and Java, “\b” only works right when applied to ASCII characters. Multi-byte (i.e. Unicode) encodings cause “\b” to break.
Regular expressions still make my brain hurt.
By Jim, on December 23rd, 2010 I just had to do one more snowman. This piece is carved from pear wood and stands about 3-1/4 inches tall. The arms are random twigs from the yard and the pipe is carved from mesquite. The pipe is slightly too large for the snowman, but I’m not sure I could carve it any smaller. As with the last one, the rest of the decorations are chips of black walnut.

I need to be a little more careful with the glue. I managed to smear glue around the nose and mouth, and it’s visible under bright light. That wood glue is good stuff, but it doesn’t clean up very well. The only way I know of to remove it would be to carve or sand it away. Live and learn.
Off to other things, but I’ll definitely come back to the snowmen. They’re fun.
By Jim, on December 19th, 2010 While sitting out on the porch today overseeing a batch of ranchero sauce cooking on the gas burner, I whittled this little snowman (about 3 inches tall) from a sycamore branch.

The nose is actually a dark spot in the wood, which just happened to be in a fortuitous spot. The eyes, mouth, and buttons are little chips of walnut that I glued on. The arms are twigs from a juniper bush.
By Jim, on December 16th, 2010 I was doodling on a piece of maple over the weekend and ended up with a goofy snowman. The nose is the end of a toothpick. Goofy, sure, but it has a certain charm.

By Jim, on December 10th, 2010 How constant are constants?
I got a rude surprise today when I discovered that constants in C# (.NET programs in general) are a little more constant than I thought. I know that sounds strange, but let me explain.
Here’s a C# class that defines two constants and a method that outputs those constants.
using System;
namespace OtherTest
{
public static class TestObj
{
public const int ConstInt = 42;
public const string ConstString = "First test";
public static void ShowConstants()
{
Console.WriteLine("TestObj.ShowConstants");
Console.WriteLine("ConstInt = {0}", ConstInt);
Console.WriteLine("ConstString = {0}", ConstString);
}
}
}
We can compile that into OtherTest.dll with a simple command line:
csc /t:library OtherTest.cs
If you’re having trouble running the compiler, go to your All Programs menu and select Visual Studio|Visual Studio Tools|Visual Studio Command Prompt. Then you should be able to run the command line compiler.
Okay so far. Here’s some code that references the constants in that class:
using System;
using OtherTest;
namespace testo
{
class Program
{
static void Main()
{
Console.WriteLine("Inside Program.Main");
Console.WriteLine("ConstInt = {0}", TestObj.ConstInt);
Console.WriteLine("ConstString = {0}", TestObj.ConstString);
TestObj.ShowConstants();
}
}
}
Compile that program and link with the OtherTest assembly with this command line:
csc /t:exe /reference:OtherTest.dll ConstTest.cs
That creates ConstTest.exe which, when run, provides this output:
C:\DevWork\ConstTest>ConstTest
Inside Program.Main
ConstInt = 42
ConstString = Firsttest
TestObj.ShowConstants
ConstInt = 42
ConstString = Firsttest
That’s exactly what we expect. Now, change the values of the constants in OtherTest.cs:
public const int ConstInt = 99604;
public const string ConstString = "Second test";
And re-compile the assembly:
csc /t:library OtherTest.cs
And run the ConstTest.exe program again:
C:\DevWork\ConstTest>ConstTest.exe
Inside Program.Main
ConstInt = 42
ConstString = Firsttest
TestObj.ShowConstants
ConstInt = 99604
ConstString = Second test
If you’re as surprised as I am, raise your hand. The constant that the main program sees is different from the constant that is defined in the external assembly.
I understand what’s going on, but I’m pretty surprised by it. The compiler, when it compiled ConstTest.cs, reached into OtherTest.cs, got the values of the constants, and included them as constants in the compiled code. So when the code references TestObj.ConstInt at runtime, it’s really just getting the constant 42. That all makes sense. Except, of course, when you change the constant in the OtherTest.cs assembly, re-compile, and the values don’t match.
What surprises me is that the C# compiler (CSC) hoists the constants from the assembly. After all, the compiler is just creating MSIL (intermediate code) that is later compiled to native code by the JIT compiler at runtime. I would have expected CSC to write MSIL that says, in effect, “Use the constant value defined in that other assembly here,” and let the JIT compiler figure out how to optimize everything. That would eliminate the confusion you see above, and I don’t understand why it’s not done that way.
The thing to remember here is that constants are evaluated at compile time. I don’t know all the ramifications of this, but one thing is for certain: if your program references constants that are defined in an external assembly, then your program is at risk any time that assembly is changed. Unless you make the rule that a publicly visible constant never changes value throughout the life of the source code, you’re running the risk of encountering a very difficult to find bug that will “go away” when you re-compile the project. And there’s nothing worse than bugs that just “go away.”
By Jim, on December 10th, 2010 This is a set of earrings I carved from basswood. They’re actually very light–lighter than most gold earrings. People like the pinecones, but they’re kind of tedious to carve.

By Jim, on December 9th, 2010 I started carving on this pumpkin patch back in October after I completed the third of my scarecrow figures. Then I got bogged down with other things, or perhaps I was afraid to put this thing together. If I didn’t try, then I couldn’t screw it up. I did make some mistakes, but nothing fatal.
This is what I call a scrap pile project. The pumpkins are carved from small bits of basswood that were left over from cutting out blanks for other projects. The hay bales are cut from a piece of 1″ x 1″ maple that I found in the scrap box. The scarecrow post is a piece of black walnut left over from the little whale I carved over a year ago. The fence is oak from the whiskey barrels. And the scarecrow and the base are from a chunk of scrap lumber I took off a pallet last year. Amazing what a little sandpaper and paint can do, huh?

By Jim, on December 7th, 2010 In a 2007 study, researchers from the California Institute of Technology and Stanford’s business school found that people enjoy more expensive wine more than cheaper wine–even if the two wines are the same. Article. If you think the wine is more expensive, you like it more.
A separate study showed that test subjects rated a wine higher if they were told beforehand that a wine expert had rated it highly. Those who were told that the wine was rated as “average” rated the wine considerably worse. Both groups rated the wine differently from those who weren’t given any advance information about the wine’s rating.
In Is Bottled Water Better Than Tap?, John Stossel describes how the 20/20 team took five different national brand bottled waters and a sample of New York City tap water to a microbiologist for testing. The microbiologist could find no significant differences among the waters. The idea that bottled water is safer than tap water is just flat wrong.
But does bottled water taste better? 20/20 ran a blind taste test that included five bottled waters and that good old New York City tap water. Many of the participants were bottled water drinkers who were convinced that they could tell the difference between bottled water and tap water. They couldn’t. Tap water tied for third in the taste test. The cheapest bottled water (Kmart’s American Fare) came in first. The most expensive water, Evian, was rated as “bad” by almost half of the testers.
Do a little searching and you can find similar tests by the truckload. Dedicated Bud Light drinkers can’t tell the difference if you replace their beer with Coors Light. If you slap an expensive label on a bottle of cheap wine, the snobbiest wine snob you know will be hoodwinked, praising the wine and your good taste. Fill an Aquafina bottle with tap water and the most tap-averse of your friends will gladly swig it down. Wrap a block of store brand cheddar cheese in a label from some artisan cheese place, put the Ritz crackers in a fancy package, and nobody at your hoity-toity wine-and-cheese party will know the difference.
Face it, Coke, Pepsi, and the store-bought cola all taste like battery acid. In a blind taste test, almost nobody can tell the difference.
Store brand canned goods are often packaged in the same plant as national brands. The only difference is the label that gets stuck to the can at the end of the line. The contents are no different.
Of course, you can tell people this all you like. Hell, most of your friends will agree. Except, of course, they can tell the difference. Sure. And everybody in the country thinks he’s an excellent driver.
The truth is that the vast majority of people (and that almost certainly includes you and me) can’t tell the difference between the $5 chardonnay and the $50 chardonnay, whether you’re drinking tap water or Dasani, or whether the eggs are organic, free-range, or factory-farmed.
Face it, the reason you can’t tell any difference is because there isn’t a significant difference. Get over it. Or at least admit that your preferences are based on marketing hype, snobbishness, or something other than actual taste.
By Jim, on December 3rd, 2010 Debra and I went up north again over Thanksgiving, spending four days and nights at a ranch in Ranger, TX with our friends Mike and Kristi. Mike spent part of each day sitting in a deer blind with his rifle, hoping to get some fresh venison. One afternoon I was asking him about his rifle, which he said is a “thirty-aught-six,” and we got to discussing the different types and calibers of rifles.
Understand, I’m not completely unfamiliar with firearms. I consider myself more knowledgeable than most in that I qualified Expert with handgun and rifle, can break down, clean, and re-assemble a weapon, and have a working knowledge of guns in general and a healthy regard for gun safety. But I’m certainly no expert or walking encyclopedia on the topic. In particular, I find the whole issue of caliber to be incredibly confusing. I expect things to make sense, and if you look only at the caliber, things make no sense at all.
We’ll start with Mike’s “thirty-aught-six,” more commonly written as “.30-06.” The “.30″ part means that it’s a .30 caliber bullet. That is, the bullet is .30 inches in diameter. The “-06″ in the designation stands for the year 1906, which was the year that the United States Army started using it. The designation covers more than the bullet. The Springfield .30-06 is 7.62 mm (diameter) x 63 mm (length) cartridge that has a specific shape and load (amount of powder). This became the standard round for the U.S. Army and was used for almost 50 years, including in the WWI era Springfield rifle and the WWII era M1.
So the .30-06 is a .30 caliber rifle. How does that differ, for example, from a .30-30? A .30-30, as you would expect, is a .30 caliber round, but what does the “-30″ stand for? It’s not the year in which the round was introduced, but rather the size of the load: 30 grains (about 1.9 grams) of smokeless powder. Winchester introduced the cartridge in 1895. It’s a 7.62 mm x 51 mm cartridge, as opposed to the -06′s 7.62 x 63.
And then there’s the .308 Winchester, which is also 7.62 x 51, and is very similar to the 7.62 x 51 NATO cartridge introduced in the 1950s and used in the M14 rifle and M60 machine gun, among others.
Remember I said before that a .30 caliber bullet is .30 inches in diameter? That’s not entirely true. The .30-06, for example, has a bullet diameter of 0.308 inches, as do the .30-30 and the .308 Winchester. The 7.62 x 51 NATO, though, has a bullet diameter of 0.300 inches. The .303 British is 0.311 inches, as is the .303 Savage. With the exception of the NATO cartridge, which really does have a bullet diameter of 7.62 mm, the rest of them are confusing as heck. Why a .308 would be 0.308 inches but a .303 would be 0.311 inches is beyond me. I suspect that the measurement is actually the diameter of the rifle barrel and depends on how the measurement is taken (i.e. between the grooves, between the lands, or maybe from one groove to the opposing land).
It doesn’t get much better with handguns, by the way. For example, I always wondered why a .357 Magnum was more powerful than a .38. I’m not talking a little bit, either. A .38 is a dangerous weapon, no doubt, but it pales in comparison with the stopping power of a .357. Why is that? After all, isn’t the .38 bigger?
Actually, the .38 Special has a bullet diameter of 0.357 inches. Why is it called a .38? I don’t know, but the neck diameter of the cartriged is 0.379 inches. The .357 Magnum bullet is also 0.357 inches in diameter. The .357 Magnum has a heavier bullet and a bigger load (i.e. more powder), making it a much more powerful weapon.
A 9 mm handgun, by the way, has a bullet diameter of 0.356 inches. So a 9 mm is pretty much the same thing as a .38, and there are 9 mm Magnum cartridges that are comparable to the .357 Magnum cartridges.
Face it, the world of guns and ammunition is seriously confusing. And for the most part the caliber doesn’t particularly matter. For example, the M16 rifle uses a 5.56 mm round (.224 inches)–the same size as the .22 long rifle bullet fired by popular .22 caliber rifles and pistols that are used for small game hunting and target practice. The difference is that the 5.56 bullet is larger, heavier, and has a whole lot more power behind it.
A fatter bullet isn’t necessarily “better” when it comes to stopping power. What matters is the amount of kinetic energy, which is one-half the mass multiplied by the square of the velocity. So if you double the bullet’s speed, it has four times the kinetic energy. Faster is better than fatter.
I don’t know that my newfound knowledge is particularly useful. There’s still a lot I don’t know, and I’m okay with that. I’m just happy that I won’t be totally bewildered the next time I get into a discussion of rifles and calibers.
By Jim, on November 23rd, 2010 I went out this morning intending to ride between 30 and 40 miles. At about 8 miles I ran over a piece of glass, which somehow made it through my Gatorskin tire and punctured the tube. Disappointing, but it doesn’t happen too often. My last flat was back in July.
A flat tire isn’t normally a big deal. I typically carry at least one spare tube, plus a patch kit. I hate being stranded. This morning, though, it was a problem. You see, the first spare I pulled out was a new tube fresh out of the box. Well, “fresh” in that I hadn’t used it yet. It’s been in my seat bag for the last four months. It wouldn’t hold air.
Undeterred, I grabbed the second spare that was in the seat bag. That one was just stuffed into the bag, kind of like how I’d stuff a punctured tube in the bag after changing it out. But a quick test showed that it would hold air, so I installed it, pumped up the tire, put my bike back together, and continued my ride.
For about two miles.
It turns out that the tube I installed is the one that punctured back in July. It had a slow leak. When I got home that day, I just put the new tube in the bag and forgot about fixing the old one.
I rummaged through the seat bag, pulled out the patch kit and the tube that was punctured a few moments before, found the hole, roughed up the rubber, opened the tube of vulcanizing compound and … nothing. Damn! That stuff has a tendency to dry up and it’d been in the seat bag all summer. So I’m left with patches but no vulcanizing compound. I considered trying to slap a patch on, depending on just the glue to hold it, but decided against the experiment. Instead, I pumped up the tire (the one with the slow leak) and headed for home.
It’s kind of frustrating, having to stop every mile and a half in order to add more air. But it beats walking barefoot, pushing the bike. Walking any distance in those cycling shoes is not an option.
So my “30 or 40 mile” ride turned into 19 miles, more than half of which was done on a leaky back tire.
I obviously need to pay more attention to my emergency supplies. I get flats so rarely that I’ve become complacent. What was an inconvenience this morning could have been a very uncomfortable walk home.
I’ve been riding in the middle of the day the last few weeks, and often “in town” rather than out on the county roads. I forgot how aggressive drivers can be. Every ride in town includes incidents of drivers purposely cutting me off (typically to make a right turn), or failing to see me even though I’m wearing very bright colors. I think I’ll re-think the idea of riding in town. It’s too dangerous.
The prevalence of stop signs and traffic lights is also very frustrating. Out of town, even the major roads will have maybe one traffic light every few miles, and the county roads have no lights and very few stop signs. There are places where I can ride more than 10 miles without seeing a car. It’s a much nicer environment for riding.
I’m closing in on my 20,000 mile goal. After today’s ride, the computer reads 19,460. I had hoped to have 19,500 before Thanksgiving because I’ll be out of town over the weekend. That would give me just shy of five weeks to ride 500 miles: easily doable. I’ll get a ride in tomorrow, but I’ll probably be 10 miles or so shy of 19,500. Provided the weather doesn’t turn too terrible in December, I’ll be at 20K before the new year.
And then I’ll back off on the bicycling for a bit. I need to get into the gym and build a little upper body strength. Contrary to what many think, bicycling does require some strength in the arms and the core muscles. I experienced some lower back pain during longer rides this year, most likely because I haven’t been doing enough sit-ups and back extensions. I’m planning some much longer rides for next year, so it’s time to build up those muscles.
The past two weekends, I attended free training workouts with Austin Cycle Camp. The first weekend was a speed workout combined with some cornering drills. This past weekend we did hills, working on maintaining a steady cadence and also on form. I picked up some good tips, and hope to engage their coaching services next year when I start training for my big rides. I’d like to get my century time down to under six hours. That shouldn’t be too tough. The harder one will be a 12-hour double century. Yeah, I’m crazy like that.
By Jim, on November 12th, 2010 I’m developing a generic Range class in C# so that I can create comparison ranges. The idea is that I can replace code like this:
int MinValue = 22;
int MaxValue = 42;
...
if (val >= 22 && val < 42)
{
}
with this:
Range<int> MyRange = new BoundedRange(
22, RangeBoundType.Inclusive,
42, RangeBoundType.Exclusive);
...
if (MyRange.Contains(val))
{
}
In isolation it doesn’t look like a win but if you’re doing that range comparison in multiple places or working with multiple ranges, some of which can have an open-ended upper or lower bound, or bounds that can be inclusive or exclusive, working with a Range type can save a lot of trouble.
Before we go on, let me introduce a little bit of notation.
First, the characters ‘[' and ']‘ mean “inclusive”, and the characters ‘(‘ and ‘)’ mean “exclusive.” So, assuming we’re working with integral values, the range [1,30) means that the numbers 1 through 29 are in the range. Similarly, (1, 30] means that the numbers 2 through 30 are in the range.
To specify “unboundedness,” I use the character ‘*’. So the range [*,255] means all numbers less than or equal to 255. And, of course, the range [*,*] encompasses the entire range of values for a particular type. By the way, inclusive and exclusive are irrelevant when unboundedness is specified. That is, [*,255] and (*,255] are identical ranges.
A Range type also allows me to do things like compare ranges to see if they overlap, if one contains the other, etc. And therein lies a problem. Consider the two byte ranges [0,*] and [*,255].
It should be clear that, since the range of a byte is 0 through 255 (inclusive), the two ranges are identical. The first range has a lower bound of 0, inclusive, and is unbounded on the upper end. The second range has defined bounds of 0 and 255. As programmers, we can tell by inspection that the two ranges are identical.
Unfortunately, there’s no way to write generic code that can determine that the ranges are equivalent. Although the primitive types available in C# all have defined maximum and minimum values (Int32.MaxValue and Int32.MinValue, for example), not all types do. And since I might want to have ranges of strings or other types that don’t have defined bounds, there’s no general way to tell the difference between a range that has no defined upper bound and a range whose upper bound is the maximum possible value for that type. If I wanted that ability, the only way would be to pass maximum and minimum possible values for each type to the Range constructor–something that I’m not willing to do and might not even be possible. What’s the maximum value for a string?
The inability to determine that an unbounded range is equivalent to a bounded range makes for some interesting problems. For example, consider the two byte ranges [1,*] and [0,255]. It’s obvious to programmers that the second range contains the first range. But how would you write a function to determine that?
public bool Contains(Range<T> other)
{
// returns true if the current range contains the other range
}
Since there’s no way to convert that unbounded value into a number, the code can only assume that the upper bound of the first range is larger than the upper bound of the second range. Therefore, Contains will return false.
Whether that’s an error is a matter of opinion. Logically, having Contains return false in this case is the correct thing to do because “unbounded” is larger than any number. But it seems like an error in the context of types like byte that have limited ranges, because there’s really no such thing as an unbounded byte. Rather, byte has a maximum value of 255 and one would expect the code to know that.
As I said, it’d be possible to extend the Range class so that it knows the minimum and maximum values for the primitive types, and give clients the ability to add that information for their own types if they want. That way, an unbounded upper value for an integer would get Int32.MaxValue. That way, the code would “do the right thing” for the built-in types, and clients could define minimum and maximum values for their own types if they wanted to.
But I wonder if it’s worth going to all that trouble. Is it more sensible to explain the rules to programmers and expect them to define specific bounds (i.e. not use unbounded) if they think they’ll be working with ranges (as opposed to comparing values against ranges) of primitive types?
|
|
|