David Masover
6/8/2008 7:13:00 AM
On Sunday 08 June 2008 01:41:31 Artie Ziff wrote:
> Is it not good programming practice to check if resource exists before
> using?
Depends. I tend to develop Ruby a bit more optimistically -- I try to use a
resource, assuming that if it doesn't exist, some sort of "NoSuchResource"
exception will be thrown. If it turns out that this is something that will
happen often, and the user might know something about it, I'll catch the
exception and provide a more helpful error message.
I think that's the whole point of exceptions -- most application code should
not be about error handling. We shouldn't have to do this:
if ok_to_do_something?
do_something
return true
else
return nil
end
# Repeat, say, five times in a single function.
# And, of course, in the caller...
if some_function_that_does_something.nil?
error "Can't do something."
exit 1
end
# That's unrealistic. Figure it's more like five levels deep.
> > It might be nice to have a user-friendly option about this,
>
> IMHO, it would be user-friendly if software emits error when required
> resource not available (and make program barf).
Generally, yes, and that's what I meant. It takes a bit more effort to say
something like "Record not found" than to say "You tried to call method :foo
on NilClass", but if it's an error that's likely to happen, it's worth that
effort, and your users will thank you.
But not having a tempdir is just a whole new level of broken-ness. I'm having
trouble coming up with a properly absurd analogy, because of how absurd that
situation is in the first place.
> Is it general convention/assumption by all Ruby programmers: a Ruby
> program always has valid writable Dir.tmpdir defined, without verification?
I can't speak for others, but I certainly would, mostly because I write Ruby
apps that are meant to run on more or less complete Unices. I know of no
modern Unix that lacks a tempdir. Actually, I know of no modern Unix that
lacks a directory called /tmp, with mode 1777. And I know of no modern Win32
implementation that lacks a tempdir of some kind -- I don't know the details
of where that is (maybe a %TEMP% environment variable?), but that's also
something that should be Ruby's responsibility to work out, not my app's.
My comment about "more problems than that one installer" wasn't about Ruby, it
was about the rest of your Unix (Linux?) system. You probably couldn't even
bring up a GUI without it -- I see a socket called /tmp/.X11-unix/X0, and
while I don't know for certain, I strongly suspect that this is needed to
communicate with my X server (as TCP is disabled).
If you're moving beyond Unices (and Windows), you're probably going to have to
make more extensive changes than tempdirs -- and you might not end up using
Ruby at all (though not, I hope, because of a lack of tempdirs).