[Beowulf] Which is better GNU C or JAVA (for network programing)

Jakob Oestergaard jakob at unthought.net
Tue Jan 20 18:50:10 EST 2004


On Tue, Jan 20, 2004 at 08:05:45AM -0500, Robert G. Brown wrote:
> On Mon, 19 Jan 2004, prakash borade wrote:
> 
> > hey
> > 
> > 	can anybody tell me 
> > what do i choose for programing 
> > either GNU c or JAVA  for implementing some
> > networkprograms using some linux systemcalls
> 
> Gads, that's two questions in two days with the distinct whiff of crowds
> of screaming peasants waving swords and torches...
> 
> Let it be Jihad, then.


 It is better to light a flame thrower than curse the darkness.
  - Terry Prachett, "Men at Arms"


<jihad_mode="on">

Just keep in mind that *my* religion is *the* superior one, no matter
what the infidels want you to believe!   ;)

> The answer technically is "it depends".  This is to make the vast horde
> of java programmers feel loved, and to acknowledge that for writing web
> applications their primary scripting language (like perl, python and
> many another before it) has a purpose in God's world.

I beg to differ.  It does not depend.  Not when Java is in the
discussion.

Once Java is out, we can do the huggy-feely 'it depends' and "just
because it sucks it doesn't mean it doesn't have a purpose" type
discussions.

For any systems related programming task, C or C++ is to prefer over
Java.  For the following reasons:

*) C and C++ have consistent run-time performance, which Java *cannot*
   have due to its automatic garbage collecting (yes, programmers can
   apply various tricks to lessen the performance penalty and
   unpredictability - but that's more work for the programmer and
   for the administrator, and it simply cannot be as good as a well
   written C or C++ program)
*) Portability - well-written C and C++ programs can fairly easily be
   ported between various UN*X type systems - and new systems can fairly
   easily be supported by the existing codebase.  Larger Java programs
   are often not portable among various JVMs, and adding portability is
   not convenient in Java (since it lacks the "dirty" macro-processor of
   C and C++).  In other words; wrt. portability Java is a very "pure"
   language, since it lacks the constructs (preprocessor) that would be
   unnecessary in a "perfect world" - unfortunately for Java, the world
   is not perfect, and therefore the "dirtier" languages have a major
   advantage here.
*) Readability - for larger systems I recommend C++ over C - it allows
   you to write very efficient type-safe code, unlike C (which would
   either not be type-safe, or require more code) and Java (which is
   inefficient by design, and will do much of its type checking at
   run-time, decreasing programmer productivity, program performance and
   ease of testing)
*) Reliability - well written C++ tends to be very stable - most
   problems will display themselves at compile time, and you have
   excellent tools to verify that your code is running as it should
   at run-time (valgrind, ElectricFence, LeakChecker), and to inspect
   what went wrong when your program crashed (core dumps and GDB).
   Most of this holds true for C as well, except that fewer errors
   show themselves at compile time, because the C type system is less
   strict than the C++ type system.

> 
> There.  Let us bask for a moment in the serenity of our knowledge that
> we have the complete freedom to choose, and that there are no wrong
> answers.

I find your lack of faith disturbing...  ;)

> 
> Now we can give the correct answer, which is "C".

Typing a little fast there, I think...  The correct answer for anything
larger than 1KLOC is "C++" - of course, you knew that, you were just a
little fast on the keyboard   ;)

(KLOC = Kilo-Lines-Of-Code)

> 
> In order to give it, I have to infer an implicit "better than" and a
> certain degree of generality in your question is in "which language is
> BETTER suited to write an efficient networking program using linux
> systemscalls, in GENERAL".
> 
> With this qualifier, C wins hands down.  A variety of reasons:
> 
>   a) The operating system is written in C (plus a bit of assembler)

This ought not to be a good argument - I guess what makes this a good
argument is, that the operating system provides very good C APIs.

So, any language that provides easy direct access to those C APIs have a
chance of being "the one true language".

>   b) Nearly all network daemons and utilities and program clients are
> written in C or perhaps an extension of C such as C++

I prefer to think of "C++" as "A better C", rather than a "C extension",
as not all C is valid C++, and therefore C++ is not really an extension.

>   c) Nearly all decent books on network programming (e.g. Stevens)
> provide excellent C templates for doing lots of network-based things
>   d) You can do "anything" with C plus (in a few, very rare cases, a bit
> of inlined assembler) 

Amen!  This goes for C++ as well though.

>   e) C is a compiler and produces (in the hands of a good programmer) 
> code that runs very efficiently.  Java is an interpreter and does not.

Java is not necessarily interpreted - but that doesn't help (it is still
garbage collected, and it's general library routines such as lists etc.
still work on "Object" types, not the real types of the elements, which
makes it run-time inefficient by design).

