fun with Mercurial

As I’ve been working my way through Cocoa Programming, I’ve been checking in my source code, using MacHg and Bitbucket, at the end of every chapter. One nice side-effect of this is that I can easily see my progress in the book. Here’s a screenshot from MacHg:

and here’s one from Bitbucket:

Mercurial

I’ve been doing a bit of work in my spare time to try to learn Objective-C and Cocoa programming for the Mac. I got sidetracked from that a bit today to explore Mercurial. I’ve been meaning to try out some of the newer version control systems, like Git and Mercurial, but just hadn’t gotten around to it. The Mac programming stuff seemed like a good excuse to set up a test environment, and get my “Hello world” exercises under version control.

I set up an account at Bitbucket today, installed MacHg on my MacBook, and started messing around. I had a few false starts, and did a few things wrong, but I think I now have things configured so that I can track my work in a local repository, and push it up to Bitbucket whenever I want.

I was attracted to Bitbucket largely because it’s run by Atlassian. I used JIRA at my last job, and like it a lot. My Bitbucket account was pretty easy to set up. It’s got a nice interface for browsing your source files, and looking at changesets. And it’s got an issue tracker and wiki built in.

And I decided on MacHg on the client side, largely because of some recommendations I saw on Stackoverflow, and a couple of other places. It took me a little while to get used to the interface, but now that I understand it, it’s pretty easy to use.

xcode project template grief

I’m trying to learn a bit of Mac programming right now. I’m using Cocoa Programming: A Quick-Start Guide for Developers, which is turning out to be a pretty good book so far. I just stumbled across an issue with my XCode install though. It’s pretty well explained here and here. Having fixed that problem, I am apparently ready to write a Hello World program. Joy!

I should probably just clear out my “/Developer/Library/Xcode/Project Templates” folder, and maybe a few other folders, and do a clean XCode install. But I know that will take some time, so I’m going to put that off. for now.

mashups

It looks like I will be working on a mobile app (iPhone and Android) at work soon, and I’ve been vaguely interested in mobile development for a while now anyway, so I spent some time this weekend looking around for ideas for a small project to work on, to get my feet wet. I spent some time on Programmable Web, and was interested to see this article wondering why there aren’t any great mashup apps for Arlington National Cemetery. Having recently attended a funeral there, I can see how a good mapping app for the cemetery would be really useful. It’s a huge place. There’s an interesting comment following the article from someone who tried to work with the cemetery to try and get something done, but he didn’t get too far.

found it

Following up on my post from a couple of days back, I found my invalid nulls today. There were null values where there shouldn’t have been on 20 records in a table of 100,000+. I fixed them. Tomorrow, I hope to zero in on whatever program bug is causing them to get in there in the first place.
A couple of quick observations:
First, SQL Profiler is my friend. There wasn’t really any way I could trace what the VB6 program was doing, and it wasn’t putting out enough error info for me to figure it out, but by following a SQL trace, I eventually isolated the problem records.
Second, I discovered Query ExPlus today, which turns out to be a pretty good tool if you need quick access to a SQL query tool on a machine where you can’t actually install the full SQL Management Studio.

how not to do error-handling

Currently working on a bug in an old VB6 system. The only thing we see in the logs is “Invalid use of Null”, which I know if the standard VB6 error you get when you try to access a value without checking to see if it’s null first. Since the system itself hasn’t changed in years, I know that, somewhere, in some table, there’s a null value where there shouldn’t be a null value. But, because the system doesn’t pass through the line # of the error, I can’t see which field it might be looking at. And, because it doesn’t pinpoint which record had the error, I can’t really tell which record it was processing when the error occurred. So, it could be any of about 100 different fields on any of about 100,000 different records. I’ve made an educated guess that it’s probably one of only a few different fields, and I think I’ve narrowed down the possible problem records too, but still, could I please ask anyone out there who may be writing non-trivial systems that may be in use long after they’ve left their company: If you’re going to “handle” exceptions, please do more than just eat them and report the error message! Please, for the love of god, at least report the line #. And, if you’re processing a large data set, give some indication of which record you were processing. Thank you.

PowerShell script to view SMTP server WMI stats

I’ve been playing with PowerShell a bit lately. Here’s a script I wrote today that extracts some info about the standard Windows Server SMTP service, does a little formatting on it, and sends it out to someone via GMail. (I’m sending it via GMail, since the purpose of the script is to determine if there’s anything weird going on with the SMTP service, and if there is, then it doesn’t make sense to use it to send the status e-mail.)

function sendmail
{
    param ($msgtext)
    $EmailFrom = "someone@somewhere.com"
    $EmailTo = "someone@somewhere.com" 
    $Subject = "SMTP Stats" 
    $Body = $msgtext
    $SMTPServer = "smtp.gmail.com" 
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
    $SMTPClient.EnableSsl = $true 
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("somebody@gmail.com", "password"); 
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}

$smtp1 = gwmi Win32_PerfFormattedData_NTFSDRV_SMTPNTFSStoreDriver | ? { $_.Name -eq '_Total' }
$smtp2 = gwmi Win32_PerfFormattedData_SMTPSVC_SMTPServer | ? { $_.Name -eq '_Total' }
$Date = Get-Date
$output = "-----------------------------------------------`n" 
$output += "Stats from " + $smtp1.__SERVER + " on " + $date + "`n"
$output += "----------------------------------------------`n"
$output += "Messages in queue dir: " + $smtp1.Messagesinthequeuedirectory + "`n"
$output += "Remote queue length: " + $smtp2.RemoteQueueLength + "`n"
$output += "Remote retry queue length: " + $smtp2.RemoteRetryQueueLength + "`n"
$output += "`nBadmail:`n"
$output += "`tBadPickupFile: " + $smtp2.BadmailedMessagesBadPickupFile + "`n"
$output += "`tGeneralFailure: " + $smtp2.BadmailedMessagesGeneralFailure + "`n"
$output += "`tHopCountExceeded: " + $smtp2.BadmailedMessagesHopCountExceeded + "`n"
$output += "`tNDRofDSN: " + $smtp2.BadmailedMessagesNDRofDSN + "`n"
$output += "`tNoRecipients: " + $smtp2.BadmailedMessagesNoRecipients + "`n"
$output += "`tTriggeredviaEvent: " + $smtp2.BadmailedMessagesTriggeredviaEvent + "`n"
# $output
sendmail($output)

I still don’t really know PowerShell that well, but I’m learning. I picked up most of the info I needed to write this script from StackOverflow and Hey, Scripting Guy.

fun with stored procedures

I had some fun today optimizing a stored procedure. It was taking about 10 minutes to run, and I got it down to running in two seconds with a pretty minor change.

Without getting into too many gory details, it was basically doing a somewhat complicated PIVOT. It was using a SUM() call to generate a value that it was really just ignoring, just checking to see if it was null or not. I replaced the SUM() with a COUNT(), and checked for zero instead of null, and bam, 10 minutes down to 2 seconds.

This is the kind of stuff I really like doing — figuring out how to make a minor change in a bit of code that doesn’t affect the output, but results in a measurable improvement in performance. The only thing that’s still a little frustrating about this is that I don’t completely understand why the difference is so great. I have a general idea of why this worked, but something weird must be going on internally with the PIVOT option in MS SQL for this to have made such a huge difference.

less than helpful naming conventions

I had to do a little debugging work today in a system that I’m not too familiar with. I came across this line of code:

bool result = processor.Process();

If your code has reached the level of abstraction where the most meaningful name you can give a class is “Processor”, and the most meaningful name you can give the main routine in your class is “Process()”, then you may have gotten a little too abstract.