On Mon, Jan 21, 2013 at 8:08 PM, Rob Browning <
r...@defaultvalue.org> wrote:
> Avery Pennarun <
apen...@gmail.com> writes:
>> Remember, files sometimes are closed because a variable goes out of
>> scope, not due to an explicit close() call.
>
> ...and as an aside, if we come across any code like that, I'd like to
> change it to explicitly close the files.
Yeah, people keep saying that, but I remain unconvinced. Explicitly
closing your file objects is just like admitting you never really
wanted a garbage collected language anyway :)
Seriously though, writing all your code with the scope of your
variables at the forefront of your thinking makes your code better -
it helps avoid circular references (which are disastrous for any GC),
and encourages writing "right sized" functions: if you're doing
something after a file has been opened and then closed, then your
function is probably too complicated.
Relatedly, keeping object references around when the object is
"closed" just strikes me as icky. The memory doesn't get released,
but the object itself is just hanging around wasting space.
So in the end, it's not explicitly closing files that bugs me... it's
the attitude that leads to *needing* to close the files. :) If you
let people explicitly close the files, it makes it easy for people to
retain that attitude, which makes things worse in more subtle (but
equally bad) ways.
(Aside to the aside: explicitly closing the files probably would not
have helped to avoid the problem with cygwin, because it's just as
likely people would have explicitly closed the file *after* doing the
rename, etc.)
Have fun,
Avery