So I absolutely agree that Java sucks, just for slightly different
reasons    :)

>   f) C is an open ANSI standard implemented in linux with fully open
> source tools with no restrictions or covenants on their usage and with
> no "vendors" in any sort of position of control over the language itself
> or its core libraries.  Java was invented by Sun, and Sun (AFAIK) still
> controls the distribution of its core libraries, and as an interpreted
> language, those libraries are sine qua non for portability.  Sun is
> relatively linux friendly, but I personally tend not to trust giant
> multibillion dollar companies that own core components of something I
> need to rely on (or incorporate those components into my code), as they
> have an annoying tendency to turn around and want money for it.
>   g) Or find themselves imbroiled in a corporate war with someboy ELSE
> with deep pockets advancing a different "standard"; for example I'd
> guess that Microsoft would like nothing better than to co-opt Java for
> themselves and <sigh> turn around and want money for it.  C is co-opt
> proof, and GCC is a highly reliable platform upon which to base code in
> the certain knowledge that the core compiler and libraries will remain
> royalty free.
>   h) ...and I could go on.

Me too, oh, me too   ;)

> 
> Note that I do not address java's OO nature, as OO C variants exist AND
> as OO programming is largely a matter of programming style, not language
> per se anyway.

Let me then.

Java *forces* OO.  C makes OO possible, but cumbersome and error prone.
C++ makes OO both possible and safe.

The fact that Java forces OO down your throat, whether you want it or
not, makes it an inferior language alone.

See, most real world problems are not stricly OO, or strictly
procedural. They tend to be a good mix. This is why C becomes cumbersome
in many larger projects, since it does not address the OO parts well.
And this is why Java is hopeless for larger projects, since it simply
forces a re-write of the would-be simple procedural code into something
sufficiently OO-like for the Java language to be able to comprehend it.

C++ on the other hand allows (and even encourages) a mix-and-match
approach - using the proper "tools" for the job at hand.

Oh, and not only does Java force OO, it forces inferior OO. It cannot do
multiple inheritance, which makes a lot of the good arguments for OO
vanish (single-inheritance encourages fat bases, which is all that OO is
*not* about).

> 
...
> 
> Note also that my diatribe above is clearly biased as I don't code in
> Java, largely become of all of the reasons above (and then some).
> Speed, power, portability, open standard and source, and available code
> base and learning support all argue in favor of C about 10 to 1.  UNLESS
> you are trying to write a powerful web-distributed application, which is
> a PITA in C and what Java was designed for.

Yes - I've been told that that is what Java does well.

In generall, I am pretty much of the oppinion that C++ does 99% of all
problems "well" or "very well", and the remaining 1% somewhere between
"poor" and "well".  Java on the other hand will do 99% of all problems
somewhere between "crap" and "not at all", where it will do the
remaining 1% "extremely well".

Unless you have a problem in that last 1%, there is probably better
languages than Java.

C, in my oppinion, would be somewhat like C++, except for larger
problems it doesn't fare qute as well (not poorly by any means, just not
as well).

The only very very large problem with C++ is, that almost no people know
the language.  There is a mountain of absolutely crap learning material
out there.  This is why you see examples of "C versus C++" where the C++
code is several times larger or even less efficient than the C example,
because the author felt that shovelling bad OO and braindead design
after the problem seemed like a good idea.

I believe that with the state of compilers today, nobody should have to
start a large project in C - except if it absolutely needs to run on a
platform for which no decent C++ compiler is available (maybe Novell
NetWare - but that's the only such platform that comes to mind...)

And I firmly believe, that in order to justify starting a project in
Java, you must have *thorough* understanding of not just the problem and
Java, but also about possible alternative languages - there really
should be very few excuses for using the language for anything at all.

...
> P.S. -- I >>tried<< to be mild, but I suppose I'll need to spend the
> rest of the day in a bunker wearing my lead underwear and a tinfoil
> skullcap.  Really, I love YAPL's as they pop up from year to year.  I
> do.  Marshmellows...ready!  Flame on...

<jihad_mode="off">

My office is cold, and I like the flames :)

Seriously though, I think that the language-flamewars are fun and
informative, since so much happens in the space of compilers and
real-world projects out there.  So, I think it's useful to get an update
every now and then, from people who have strong feelings about their
langauges - oh, and a discussion in the form of a friendly flame-fest is
always good fun too   ;)

Well, the bait is out, let's see if someone bites   ;)


 / jakob

_______________________________________________
Beowulf mailing list, Beowulf at beowulf.org
To change your subscription (digest mode or unsubscribe) visit http://www.beowulf.org/mailman/listinfo/beowulf



More information about the Beowulf mailing list