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. :-)
The author then goes on to demonstrate a 180 line program that could probably be written in about 10 in Perl.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"
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:
Over: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());
}
}
}
I'm also not keen on using long variables like 'contentCharacterArray' when their scope is only 4 lines.sub cal_as_string {
my $self = shift;
if (open my $in, '<', $self->{input_file}) {
local $/;
my $content = <$in>;
close $in;
return $content;
}
return;
}
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;
}
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).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
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?