Advertise here!

Keep it Simple, Stupid

At my job, I program in Java nearly 100% of the time. It was only recently that I began my own project where I use Ruby on Rails – the original project that I was hired to work on, and that I still help develop, is written fully in Java.

The Ruby on Rails project loads the underlying data into the database by performing a number of transforms on data in another corporate DB that I have no control over. I normalize the data and make it compatible with ActiveRecord.

I created small programs in Java to do the transformations initially, purely because to me everything still looks like a nail. After some time I also did the transforms in Ruby. Here’s a simple example of my continuing education in The Ruby Way, or essentially how dumb I can be when I write Ruby code.

Ranges

In the transform program I typically will load data from the previous day, but may want to load data over any given range of dates. So I created a simple DateRange class to handle this.

public class DateRange {
  private Date start;
  private Date end;

  public DateRange(Date start, Date end) {
    this.start = start;
    this.end = end;
    // omitting the comparsions to make sure start is really the earlier date
  }
  
  public boolean contains(Date check) {
    return (check.after(start) || check.equals(start)) 
      && (check.before(end) || check.equals(end));
  }
}

This code works all right, but it can get ugly to try and specify Dates quickly. So I added more methods to the class, such as:

public static Date date(int month, int day, int year)) {
  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.YEAR, year - 1900);  // since Java Calendar years start at 1900
  cal.set(Calendar.DAY_OF_MONTH, day);
  cal.set(Calendar.MONTH, month - 1); // Since Java months are zero-based
  cal.set(Calendar.HOUR, 0);
  cal.set(Calendar.MINUTE, 0);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);
  return cal.getTime();
}

I’ll spare you the numerous static methods I created to help myself out. Beyond being so verbose, the Java version irks me because I’m adding static methods to a DateRange class to create Date objects easier. The Date class should be doing that… 

Ruby

In Ruby, I was ‘tainted’ by the Java implementation. I started work on my Ruby DateRange…

class DateRange
  def initialize(start, end)
     @start, @end = start, end
     # omitting the comparisons to make sure start is really the earlier date
  end

  def contains?(date)
    date >= @start or date <= @end
  end
end

The seasoned Ruby programmers can all snicker now. Here’s how I used it:

range = DateRange.new(Time.local(4, 1, 2005), Time.local(5, 1, 2005))

I quickly realized I couldn’t easily iterate over the range. I needed to mixin Enumerable and implement an each method!

class DateRange
  include Enumerable
  # ...
  def each
    (@start..@end).each do |date|
      yield date
    end
  end
  # ...
end

If full-out laughter has not erupted yet, you must not program in Ruby. I was very proud of this code for a while, and the transform programs were finished quickly.

About a month later I returned to edit the transforms and create some new ones. I looked at the code and thought something just wasn’t right. Duh! Ranges are embedded in Ruby! I never needed to create my own class.

I quickly refactored all of my code to look like:

range = Time.local(4,1,2005)..Time.local(5,1,2005)

Problem solved, right? Wrong. I broke the each method. Time returns the next millisecond for succ, so when each tries to iterate over the range, it will bring up the next millisecond – I want the next day.

My next brilliant solution:

class Time
  def succ
    Time.at(self.to_i + (24 * 60 * 60))
  end
end

I redefined the succ method to return 24 hours later. So why is this wrong? Because in Ruby there are the classes Time, Date and DateTime - each for different reasons. What I wanted all along was a range of Date objects, not Time objects – thats why I named the first class DateRange to begin with.

So our next try:

range = Date.civil(2005, 4, 1)..Date.civil(2005, 5, 1)

And we’re working again. Date defines succ to return the next day, we never had to create any extra code to handle our date ranges, and we’ve learned a number of very wring ways to solve the problem.

Posted at 10pm on 07/05/05 | Posted in , , | no responses | read on

Smart Parenting

I live in a wonderful city – Rochester, NY. No really, it’s very nice but we have a few big problems. The first is always snow, but the second is our high crime rate in portions of the inner city. When you make a wrong turn in our city… well let’s just say you’ll know it real quick. 

So I was driving home from work the other day and heard a news report and thought I must have hallucinated some of it. Apparently a two year old child was playing alone in a vacant lot at 11:25 p.m. Monday when the parents heard what sounded like a firecracker – only to see their two year old with a gunshot wound.

Now, maybe it’s just me but a few questions come to mind when you hear this story:

1. Why is a 2–year old up at 11:25 p.m.?

2. Why is a 2–year old playing in a vacant lot?

3. Why is a 2–year old allowed to roam unsupervised?

4. Why in god’s name would anyone mistake a gunshot (40 ft away) with a firecracker?

5. Who would shoot a 2–year old?

And after reading the report about the arrest…

6. What jury in their right mind would believe that the man accidentally shot the child because he ‘was wiping the gun down’? Now if I had a gun, I _probably_ wouldn’t wipe it down while it was say… pointed at a child.

Posted at 7pm on 06/30/05 | Posted in , | no responses | read on

The Joys of Being a Corporate Drone

Ah, the simple joys in life as a cubicle member! The large corporation I work for (which shall remain nameless to protect the innocent…er, actually my employment) has given short notice to an entire building of workers that their building is now being ‘decomissioned’ and they must move into the adjoining buildings. And when I say ‘short notice’ I mean there was an email in their inbox this morning and a guy with racks to move their stuff standing outside their office.

So we now have the entire building of bald old white men and all their equipment scrambling around our building dropping off their stuff into empty and intern-occupied cubicles. Ah, to be one step above an intern! They are getting the boot to some other place while I get to keep my cubicle.

The irony of this is that I will probably be getting a new load of computers and monitors to be dumped into my cube, just a week after I angered the furntiure police by secretly moving out all the extra chairs monitors and computers that no one used and took up all my space. (I have a roughly 9’ x 10’ cube, which until last week had 6 chairs, 8 computers and 5 monitors. Yes, I am aware that cattle get more room.) 

Posted at 1pm on 06/30/05 | Posted in | 1 responses | read on

Welcome to the blog!

I think the title is fitting, seeing as how I appear to be the last North American programmer left to start a blog.

While I’m at it, does anyone have a gmail invite?

[Editor’s note: It’s a joke.]

[Editor’s note #2: Although I say it’s a joke, I’m really that uncool – I only got an invite 3 days ago.]

Posted at 12am on 06/30/05 | Posted in | no responses | read on

Older posts: 1 ... 11 12 13