Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/nebupook/public_html/include.database.php on line 2
NebuPookins.net - NP-Complete - Why I think Java is better than C/C++
 

Deprecated: Function ereg_replace() is deprecated in /home/nebupook/public_html/include.parse.php on line 32

Deprecated: Function ereg_replace() is deprecated in /home/nebupook/public_html/include.parse.php on line 33
Why I think Java is better than C/C++
[Computer]

I have a few computer engineer friends who like to give me a hard time because I program in Java instead of C/C++ C/C++. They make it sound like it's something for which I should be given pity!

Oh, it's not his fault. McGill pushes Java on all their students.

I attribute a lot of our differences to the stereotypical tension between computer scientists (idealists) and computer engineers (realists), and I think this is reflected heavily in my preference of Java over C/C++ (and vice versa for them). I think Java is a much more elegant language than C/C++. They say that Java is basically taking C++ and running it on an emulator (thus making it slower). I say Java is C++ minus all the "backwards compatibility with C" baggage.

Since I used to do C/C++ in highschool (by that I mean C++ written in a procedural style), I'm seen by them as a sort of traitor, or one who has turned to the dark side. They ask "Don't you miss pointers?" and I had to laugh, and laugh, and laugh. All I could reply was "Try to guess when was the last time I saw 'core dump'." Java's closest equivalent is NullPointerException, and I don't think I've ever had a NPE get thrown which I couldn't fix within 30 seconds thanks to the stack trace. Garbage collection doesn't just stop memory leak bugs; it also stop bugs which arise from deallocating too early out of fear of memory leaks.

Anyway, now that I have a job as a compiler language, I've discovered more ammunition to use in the constant battle I have with these friends. I've discovered that both the C and C++ languages are ambiguous.

What does this C code do?

foo(*bar);

Most programmers assume it's a call to a function named "foo", with a dereferenced pointer to a variable named "bar". But it might also be declaration of a variable named "bar" of type "pointer to foo". The only way to tell is to perform a symbol table phase, depending on whether foo represents a function or a type. The situation is worst with C++. Now it could also be a construction of a new object of type foo. Here, even the symbol table solution doesn't work: Even if we know foo is a type, it could either be a declaration of a variable named bar of type "pointer to foo", or a call to the constructor foo.

In Java, you cannot declare a variable using the form "identifier(identifier)", so anything that appears in this form, you know is a method call. As for constructors, Java requires the use of the keyword "new".

What does this C++ code do?

x = abc<a, b>(12);

This might be a call to a constructor for a generic class, with two template parameters "a" and "b", and one parameter to the constructor "12". Or it might be an expression in which the boolean value "abc < a" is assigned to x, and the boolean value "b > (12)" is discarded. That's right, C++ is crazy enough to let put multiple expressions in an assignment expression only to throw away the results of those expressions. What was the point of that?

In Java, if this were a call to a construct, again you would need the keyword "new". And in Java, you can't put multiple expressions on an assignment statement separated by commas only to throw away the result of those expressions. I mean, why would you ever want to compute an expression and then throw away its value?

Now consider this code:

void f(long); void f(char*); void g() { f(0); }

What method gets called? 0 is of type integer. Will the compiler cast this to long, or will it cast it to pointer of char? It's not even defined! The GnuCC compiler picks randomly according to this source.

Java avoids this problem by not having a pointer type. You can't make an implicit cast from int to char (such casts must be made explicit), so the cast would be to long. So it's always well defined what happens in Java.

If I told you "I just invented a programming language where when you make a method call, it's decided randomly which method actually gets called," you'd probably say that's the dumbest language you've ever heard of. Well, that's C for you.

I'm probably gonna get some flames for this. The arguments I make are factually correct (I think anyway, I haven't actually tested the code samples myself, not having convenient access to a C/C++ compiler), but I did lay on some "fighting words" for the humour factor. Sorry if I offend any C/C++ fans out there. I do admit that C/C++ has its place, just like ADA, Pascal and FORTRAN had their places. But I think it's time to move on to languages which are well defined, if only because it makes it easier to reason about them, and prove theorems about them. This allows us to write tools that automatically generate code, or that perform refactoring, or any other mundane task that might be much too tedious (or even error prone) for a human programmer.

 
Deprecated: Function ereg_replace() is deprecated in /home/nebupook/public_html/include.parse.php on line 60

Deprecated: Function ereg_replace() is deprecated in /home/nebupook/public_html/include.parse.php on line 61
E-mail this story to a friend.

You must be logged in to post comments.

Sites linking to this post: