Earlier quoted context omitted.
Which counts as the sugar which he mentions. See also "using" in C#. These are pretty clearly design warts. WPF (C# UI library) jumps through some interesting hoops to make it look like all your resources can be properly garbage collected, but even then it tends to come back and bite you for any non-trivial application.
It doesn't really taste like syntactic sugar though; it's a special functionality of the standard library, and you could easily get away without knowing about the Java/C-style API of keeping a file handle that has to be controlled around.
Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)
21–30 of 72 posts
Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)
#22...am I reading correctly that his argument for using C++/D (!) is that it's hard to remember to say this: try { mightFail(); } finally { doCleanup(); } instead of this: mightFail(); doCleanup();
That seems like a pretty serious argument to me. Not only is it a pain in the ass to remember and write that every time you consume a resource that needs to be released somehow, it also introduces all kinds of scoping headaches in Java. If mightFail() returns a value that you want to use after cleanup (i.e. all the time,) then you have to declare the variable to store that value outside of the try {} block.
That's kind of the point. The variable will only be assigned a value if mightFail returns normally. The scoping rules make it impossible to accidentally use an uninitialized variable (i.e. a variable whose first assignment was not reached due to an exception).
Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)
#23Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)
#24Interesting how java is the only language included in the title, but the slides have the opinion that C#, Ruby and Python all suck as well. Seems like a cheap way to get upvotes.
And in python you'd use the with construct.
with open("some.filename") as f:
...
f is an opened file which is automatically closed. This isn't strictly necessary in Python, since files are flushed and closed when reaped, including in case of exception, but it's useful. You can also do this with all of the threading primitives: with threading.Lock():
do_that_one_contentious_thing()
Useless syntactic sugar? Maybe. It's an explicit scope which makes certain guarantees, though, so it's not just fluff.What was his other example? DB connections? Well, in Python, the DB API requires that DB connections not easily leak, but if you're stuck with a crappy driver, you can still auto-close your connections:
with contextlib.closing(dbapi.Connection(...)) as handle:
cursor = handle.cursor()
...
So it's definitely possible.I think it's unfair of him to pick on Ruby and Python just because their syntax is more oriented towards assuming the garbage collector is non-sucky and exceptions aren't expensive.
Edit: Fixed formatting.
Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)
#25...am I reading correctly that his argument for using C++/D (!) is that it's hard to remember to say this: try { mightFail(); } finally { doCleanup(); } instead of this: mightFail(); doCleanup();
class C {
void mightFail() { ... }
~C() { doCleanup(); }
};
C c;
c.mightFail();
Anyway, I hate this slide deck so much.1. The author's point could be made in far fewer slides. As in, like, two slides. I hate presentations that are disrespectful of the audience's time for the sake of being cute.
2. I am generally unimpressed by arguments of the form: (a) Language X has flaw Y. (b) Therefore language X is unsuitable for development. This can be instantiated for every language X for some flaw Y and is not an argument against any language. You need to additionally make the argument: (c) Y is so serious that it outweighs the considerations in favor of X and against the other languages one might reasonably use. Of course (c) is an incredibly high bar, which is why most anti-language zealot arguments do not even attempt to make it, and also why most anti-language zealotry is silly.
In order to do (c) in this case, you would have to make the case that writing finally clauses is worse overall than, e.g. debugging memory corruption errors, and writing copy constructors and overloaded assignment operators, and all the other baggage of C++, rather than handwaving them away with "the downsides have been exaggerated".
Which, by the way, is ironically the biggest flaw of this slide deck: the author vastly exaggerates the downsides of finally. try/finally is no worse than checking the result codes of C syscalls, and it is sometimes better. I don't recall the C style of programming stopping Ken Thompson from building great things and I doubt that try/finally is actually what stops Java programmers from building great things.
Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)
#26Earlier quoted context omitted.
It's unclear to me why he says that the JDK7 addition doesn't really solve the problem. It seems to me that it is exactly this problem that ARM blocks are trying to solve. It's still not as clean as leveraging deconstructors when an object goes out of scope but it's much better than before.
Well, it's an awkward solution, because exceptions have this general problem of blowing away everything you're doing, and it's only a solution for this one case. The solution to this problem that makes sense to me is either conditions and restarts a la Common Lisp, or a type system that can handle multiple return values of different types in a sane way (e.g. return either a result or an error code and then pattern ma…
Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)
#27...am I reading correctly that his argument for using C++/D (!) is that it's hard to remember to say this: try { mightFail(); } finally { doCleanup(); } instead of this: mightFail(); doCleanup();
Although if doCleanup() also does some additional cleanup, like cleaning up some associated resources that aren't scoped to this block then I think you're still screwed. Maybe someone can clarify if I'm wrong.
Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)
#28Earlier quoted context omitted.
That seems like a pretty serious argument to me. Not only is it a pain in the ass to remember and write that every time you consume a resource that needs to be released somehow, it also introduces all kinds of scoping headaches in Java. If mightFail() returns a value that you want to use after cleanup (i.e. all the time,) then you have to declare the variable to store that value outside of the try {} block.
> [...] then you have to declare the variable to store that value outside of the try {} block That's kind of the point. The variable will only be assigned a value if mightFail returns normally. The scoping rules make it impossible to accidentally use an uninitialized variable (i.e. a variable whose first assignment was not reached due to an exception).
try {
var result = mightFail();
}
catch {
handleError();
}
finally {
cleanup();
}
print(result) // we don't want to allow this
That's true, the scoping protects you from making that mistake. However, the more common case is this: try {
var result = mightFail();
}
finally { // let the exception bubble up
cleanup();
}
print(result); // this must be OK, but I can't do it in Java
There are two things I can do to fix the latter case. I can either declare the variable outside the block, adding a silly-looking extra line of code, or I can move the print() within the block, which can be problematic -- it means that if print() were something time-consuming, I would be holding onto the resource during print() for no reason. I think this is a crappy thing to force onto the programmer.Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)
#29While I agree that Java sucks because it makes certain very common things require extreme verbosity, worrying about garbage collection isn't all that important except in systems-level programming (which isn't done in Java really), and large GUI that need tons of memory and still need responsiveness. But many people wouldn't even think to use Java in those cases anyways, so I'm not really sure what this guy's point is.
Re: Exception Safety, Garbage Collection etc. (a.k.a. Why Does Java Suck So Bad?)
#30I think you can avoid nesting finally thus: X x = null; Y y = null; try{ x = foo(); y = bar(); yadda(x,y); } finally { if (x!=null) x.dispose(); if (y!=null) y.dispose(); }
Util.dispose(x,y);
...and let that handle all the possible issues.