May 26

In my post about clever code vs. terse code, I showed some distaste for code that attempts to put a bunch of logical statements on one line. I have a particular coding prejudice that lines of code should be short from left-to-right, if at all possible.

The reason? I like sidebar comments.

Maybe this is my showing my age. Back in the day, we didn’t have widescreen MacBook Pro laptops that could display 600 characters across the screen. (And we liked it! While walking uphill both ways in the snow to school…) Between 17″ CRT monitors and the IDE, I was lucky to get 100 characters across.

So instead of writing long lines of code, I prefer to use a couple extra lines and simplify each discrete statement. Then I will sometimes put comments (aligned with each other) out in the right margin that map to my pseudo-code (which is usually just in my head….I don’t actually write much pseudo-code in Ruby).

For example:

1
2
3
4
5
6
7
8
# Spawn a set of threads to process the queue
1.upto(threadcount) do |thread_id|
  threads << Thread.new(thread_id) do |i| # Each thread
    until @queue.empty?() do                    # grabs an item from the queue
      yield(thread_id, @queue.deq())            # and yields it to the block
    end
  end
end

Some people call this “code smell.” The meaning, I guess, is that if I have to write comments that outline what the code does, the code must be too complicated.

I beg to differ.

Imagine for a moment that you are a fresh-out-of-college programmer, and you open up my codebase to maintain it. You find this:

1
2
# Spawn a set of threads and process the queue all on one line
1.upto(threadcount) { |thread_id| threads << Thread.new(thread_id) { |i| yield(thread_id, @queue.deq()) until @queue.empty?  } }

You might run away screaming after a few dozen lines like that. I sure would. Smaller lines with comments make it easier to follow. And I think sticking them out on the right margin where they don’t interrupt the flow of code makes it easier to ignore them if you’re an experienced programmer who doesn’t need them.

May 19

I was paying a parking ticket online to Montgomery County, MD. When the site processed my payment, it submitted to the action shown below. Haha.

I am pretty sure I wouldn’t make that mistake with rails routes.

May 13

I use Mac OS X 10.5.2.

I was in the process of setting up a master repository for the source code to a couple of projects I am working on that I am not hosting on github. Since I work on them from a couple locations, I figured I’d set up my workstation at home as a git “server” (to use the SVN terminology) and then use “git pull” on both that workstation and on my laptop to grab working copies, and keep them in sync.

Hey, git’s made for distributed development. Who says I can’t pretend to be two remote developers?

I set up my “origin” repository like this. All went well until I tried to push my master branch to it.

1
2
3
4
5
6
7
8
9
10
11
myhost: Pete$ mkdir -p /var/git/pete_on_rails.git
myhost: Pete$ cd /var/git/pete_on_rails.git/
myhost: Pete$ git --bare init
Initialized empty Git repository in /private/var/git/pete_on_rails.git/
 
myhost: Pete$ cd ~/workspace/pete_on_rails/
myhost: Pete$git remote add origin ssh://myhost.local/var/git/pete_on_rails.git
myhost: Pete$  git push origin master
Password:
bash: git-receive-pack: command not found
fatal: The remote end hung up unexpectedly

Boom.

Unfortunately, “SSH” isn’t so smart about paths on OSX. It doesn’t read ~/.bash_profile or /etc/bashrc or /etc/profile or /etc/login or ~/.login.

It usually DOES, however, read ~/.bashrc. That’s not where I like to set up my pathing, but adding this to the file fixed the issue:

1
2
# in ~/.bashrc
export PATH=/usr/local/git/bin:$PATH

Here’s what happened after I ran the command:

1
2
3
4
5
6
7
8
myhost: Pete$ git push origin master
Password:
Counting objects: 2663, done.
Compressing objects: 100% (2460/2460), done.
Writing objects: 100% (2663/2663), 5.13 MiB | 2271 KiB/s, done.
Total 2663 (delta 292), reused 0 (delta 0)
To ssh://myhost.local/var/git/pete_on_rails.git
 * [new branch]      master -> master

If you need a hint as to what SSH thinks the path is, take a peek in /etc/ssh/sshd_config.

May 12

Well, it’s happened. The Rails 2.1 RC1 release has been tagged at github. If you haven’t already downloaded it after hearing the news on your iPhone RSS Railscast feed, then head on over to Railscasts.com and watch Ryan’s short video on how to install it.

May 06

Working in telecom, I have to integrate my Rails and plain Ruby apps with Java or Unix based systems running on remote hosts. Most often these systems are running remotely and are accessed via an XML-over-HTTP interface (no SOAP, no REST, blah…).

I’ve found that one of the most infuriating things that UNIX and Java “roll your own web service” writers do is specify “time” fields as “seconds since the epoch”. WORSE: Many Java service writers specify time fields as “milliseconds since the epoch”.

Come on, now, people? It’s a web service! Do you really think I care about accuracy in milliseconds?

I won’t go into a rant about this. Suffice it to say: I Find This BS To Be Lazy. It works that way so that the server software doesn’t have to do any complicated date parsing or conversions.

Fortunately, Ruby makes it easy to deal with if you know the right methods to use. Here’s how to convert to a UNIX or Java internal-time:

1
2
3
4
5
rubytime = Time.new  # => Tue May 06 11:11:05 -0400 2008
 
# .. Convert from Ruby time to Unix time. 
unixtime = rubytime.to_i            # 1210086665
javatime = rubytime.to_i * 1000  # 1210086665000

What about converting the other way? The example above are converting the “less precise” Ruby time (no milliseconds) into equally or more precise time formats. What do you do when you have a “milliseceonds since epoch” time and you want to use it in Ruby, but preserve the sub-second precision?

1
2
3
4
5
6
7
8
9
# Add just over a half second to the time used above
millis   = 1210086665555  
 
# adding .0 preserves subsecond accuracy
precise = Time.at(millis / 1000.0) 
 
precise.to_i            #  1210086665
precise.to_f            #  1210086665.555
precise.to_f * 1000  # 1210086665555.0

Now. Go convince all those legacy Java and Unix service writers to stop specifying “Epoch Time” in their interfaces! It’s Just Plain Lazy!