peter koch said:
I agree here. Readability matters a lot. And here C++ is a clear
winner, due to its more advanced features such as templates, operator
overloading and RAII,
You seem to have missed the point... whether intentionally or otherwise,
I don't know. I am referring to how quickly the language can be
understood; not how quickly software written in the language can be
understood.
Nevertheless, since we're now talking about it:
// C++
func()
{
class_with_possible_ressource cwpr;
dosomethingwith(cwpr);
}
// Java
void func()
{
ClassWithPossibleResource cwpr = new ClassWithPossibleResource();
try
{
doSomethingWith(cwpr);
}
finally
{
cwpr.dispose();
}
}
I don't know where you got the so-called Java code you posted. Whoever
gave it to you, don't accept their Java code any longer. Corrections
aside from syntax and naming conventions include:
1. A constructor would throw an exception if initialization failed, so
there is no need to check for that situation.
2. There is no need to catch exceptions within the finally block. If
the call to the clean-up method (e.g., dispose) fails, then an exception
will be thrown, which is what you wanted anyway. Although some people
may choose to be picky about which of the two exceptions they wish to
see in case doSomethingWith also failed, the C++ code doesn't handle
that any better, so it's rather irrelevant here.
3. Removed the cast to an interface before calling dispose. Upcasting
of references is always dispensible in situations like this. It appears
to have been added for no other reason than to make the Java code look
longer and uglier. Actually, come to think of it, most of the code that
was posted appears to have been added for that reason.
Four simple lines of C++ becomes 21 lines of complex and convoluted
Java-code.
Not sure how you're counting. Nevertheless, the C++ code is clearly a
little bit shorter, since the Java code needs to make cleanup explicit.
So the Java func above is as easy to understand as the C++-one?? Come
on - you do not really mean that.
No I don't mean that. See above.
Also you have a huge problem writing
generic code in Java .... those ugly and presumably costly runtime
checks have to be made all the time.
How is that relevant? Or is this just becoming a gripe list about
languages, which you obviously don't understand to begin with?
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation