Java or C++?

G

Grzegorz SÅ‚odkowicz

You must be joking - better designed? C++ was a botch to an already poor
language.
Although I'm relatively new to the concept that C++ is too difficult to
use, I would concede that with certain mindset and priorities Java may
be a valid choice. Not so if one is willing to expand knowledge about
programming and find a complement for Python.

Where I can't agree is that C is a poor language. You may prefer Unix
was written in Pascal but C is still an excellent language, if no longer
'general-purpose.'

C++ continues its philosophy of granting as much control as possible to
the programmer. Whether it's a good or bad thing is debatable and
depends on purpose for which you're using it.
Personally I find Java very satisfying to write.
Bing.
 
P

Paul Anton Letnes

Well, if you're new - first find the function, then how to use it,
this funny %d5 (or something, don't remember) syntax - it's hard
compared to:
cout << 5 or similar stream tricks, or just 5 + "" in Java, or just
str(5) in Python. Anyway, small tasks are very hard for C newbies.




Den 15. april. 2008 kl. 19.35 skrev (e-mail address removed):
 
N

NickC

This will automatically call the constructors of any contained objects
to initialize the string. The implicit assignment operator
automatically performs the assignment of any contained objects.
Destruction is also automatic. When 'p1' goes out of scope, during the
destructor the destructor for all contained objects is called.

Yeah, C++ does try to be helpful, and all of those automatic copy
constructor, assignment operator and destructor implementations screw
up royally when confronted with pointers (and being able to use
pointers is basically the whole reason for bothering to write anything
in C or C++ in the first place). Code which relies on these default
method implementations is almost certain to be rife with memory leaks
and double-free bugs. So instead of being a convenience, they become a
painfully easy way of writing code that silently does some very, very
wrong things.

Other things like methods (including destructors!) being non-virtual
by default also make C++ code annoyingly easy to get wrong (without it
obviously looking wrong).

The whole design of C++ is riddled with premature optimisation of
speed and memory usage in the default settings, instead of choosing
safe defaults and providing concise ways of allowing the programmer to
say "I know optimisation X is safe here, please use it".

And the result? Any serious project in the language has to adopt it's
own techniques for avoiding all those traps, and those techniques are
likely to eliminate any supposed optimisations provided by the choices
of the C++ committee, while filling a code base with boilerplate that
only exists for the purpose of working around defects in the language
design (Scott Meyers has written at length about the worst of these
issues, far more clearly and eloquently than I ever could [1]).

That said, C++ code has one *huge* benefit over ordinary C code, which
is scope-controlled deletion of objects, and the associated Resource-
Acquisition-Is-Initialisation model. Even without using exceptions
(although those are handy as well), RAII is an excellent way of
guaranteeing that memory is freed, files are closed, or other
resources are released when a block of code is finished. RAII was
actually one of the inspirations behind the final form of PEP 343's
with statement.

Cheers,
Nick.

[1]http://www.amazon.com/Effective-Specific-Addison-Wesley-
Professional-Computing/dp/0201924889
 
R

rustom

Hello, I was hoping to get some opinions on a subject. I've been
programming Python for almost two years now. Recently I learned Perl,
but frankly I'm not very comfortable with it. Now I want to move on
two either Java or C++, but I'm not sure which. Which one do you think
is a softer transition for a Python programmer? Which one do you think
will educate me the best?

The movement from knowing no programming language to knowing one is
invariably a much larger one than the shift from one to two (see
http://en.wikipedia.org/wiki/Diminishing_returns). That is you are
likely to get much less from this shift than what you got from python
two years ago.

So before you make this shift do ask yourself: have you got the best
of what python has to offer? I taught python in the univ for a number
of years and I would always tell my students that the library
reference gave a better conspectus of modern day computer science/IT
than anything else I knew of. [That not too many of them listened is
another matter :) ]
Then python has a lot of advanced (new) stuff: generators and
generator expressions, comprehensions, lambdas and functional
programming, descriptors and protocols....

That said you can of course choose your second language to optimize
your learning (where optimize could be minimize or maximize!)

One language that is promising (and under active development) is curl.
(http://www.curl.com)
It is targeted to be
like C++ in efficiency (native compiler not VM or interpreter)
like C#/Java in its OOP with gc style
like Javascript in supporting dynamic rich web apps
in addition to replacing HTML
 
J

Jorgen Grahn

Yeah, C++ does try to be helpful, and all of those automatic copy
constructor, assignment operator and destructor implementations screw
up royally when confronted with pointers

I think that those are newbie problems. The rules for those three
"default implementations" are simple and match what C does for
structs. Use the standard containers, make a habit of forbidding
copying of objects which make no sense copying, and remember the
"explicit" keyword, and you will rarely have problems with this.
(and being able to use
pointers is basically the whole reason for bothering to write anything
in C or C++ in the first place).

Is it? I rarely use pointers in C++ as anything but a kind of
object reference, and mostly because I am forced to.

I use C++ because it is an expressive language with static typing,
which has access to all the hundreds of libraries with a C API on my
(Unix) machine. And because it is fun to use.

I use Python because it is an expressive language with dynamic typing,
which has access to the most important libraries with a C API on my
(Unix) machine. And because it is fun to use.
Code which relies on these default
method implementations is almost certain to be rife with memory leaks
and double-free bugs. So instead of being a convenience, they become a
painfully easy way of writing code that silently does some very, very
wrong things.

I have worked with old code with those kinds of bugs.

It's simple to check and fix. If a class has pointer members of the
Has-A type, the constructors, operator= and destructor have to handle
them (or be suppressed). If they don't, the code is broken.

If you grasp the concept of invariants, it's hard to get wrong. An
object of type Foo has a number of valid states. You have to make sure
there are no ways to create a Foo which is in an invalid state, or
destroying one without cleaning up its state. The best way is usually
to construct it from members which make similar guarantees, e.g. the
standard containers.
Other things like methods (including destructors!) being non-virtual
by default also make C++ code annoyingly easy to get wrong (without it
obviously looking wrong).

The other side of the coin is that you can write tiny classes in C++
with *no overhead*. If my class Foo can be implemented as an integer,
it doesn't need to be slower or take more space than an integer. It
can have value semantics, live on the stack etc, like an integer.

I assume Java programmers avoid such types, and I assume it decreases
type safety in their programs.

Ok, it could have been the other way around so that there was a
"nonvirtual" keyword ... but on the other hand I use inheritance in
C++ about as often as in Python, i.e. almost never.
The whole design of C++ is riddled with premature optimisation of
speed and memory usage in the default settings, instead of choosing
safe defaults and providing concise ways of allowing the programmer to
say "I know optimisation X is safe here, please use it".

"Premature optimization" is a phrase which is always useful as a
weapon, isn't it?

But yeah, I think we can agree about this, at least: when you program
in both Python and C++, it is painfully obvious that C++ never
sacrifices speed or correctness, and it is painfully obvious that the
programmer pays a price for this. Compare ... maybe, for example, the
C++ standard library's very detailed and general iterator and
algorithm concepts with things like Python's str.split and str.join. A
function which takes a list of strings plus a delimiter and returns a
string would be unthinkable in the C++ standard library.
That said, C++ code has one *huge* benefit over ordinary C code, which
is scope-controlled deletion of objects, and the associated Resource-
Acquisition-Is-Initialisation model.

Yes, RAII is one big advantage over any other language I know of.
Compared to good old C, I can come up with many others.

I was going to say something about C++ versus Java here, but the fact
is I haven't written more than a few pages of Java since it came out.
The language (or the culture around it) seems to want to isolate itself
from the rest of the world -- unlike C++ and Python.

/Jorgen
 
H

hdante

Summarizing the discussion (and giving my opinions), here's an
"algorithm" to find out what language you'll leard next:

1. If you just want to learn another language, with no other
essential concern, learn Ruby.
2. If you want to learn another language to design medium to large
size applications, considering market, jobs, etc., and the speed gains
of static byte-compiled languages, learn Java or C#.
3. If you want to learn another language to design applications with
speed gains, but you want that the transition be as smooth as possible
and don't have market concerns (and with the possibility of taking
another easy step later to reach step 2), learn Groovy (for the JMV)
or Boo (for .NET).
4. If you want to develop applications but, for some special reason,
you require native compilation (like speed requirements, embedded
systems, etc.), learn C++
5. If you want to develop system software, or just learn better how
machines work, or understand better low level implementation aspects
of software, learn C.
6. If you just want to speed-up your python programs or offer some
special, system-specific or optimized behavior to your python
applications, or you just want to complement your python knowledge,
learn C.
 
D

Dan Bishop

I think that those are newbie problems. The rules for those three
"default implementations" are simple and match what C does for
structs. Use the standard containers, make a habit of forbidding
copying of objects which make no sense copying, and remember the
"explicit" keyword, and you will rarely have problems with this.

Yes, but why should you have to remember? Wouldn't it be less error-
prone to make objects uncopyable and constructors explicit unless
stated otherwise? (Yes, default implementations for structs had to
match C, but "class" was a new keyword.)
The other side of the coin is that you can write tiny classes in C++
with *no overhead*. If my class Foo can be implemented as an integer,
it doesn't need to be slower or take more space than an integer. It
can have value semantics, live on the stack etc, like an integer.

I assume Java programmers avoid such types, and I assume it decreases
type safety in their programs.

I haven't written in Java since version 1.3, so maybe things have
changed. But last time I checked, if you want tiny value-semantics
types in Java, you're stuck with the built-in ones. One of my biggest
gripes about that language.
 
B

Bruno Desthuilliers

hdante a écrit :
Summarizing the discussion (and giving my opinions), here's an
"algorithm" to find out what language you'll leard next:

1. If you just want to learn another language, with no other
essential concern, learn Ruby.
2. If you want to learn another language to design medium to large
size applications, considering market, jobs, etc., and the speed gains
of static byte-compiled languages, learn Java or C#.
3. If you want to learn another language to design applications with
speed gains, but you want that the transition be as smooth as possible
and don't have market concerns (and with the possibility of taking
another easy step later to reach step 2), learn Groovy (for the JMV)
or Boo (for .NET).
4. If you want to develop applications but, for some special reason,
you require native compilation (like speed requirements, embedded
systems, etc.), learn C++
5. If you want to develop system software, or just learn better how
machines work, or understand better low level implementation aspects
of software, learn C.
6. If you just want to speed-up your python programs or offer some
special, system-specific or optimized behavior to your python
applications, or you just want to complement your python knowledge,
learn C.

