Alieniloquent


Delphi Hint H2455: We Broke Your String Constant

December 13th, 2005

So, Brian and I are going along writing code that was supposed to be easy. Then we run into a problem. Some of the super secure encryption we do to obfuscate some constants in our code wasn’t working right — in .NET. It worked fine in Win32. So, we put on our spelunking hats and settled into our harnesses and went down into the depths of our code.

What did we find? We found hint H2455. It says “Narrowing given wide string constant lost information.” The helpful help says that if it encounters characters with ASCII values above 127 it may yes, I said may, replace them with a ‘?’. How nice of them.

So we try declaring a typed constant as an AnsiString, but no, why would that work? The string literal is still WideString. We look at the assembly and sure enough, the byte 0×97 is getting replaced with 0×3F: ‘?’.

What did we end up doing? We ditched the string constants. We use an array of Byte and we made a function that will make an AnsiString out of it. As a nice conclusion to this little vignette, that array is prettier to look at.

On Measurement

December 12th, 2005

So my boss walks into the room and slaps a print-out on the wall with some tape. It says:

Gilb’s Law: Anything you need to quantify can be measured in some way that is superior to not measuring at all.
— (Tom Gilb)

Kent’s corollary: Any imperfect measurement can be abused during planning and decision-making by assuming a level of precision greater than what the measurement actually delivers.

Now powered by Subversion

October 26th, 2005

I previously blogged about Subversion at my workplace. I promised that I would write a blog post about how the conversion went. Many of you may think that I forgot. I didn’t. We just finished it today, and as I expected, I have things to share.

Saturday evening I kicked off vss2svn after installing all the things I needed to run it. I had done this before on a small repository, but it was long enough ago that I didn’t remember exactly what it was like. So I started it, and disconnected from VNC figuring it would take it a while. It did, indeed, take it a while. In fact, it still hadn’t done a single thing when I came in Monday morning. VSS has this need for a username to be specified. It will use your windows login if it’s a valid VSS user, but if it isn’t, the command line tool will prompt for one. The script ate that prompt, so I didn’t see it. That brings me to the first lesson.

Lesson #1: Login to windows as a user with a VSS account.

Monday, having guessed that was the problem, I fixed that by creating a VSS account for the user I was running as. Problem solved. The script ran and it built its database, and it told us what users we needed to have. I put all of those users in my password file and hit enter. Boom! It blew up. It gave some sort of error about not being able to import. After a good deal of time, it was discerned that the script was not properly forming the import command. It’s a good thing I know Perl.

Lesson #2: Download the perl script, not the executable, you will need to hack the source.

Having fixed the script I launched the process. It ran again. This is the point at which I start getting really annoyed at the fact that the script doesn’t pick things up where it left off. We are dealing with a very large codebase here, so it takes a while to analyze. Thank goodness I was running this on the machine where VSS and SVN are served and not over the network.

So it starts going. It converts. It converts some more. We get through two hours and about 1500 revisions when, yes you guessed it, BOOM! It blows up. This time it is asking for a revision of a file that VSS doesn’t have. This is somewhat odd, because you’d think that’s what version control is for: to keep old revisions. Well, you see, Microsoft decided to add a feature to VSS that would allow you to keep all the revision history but only store the most recent version of the file. So, if you analyze the history to figure out commits and then ask for an old revision…you get nothing. Try to add a file that doesn’t exist to Subversion, and you get an error.

Lesson #3: VSS has a lot of retarded [mis]features.

Ruby to the rescue! We write a script to figure out how many of these files exist. We run it. Wow, that’s a lot of files. We look at all of them, and determine that we don’t care about their revision history anyway. So we’ll just “delete” them all from VSS and then re-add them. We’ve got a command line tool. It can perform both operations. Excellent! We’ll just write another Ruby script. If only things were so simple.

Given that it’s Microsoft — the same company that said nobody would need more than 640k of memory — I don’t find it surprising that our script didn’t work. I think somebody said “nobody will ever want to script VSS.” You see, when you “delete” a file from VSS, you don’t actually delete the file. You simply set the file aside in a pile of files that you don’t play with any more, but it’s still there, even though you don’t play anymore. If you try to add a file with the same name in the VSS GUI, it will say “You used to play with this file, would you like to just play with the file you used to have, or would you rather play with this new file instead?” If you try the same thing using ss.exe (the CLI), it simply says “You already have a file with that name, sucker.” Stupid program.

Lesson #4: ss.exe sucks

Once I readded all of those files by hand, and later discovered that ss.exe put them somewhere else that was nowhere near where we asked it to, I kicked off the script again. It ran, and ran, and ran, and ran. Then it ran and ran and ran and ran. We started it Monday afternoon and it finished Wednesday morning. It did, however, convert successfully.

Lesson #5: 1,000,000ish lines of code and 8ish years of history = 2 days to convert

So, today we fixed up our buildscript (which was easyish) and our cruise control, and we got everybody switched over. Our shop is now powered by Subversion.

Subversion + Apache + Windows

