Re: On Java and C++
Chris Smith skrev:
Roedy Green wrote:
Mixing exception handling and memory management boggles the human
mind.
Noah Roberts <roberts.noah@gmail.com> wrote:
Only one incapable of learning very simple techniques to make it a
non-issue.
http://www.hackcraft.net/raii/
There are two questions being considered simultaneously here. One is
what is required to produce useful software in a language. The other
matter is what is required to understand that language. I side with the
position that a language that's hard to understand has a weakness in
this even if it remains possible to write software using that language.
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,
Exception handling and memory management IS tricky in C++.
I must disagree. Exception handling is far easier in C++, due primarily
to RAII. Consider:
// C++
func()
{
class_with_possible_ressource cwpr;
dosomethingwith(cwpr);
}
// Java
func()
{
class_with_possible_ressource cwpr = new
class_with_possible_ressource ;
if (cwpr->did_initialise_properly())
{
try
{
dosomethingwith(cwpr);
}
finally
{
try
{
idisp = (IDisposable)cwpr;
idisp->Dispose();
}
catch (...)
{
}
}
}
}
Four simple lines of C++ becomes 21 lines of complex and convoluted
Java-code.
If you know that the class contains a ressource you save four lines -
and if you know that the class does not contain a ressource (and you
dare betting your program that it newer will) you go down to seven
lines - almost the double of C++.
[snip]
RAII is a different matter. It's a nice feature to have; but it doesn't
make the language any easier to understand.
So the Java func above is as easy to understand as the C++-one?? Come
on - you do not really mean that. 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.
/Peter
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation