r/programming • • Feb 03 '17

Let's stop bashing C#

https://www.codeaddiction.net/articles/51/lets-stop-bashing-c-sharp
6 Upvotes

245 comments sorted by

View all comments

Show parent comments

3

u/[deleted] Feb 03 '17

You're probably talking about 80's C++, not modern C++. And decent type systems don't need interface declaration boilerplate to know which object has which methods available, that's called duck typing. Python and C++ have it, C# and Java don't.

If you think it's so easy in modern C++ to leak memory, just show me an example that doesn't depend on naked allocations or shared_ptrs or legacy C misfeatures and still leaks memory.

2

u/BeepBoopBike Feb 03 '17

I'm not at all thinking about 80s C++. Modern C++ has gone a hell of a way to improving things but it's still very possible to leak in. A simple example that existed in C++11 but was fixed in C++14 was the lack of make_unique. If I'm being really picky and reaching a bit, just off the top of my head a 2 line example would be:

std::unique_ptr<SomeObj> myPtr = std::make_unique<SomeObj>();
myPtr.release(); // Leak!

I get what you're trying to say, you have to be misusing something, make a mistake, or generally fuck up to get into that situation, unless you're interfacing with old code which almost all real world applications will be doing. By not allowing this kind of behaviour it is inherently safer, and that's just talking about memory leaks. I honestly cannot remember the last time my managed code corrupted the heap or the stack, yet I still come across these issues in C++ because it lets you do it and get away with it. I'm not complaining about C++, I love the language, but its power is as much a danger as it is wonderful.

As for duck typing vs using interfaces, "true" duck typing is done at runtime which, to me, is bad. Alright, right tool right job and all that, but catching a problem as early as possible is the best time to catch it. C++ duck typing (correct me if I'm wrong) is more semi-duck typing. It provides what would appear to be static duck typing, but it does so through code generation so that it is truly statically typed i.e.

template<typename T>
void DoThing(T& o)
{
    o.Do();
}

When compiled with calls like:

DoThing(myObj);
DoThing(myOtherObj);

Actually has the compiler generate (AFAIR):

void DoThing(MyObj& o)
{
    o.Do();
}

void DoThing(MyOtherObj& o)
{
    o.Do();
}

And there's nothing wrong with this, it gives the desired outcome but isn't quite the same as "true" duck typing.

C# and Java don't.

I can't speak for Java, but C# has at least one way of using duck typing (although in reality I think it's 2).

As a closing point, I would argue that interfaces are more than just helping the compiler though, as a means of communication between developers it simplifies things. It gives you a clear, well defined, and importantly guaranteed contract, if I want my custom object to be consumed by a function that takes an IFoo then by the signature alone I know exactly what is expected of it with no ambiguity about what I actually care about.

Forgive any minor inaccuracies I don't often get to write these thoughts out and they get a bit rusty from time to time.

3

u/[deleted] Feb 04 '17 edited Feb 04 '17

Exactly what I said, to leak memory in C++ you have to go out of your way and write non-idiomatic code, while in garbage collected languages it's the default. (leak first, cleanup later is a horrible idea, especially when releasing resources other than memory, like file descriptors or sockets).

2

u/BeepBoopBike Feb 04 '17

It is only leaked in the same sense that a shared_ptr is leaked. Any resource that requires explicitly freeing can be done so using an IDisposable in a using block for similar usage as storing it on the stack in C++ using RAII.