October 20th, 2005

I’ve always set up Subversion and Apache on Unix systems. Heck, aside from work, I generally don’t use Windows. However, at work, it’s all Windows on our production boxes. So, naturally, we needed our version control to run on a Windows box.

I started out reading Brian’s guide to installing everything. Now, I didn’t use his one-click installer, but I followed the rest. Notably, I believe the system account on our server didn’t pay attention to the path I had set system-wide, as it could not load the Subversion modules unless I gave it absolute paths for everything. But I got that working. It was interesting to note that when I had Apache installed, the Subversion installer actually offerred to install and configure the modules for me. I had Subversion access through HTTP.

Since we were going to be keeping our source code in this, I wanted to make sure it was secure. Only authorized people should be able to access the repository. I don’t want to administer a password database, and furthermore nobody wants to remember two passwords, so I wanted to hook into our Windows domain authentication. The way to do this is mod_auth_sspi. I spent about thirty minutes tracking down that module, as apparently the original author no longer publishes it. Furthermore, I could only find the source.

Finding the source was only a third of the battle, though. Getting it built and installed each took another thirty each. The Makefile is not very generic. You have to edit lots of paths to get things to work, and it doesn’t even define $(RC) even though it uses it. But, I finally got it to compile. Then came the adventure of installing it. I copied the module to the right place and added the right line to the config. Apache won’t start. I run it by hand from the command line, and it tells me it can’t find the file, despite it being right there next to all the others it can find just fine. I spent most of thirty minutes googling, and finally realized that it could find my file just fine. It was a DLL that my module loaded it couldn’t find. Namely, msvcr71.dll. I copied that in, and everything worked hunky dory.

My last concern was backing up our repository. Currently we just do a disk backup of our VSS repository. I don’t think we’ve ever had to restore from one of those, and I’m not entirely sure it would work. But, I do know that straight disk copy out of an active Subversion is bad, since it’s database files. So, I go looking about how to back it up. There’s a script that comes in the source tree called hot-backup.py. I installed Python and tried to run it. No luck. It wasn’t written with Windows in mind. I had to spend most of an hour poking and prodding it. It turns out that os.spawnl doesn’t read from the path environment (on purpose) but does not need or want the path to the executable quoted, but os.popen3 does read from the environment and does need and want the path to the executable quoted. Once I got that all figured out, I put it in the scheduler and hopefully it will run tonight during the wee hours backing up my nearly empty repository.

More soon about moving the source over. It’s very exciting.

Subversive Joy

October 20th, 2005

A year and a half ago, when I started working at my company, one of the first things I saw was the version control they used: Visual Source Safe. My immediate reaction was, “Why don’t we ditch this crap and use a real version control tool like Subversion, or at least CVS.” Unsurprisingly, the response was, “We’d love to ditch this crap, but we can’t afford to right now, and we don’t know what we want to switch to anyway.” So began our journey.

The first task set before us was to decide which version control we wanted to switch to. There was Vault, which would have theoretically been a drop-in replacement. I like Eric, but I don’t like the VSS/Vault paradigm. Another contender was StarTeam, since we’re a Delphi shop, but it is expensive. There were a few other contenders as well, but they all had one thing in common: they cost money. I sat in the corner as this list was assembled and kept piping up, “What about Subversion? It’s fast, it’s fully-featured, and it’s free.” It was added to the list, but not as a serious contender for starters.

Then I bought Pragmatic Version Control Using Subversion, and devoured it. I hadn’t even known how cool Subversion was, and now I really wanted to use it. The first thing I did upon completing the book was give it to the senior engineer in charge of deciding what we were going to move to. He worked his way through the book and agreed with me: it was cool. Finally, Subversion won the Jolt award last year, so it was decided. Needless to say, I was excited.

That was months ago. We were in the middle of a big development push and couldn’t afford the time to port everything over. So, it was decided when we released, we’d switch. Well, that time came and went in late August, and we are still on VSS. We have, however, finally been feeling severe pain about merging and various other activities in version control. So, I finally got the go ahead to start working on it. Today I set up our Subversion server and got it ready for prime time usage (pending blog post about how). Tomorrow I will actually move our source tree into Subversion. Next week, we’ll cut over. I am very excited.

PostgreSQL

October 19th, 2005

So I’m working on a website for Erica. She wants some custom coding done and some database goodness, so naturally she asked me to do it.

Being The Ruby Guy, I decided to do it in Rails. I also decided to use MySQL, as it is what I know. Well, I ran into some snags, and I still don’t know what was causing them. Suffice it to say, MySQL was just not working. So, I decided to give PostgreSQL a try.

Now, I’ve always know that PostgreSQL had more features than MySQL. I’ve heard that it’s generally better than MySQL. Yet, I’ve always been afraid to try it. It has been a completely irrational fear, of course, but fear nonetheless. Well, since I found myself needing a different database, I bit the bullet.