And if you really want to actually *learn* something, learn a functional
language.
 
S

Stefan Behnel

hdante said:
6. If you just want to speed-up your python programs or offer some
special, system-specific or optimized behavior to your python
applications, or you just want to complement your python knowledge,
learn C.

"Learn C", ok, but then go and use Cython instead.

Stefan
 
J

John Nagle

Bob said:
C++ is for masochists. Go for Java.

Definitely Java. And I have ten years of C++ experience.

C++ is the only major language that has hiding without safety.
That was a mistake.

Perl is useful because it runs everywhere; you'll be able to run
your Perl program on just about any commercial web hosting service.
Other than that, there's not much good to be said for it.

Java is a generally good language fighting to get out from under
a mountain of mediocre libraries.

John Nagle
 
N

Nicola Musatti

On Apr 15, 1:46 pm, Brian Vanderburg II <[email protected]>
wrote: [...]
Yeah, C++ does try to be helpful, and all of those automatic copy
constructor, assignment operator and destructor implementations screw
up royally when confronted with pointers (and being able to use
pointers is basically the whole reason for bothering to write anything
in C or C++ in the first place). Code which relies on these default
method implementations is almost certain to be rife with memory leaks
and double-free bugs. So instead of being a convenience, they become a
painfully easy way of writing code that silently does some very, very
wrong things.

