3 ms·
The argument seems logical as far as it goes. But having a piece of code which may or not run seems to introduce inherently greater mental complications into th
by joe_the_user 13y ago
The argument seems logical as far as it goes. But having a piece of code which may or not run seems to introduce inherently greater mental complications into the programming process.
- lmm 13y agoFinalizers are almost inherently a bad idea in garbage-collected languages; the best practice is simply not to use them. If you're trying to do C++-style RAII (a very good idea!) you want something like Java 7's try-with-resources.
- salmonellaeater 13y agoThe key point is that finalize() is not reliable, so you shouldn't depend on it. The correct idiom is more or less myResource = foo(); try { ... } finally { myResource.release(); } Some programmers (although I've never met one) try to avoid this boilerplate by writing a finalize() method on myResource that releases it. It's an anti-pattern. C# and Java have syntactic sugar now that makes the boilerplate shorter, which should help.
- barrkel 13y agoYou program as if you don't have finalizers. Finalizers really are just a safety net.