Thursday, December 28, 2006

Coding with the correct Property

Be sure to use the right property when writing your code. I was looking back at one of my programs today trying to debug a process and found a bug.

My code
TimeSpan diff = DateTime.Now - System.IO.File.GetLastWriteTime(FileIn);
if( diff.Seconds < 3 )
{
Thread.Sleep(3000);
}

The quirk here is that if the file was last written 5 days, 4 hours 10 minutes and 1 second ago then this will wait 3 seconds before trying to process the file again.

I should instead have used TotalSeconds.
TimeSpan diff = DateTime.Now - System.IO.File.GetLastWriteTime(FileIn);
if( diff.TotalSeconds < 3 )
{
Thread.Sleep(3000);
}

This will return the total time in seconds. In my situation this wasn't a problem because it would only pause the process for 3 seconds and work correctly. If I had chosen to make the program wait 1 minute though, then everytime the timer fired it would have been less than 3 seconds. Also if I was checking minutes instead. I could have seriously slowed an automated process due simply to a failure to use the correct property.

Wednesday, December 27, 2006

Christmas Geek toys

I had a better Geek toy Christmas this year than I have in the recent past. This year I got a Digital Voice Recorder, a 2 GB USB Drive, and a 1 GB Memory stick for my digital camera.

Also, my kids got a VSmile video game system. It's the first video game system in my house in the past 5 years. I have a somewhat addictive gaming habit and have some pretty bad tendinitis in my right thumb from way too many hours of Nintendo. I played a little today and I started to get that itch. I wonder if there's a cheap Xbox 360 anywhere nearby.

Thursday, December 21, 2006

A Disillusioned Pragmatic Prgrammer

