Truth in Advertising

ziggy on 2003-05-21T20:45:23

This cautionary warning appears in a recent article on O'Reillynet:

This code is presented as an example. Do not use it on data for which you don't have a copy. It hasn't been widely tested. Consult a professionally trained computer scientist or a twelve year old child before attempting anything difficult on your own machine..
Sounds about right to me. :-)


Java

gav on 2003-05-21T21:48:47

The author makes the comment:
If you are a Perl programmer you might scoff that Java is for sissies. With exceptions and strong typing, Java makes you say "please" while Perl makes you say "sorry"
The author then goes on to demonstrate a 180 line program that could probably be written in about 10 in Perl.

I don't scoff that Java is for sissies, but Java is for people who don't mind typing a lot or being an expensive copy and paste machine. Consider:

  private static String representCalendarAsString()
                                 throws CalendarNotFoundException {
    try {
      char[] contentCharacterArray = new char[(int) inputFile.length()];
      calendarReader.read(contentCharacterArray);
      calendarReader.close();
      return new String(contentCharacterArray);
    } catch (IOException e) {
      throw new CalendarNotFoundException(e.getMessage(), e.getCause());
    }
  }
}
Over:
sub cal_as_string {
    my $self = shift;
    if (open my $in, '<', $self->{input_file}) {
        local $/;
        my $content = <$in>;
        close $in;
        return $content;
    }
    return;
}
I'm also not keen on using long variables like 'contentCharacterArray' when their scope is only 4 lines.

Re:Java

Juerd on 2003-05-21T22:11:55

> sub cal_as_string {
> my $self = shift;
> if (open my $in, '{input_file}) {
> local $/;
> my $content = ;
> close $in;
> return $content;
> }
> return;
> }

Or...

sub cal_as_string {
        open my $in, shift->{input_file} or return;
        local $/;
        return readline $in;
}

Java sucks

djberg96 on 2003-05-21T22:37:25

Here's his 47 line ICalendarToString class reduced to an 8 line Ruby class:
class ICal
   CALENDAR_HOME = "Calendar/"

   def self.to_s(calendar_name)
      calendar_name = CALENDAR_HOME + calendar_name + ".ics"
      IO.readlines(calendar_name).join
   end
end
The IO class will even return the appropriate exception and spit the error to the screen (making try/catch/throw a lot of wasted effort).

Except for the JVM, Java has absolutely nothing on Ruby (though that may be a big deal for your shop).

Re:Java sucks

dlc on 2003-05-22T14:20:45

It's even shorter in Python, which will throw exceptions like Ruby:

class ICal:
    CALENDAR_HOME = "Calendar/"

    def to_s(self, calendar_name):
        calendar_name = self.CALENDAR_HOME + calendar_name + '.ics'
        return open(calendar_name).read()

Re:Java sucks

djberg96 on 2003-05-22T23:19:23

Except you cheated slightly by not making to_s() a class method.

Re:Java sucks

dlc on 2003-05-22T23:27:03

Well, python doesn't really have class methods. But this is close enough:

ICal().to_s(filename)

Isn't it?