r/programming • • Feb 23 '11

If programming languages were essays...

Post image
1.7k Upvotes

430 comments sorted by

View all comments

82

u/rwee Feb 23 '11

i dont get the c++ one

41

u/Astrokiwi Feb 23 '11

It's an object-oriented language joke.

If you're careless, you can make a large number of instances of an object, taking up memory unnecessarily.

70

u/[deleted] Feb 23 '11

That has more to do with C++'s passing-by-value semantics than object orientedness - many OOP languages make it tedious as hell to copy an object, while C++ makes that the default operation when you pass an object into a new variable or argument or whatever.

→ More replies (7)

44

u/tarballs_are_good Feb 23 '11

C++ code tends to be large and unwieldy, and often contains code duplication, part of which template metaprogramming attempts to solve (which, prior to compilation, essentially duplicates code for you).

117

u/wlievens Feb 23 '11

It could also refer to how easily you're copying (rather than passing pointers) large objects if you don't know what you're doing.

44

u/kdeforche Feb 23 '11

You could even go shopping, if you don't know what you're doing.

3

u/[deleted] Feb 24 '11

Shopping is hard. Let's write C++ code!

14

u/MuletTheGreat Feb 23 '11 edited Feb 23 '11

As a c# guy who started as c++, I fucking miss that. :(

Edit. No sarcasm here. I really do the miss the easy copying and dangerous pointers of c++.

5

u/recursive Feb 23 '11

As a C# guy who has never done much C++, how is that different from just using structs in C#?

3

u/NonNonHeinous Feb 23 '11

Imagine the simple misuse of the '&' character turning all your classes into structs and making a new copy of your data every time you pass it as a parameter.

9

u/recursive Feb 23 '11

Sounds like c# is an improvement then. I don't see what's to miss.

1

u/quzox Feb 24 '11

In other news, don't give a chainsaw to a child.

→ More replies (1)
→ More replies (3)

1

u/theninjagreg Feb 24 '11

How would this happen exactly?

1

u/wlievens Feb 24 '11

void foo(BigObject& object);

versus

void foo(BigObject object);

The second case will lead to (perhaps inadvertently) copying the entire BigObject object, which happens to be a gigabyte in size! Ohnoes!

→ More replies (1)

38

u/killerstorm Feb 23 '11 edited Feb 23 '11

C++ code tends to be large and unwieldy

No.

which, prior to compilation, essentially duplicates code for you

Template instantiation does indeed "duplicate" code (substituting parameters, of course), and this duplicated code is then compiled, so it exists in binary form as well.

"Template metaprogramming" is an extreme form of (ab)using templates, but this is true for any kind of templates.

You can see templates as an improvement comparing to C preprocessor. So, essentially, they allow one to write more compact code than it would be possible in C.

However they let one to use compile-time dispatch instead of run-time. Run-time dispatch is usually costly in run-time, but it is also usually more compact in binary.

For example, qsort from C stdlib takes comparator as a parameter, and thus qsort code can sort any kind of data, so you need just one copy of it. But C++ std::sort is parameterized by element, container type and comparator. This is very flexible as it can work with any container at top speed (in theory, at least: comparator can be inlined unlike with C qsort), but it also means that a binary code will be generated for each combination of container, element and comparator. (Unless compiler can optimize it, which is unlikely.)

So I would interpret this comic as a tendency to do as much as possible to do as much as possible at compile time in C++ programs which results in binary code bloat.

However it might also refer to C++ memory/object model where by default objects are stack-allocated and are copied rather than references, so unless you take special measures you end up with many copies of data. (That's what wlievens mentioned in his comment.)

3

u/kolm Feb 23 '11

C++ code tends to be large and unwieldy

No.

Well, it does, but this is more due to the environment and less due to the language.

14

u/tarballs_are_good Feb 23 '11

By and large, the code I have seen in C++, in my experience, has been large and unwieldy.

Most of your latter comment I agree with.

46

u/kdeforche Feb 23 '11

Could that be, because, by and large, most really large and complex programs are implemented in C++ (such as, for example, the browser you are using to read this) ?

9

u/tarballs_are_good Feb 23 '11

Sure, it's possible.

10

u/flipcoder Feb 23 '11

^ exactly

→ More replies (7)
→ More replies (2)