I finished reading The Pragmatic Programmer a few days ago. There were a lot of good ideas in the book, but mostly it can be summed up with continue learning, do things on purpose, and follow good standards. The good standards of course are object oriented programming, decoupling systems as much as possible, using test driven development, the DRY (don't repeat yourself) principle, etc. All of the good standards already existed outside this book, it just points out that a Pragmatic Programmer will do these. Some other tips that are made are as simple as "learn your editor really well". That makes sense. Anyone who wants to be as efficient as possible should take the time to learn the all the options available in their editing tool and the shortcut keys to be able to get more done at crunch time. It's worth the initial investment for the repeated payoff while using your tools.

However, after having completed the book, I was a bit disappointed in the continued support. There is a website http://www.pragmaticprogrammer.com/ the book says to go here for the code samples and up to date links. There are no code samples or up to date links. There are some bloggers listed, but only 2 of them have even written a post within the past month. The main authors of the book have not posted anything in several months. The website is only copyrighted through 2005. All in all the pragmatic programmer support seems to have dissolved. This is very disappointing. There are several good suggestions from the book that I was hoping to have some kind of follow up with. The book give a list of Knowledge Goals

  • Learn at least one new language every year. Different languages solve the same problems in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut. Additionally, learning many languages is far easier now, thanks to the wealth of freely available software on the Internet.
  • Read a technical book each quarter. Bookstores are full of technical books on interesting topics related to your current project. Once you're in the habit, read a book a month. After you've mastered the technologies you're currently using, branch out and study some that don't relate to your project.
  • Read nontechnical books too. It is important to remember that computers are used by people--people whose needs you are trying to satisfy. Don't forget the human side of the equation.
  • Take classes. Look for interesting courses at your local community college or university, or perhaps at the next trade show that comes to town.
  • Participate in local user groups. Don't just go and listen, but actively participate. Isolation can be deadly to your career, find out what people are working on outside of your company.
  • Experiment with different environments. If you've worked only in Windows, play with Unix at home (the freely available Linux is perfect for this). If you've used only make files and an editor, try an IDE, and vice versa.
  • Stay current. Subscribe to trade magazines and other journals. Choose some that cover technology different from that of your current project.
  • Get wired. Want to know the ins and outs of a new language or other technology? Newsgroups are a great way to find out what experiences other people are having with it, the particular jargon they use, and so on. Surf the Web for papers, commercial sites, and any other sources of information you can find.
I thought that list was awesome. So what are the up to date suggestions for new languages with reasons to learn them? Which books are good? Is there anyone out there that's a windows user talking about his own struggles to do these same things? Nope. That is all completely left out of the website. They also link to a wiki, but the wiki too is in poor shape. I'm not up to fixing the Pragmatic Programmer community. I will however continue to do the things that I have, which are most of the suggestions made in the book. I will continue to document my own struggles.

Now to start my next book and start learning a new language. Tcl anyone? Nah, maybe LISP. Oops, I'm busy learning Java as a new language right now, I guess that will have to do and I'll continue to plug along with it.

Tuesday, December 19, 2006

Backup script

I have been using the TiddlyWiki for a while now for keeping track of my notes, todo tasks, and other items. I am slowly migrating most items from here to MonkeyGTD to more closely use the GTD system. However, there is a built-in backup option that will save a copy of the file for every change. It's typical of wiki's to be able to have a per change rollback process and that's awesome, but my needs are different. I want a once a day option. I'm ok if I lose all of my changes for a day, as long as I can get back to where I was at the end of yesterday I'll feel comfortable. However, there is no option for that.

Instead what I did was turn off the backup option in tiddly wiki. It's my biggest annoyance saving the entire html file everytime I make a 1 character change. My file is currently 205 KB, but 160 KB are the javascript that is embedded to make the wiki work, so a majority of each of my backup files were the javascript. I wanted either only the data that I had, or a once a day copy. The javascript is way over my head, and at over 50,000 lines of it, I didn't have the time to learn it all and tweak the backup process. Instead what I did was build my own archive process.

I keep my wiki on a USB drive and when I plug it into my computer it is always drive E. So I built myself a startup.cmd file with the commands.

start backupwiki.vbs
c:
cd \Program Files\Mozilla Firefox
start firefox.exe "e:\my wiki\MyTiddlyWiki.html"

And I build the backupwiki.vbs that creates my backup copy daily of my file, only if I need it.

Dim filename
filename = "MyTiddlyWiki.html"

Dim dtPart
Dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists(filename) Then
Set mainfile = filesys.GetFile(filename)
modifiedDate = mainfile.DateLastModified
dtPart = FormatDate(mainfile.DateLastModified)

If modifiedDate < Date() Then
If Not filesys.FileExists("MyTiddlyWiki." & dtPart & ".html") Then
filesys.CopyFile filename, "MyTiddlyWiki." & dtPart & ".html"
End If
End If

End If

Function FormatDate(dateToFormat)
FormatDate = Year(dateToFormat) & Pd(Month(dateToFormat),2) & Pd(Day(dateToFormat),2)
End Function

Function pd(n, totalDigits)
if totalDigits > len(n) then
pd = String(totalDigits-len(n),"0") & n
else
pd = n
end if
End Function


Now when I am ready, I open my folder and just double click the startup.cmd file and it will open in Firefox for me and create the backup when needed. In about 30 minutes I wrote the scripts that will save me from some annoyances, but more importantly hold me to my daily backup. If I don't use it for a few days, the backup will still be before I make any new changes and won't hurt anything. It's great!

Do you have any repetitive tasks you do that could be automated somehow?

Monday, December 18, 2006

Solve your customer's problems.

It's important when designing a solution that you understand the environment that your users work in. If you don't, then you may use an assumption that causes a problem. This is a true story, not something made up or something that happened years ago, this happened last week.


To reduce the chance of theft gas stations have to drop money into a drop safe frequently. That way if any theft occurs they can only get a max of about $100. To solve this problem the cash registers were programmed to force the cashier to drop money into the safe at $100. However, the starting cash in a drawer is $100. That's what is used to make change. Thus every purchase puts the register over $100 and the only way to take another order is to pull some money out and punch in the amount being dropped into the cash register so it can continue to track it.

The problem is that the developers didn't realize that the cash register started with $100, they expected the register to be empty and when it hit $100 to force a drop. They didn't realize the users neeed to be able to make change to help the customer.

This is not a joke, the problem occurred at a major gas station last week and was not solved yet over the weekend. So if you are in a major gas station and the cashier is cursing and asking you to wait while dropping some more money into the safe, remember, it's because the IT department didn't understand the problem that they were trying to solve.

Wednesday, December 06, 2006

Coming soon.

I have not forgotten my geek posts. I'm extra swamped at work right now with people quitting, and learning a new technology (Java, JSF, Websphere all at once). I haven't had time to write up anything interesting. I do have notes on a few things that I want to post, so be patient and I'll have something soon.