When a class includes a pointer data member, there is no single, right
way to handle it. C++ automatically generated member functions are
defined so as to be consistent in dealing with *values*. Any C++
programmer that hasn't learnt this simple fact, shouldn't be trusted
with any programming language. Python and especially Java will only
make it harder to spot the mess he is making.
Other things like methods (including destructors!) being non-virtual
by default also make C++ code annoyingly easy to get wrong (without it
obviously looking wrong).

I can see how that might be confusing for programmer coming from
Python, but it's more natural for those coming from C.
The whole design of C++ is riddled with premature optimisation of
speed and memory usage in the default settings, instead of choosing
safe defaults and providing concise ways of allowing the programmer to
say "I know optimisation X is safe here, please use it".

I absolutely agree.
And the result? Any serious project in the language has to adopt it's
own techniques for avoiding all those traps, and those techniques are
likely to eliminate any supposed optimisations provided by the choices
of the C++ committee, while filling a code base with boilerplate that
only exists for the purpose of working around defects in the language
design (Scott Meyers has written at length about the worst of these
issues, far more clearly and eloquently than I ever could [1]).

Did you give up on C++ in the early nineties? Things have changed a
lot since then. Many standard/commonly accepted solutions to the
problems you mention can be found in the C++ standard library and in
Boost (http://boost.org). With std::vector and boost::shared_ptr you
can go an extremely long way without giving pointers any special
considerations.

Cheers,
Nicola Musatti
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top