It was about damn time. Building PostgreSQL from source was a breeze. I’ve built MySQL from source, and it’s a pain in the ass. Setting up my database was also a breeze. It is also a pain in the ass for MySQL. So PostgreSQL has two counts of not being a a pain in the ass, and MySQL has none.

Then it comes time to think about database users. I’ve always thought it was a little silly that MySQL conflates granting privileges to users with creating users. PostgreSQL doesn’t do that. You create users and then you assign them privileges, as it should be.

The command line shell for PostgreSQL also is easier to explore and learn than the one for MySQL. The built-in help is nice, and the online manual is much easier to read.

So, all told, I have a new favorite database.

Ruby Talk

October 18th, 2005

I know, I know. It’s been a while since I last blogged (9 days, and longer since it was anything of content). I’ve been busy! Every so often I think to myself “Do I want to blog about what I’m doing, or just do it.” Between work, school, home-ownership, and sleeping…there’s not much time to do all the things I find fun. Sadly, blogging seems to fall to the wayside.

But, I am getting better about sharing with other people. For the third time in two months, I gave a talk about Ruby at a user group meeting here in Omaha. This time it was OJUG, and I gave them Jim Weirich’s OSCON 2005 talk, “10 Things Java Programmers Should Know About Ruby” (yay for creative commons). Well, at least I gave a talk using Jim’s slides, as they were just perfect.

I’ve really enjoyed doing this, so I’m going to put this out there. If you are a group in the Omaha area, and would like to have me come talk about Ruby, shoot me an email. I’m cheap. All I need is some free food, and I’m your man.

New Phone Number

October 9th, 2005

Well, we sat down to do some budgeting, and one of the things that got trimmed was our cell-phone service. The timing was very good: our contract just ended last week. So, we no longer have our cell phones. If you had my (or Erica’s) phone number before, just post a comment or send me an email and I’ll let you know what the new number is.

First Lawn Mowing Post!

September 21st, 2005

Well, I was going to do it Saturday morning (really early before it got hot). Then I thought, hey I can do it Thursday after I’m done with class but before I go pick up Erica. Then, tonight, I couldn’t wait. I pulled out the lawn-mower that my co-worker has loaned to me and mowed my front lawn. It’s not that big. It’s actually kind of awkwardly shaped due to the large (12-feet in diameter) evergreen tree taking up much of the area.

It was kind of dark by the time I got mowing, but I think I did a fair job. We’ll see tomorrow, I suppose. I’ll be mowing the back tomorrow afternoon.

Division of Responsibility

September 21st, 2005

So I was looking at a C# class that looked something like this:

class FooFactory
{
  private BazCollection _bazzen;
  private QuxCollection _quxxen;


  // …constructors, other methds, etc…
  public Foo BuildFoo(Bar bar)
  {
    return new Foo(bar, _bazzen, _quxxen.FindQuxForBar(bar));
  }
  // …more stuff…
}

Now that smells to me. Take a moment and see if you can sniff it out. I’ll wait.

That’s right. We’re passing in bar and then also passing in something that we use bar to get. But that’s not all. I didn’t show it in the snippet, but inside Foo the way we use the BazCollection is also indexed by bar. So, we have this object and sometimes it indexes into a collection and sometimes the objects that construct it index into a collection for it. The responsibilities are muddled. Sometimes a Foo indexes in and sometimes it doesn’t.

The power that objects give us is to wrap up data and the responsibilities associated with that data into a nice little bundle. That power doesn’t do us a lot of good, though, if we don’t actually use it. In a case like this the responsibility of which object should be indexing into the collection is spread onto two objects, and only should be in one.

Now, one refactor that we could do would be to just pass the collection in for the Qux as well:

public Foo BuildFoo(Bar bar)
{
  return new Foo(bar, _bazzen, _quxxen);
}

This refactor is not the one I would choose, though, as now it is even more complicated to construct a Foo. Specifically if I want to test my Foo (which I do, of course). The foo is only ever interested in a single Baz and a single Qux, so it’s just extra overhead to have to create a collection for each. That brings me to the next refactoring that I might try:

public Foo BuildFoo(Bar bar)
{
  return new Foo(bar, _bazzen.FindBazForBar(bar), _quxxen.FindQuxForBar(bar));
}

This has the advantage of making Foo’s responsibility very clear. It is meant to bring the bar, baz, and qux together. But, that call-site still stinks. In fact, it reeks more with that original odor. At this point, it would be worthwhile to see what Foo actually needs the Bar for and further factor that out. Maybe we can have something like this:

public Foo BuildFoo(Bar bar)
{
  return new Foo(bar.name, bar.id, _bazzen.FindBazForBar(bar), _quxxen.FindQuxForBar(bar));
}

That way we remove the dependence on Bar completely from the Foo, and push it into the FooFactory. Maybe we will create an object that encapsulates those parameters. In fact, maybe this factory is that object. But one thing is for sure, this code is better separated and easier to test and use than the original.

Layout, design, graphics, photography and text all © 2005-2007 Samuel Tesla unless otherwise noted.

Portions of the site layout use Yahoo! YUI Reset, Fonts & Grids.