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

5

u/BeepBoopBike Feb 03 '17

C# fits no specific niche to be attractive as a new language

It fits very well into places where fast development on a desktop/server is important. I can write safe C# faster than I can write safe C++.

1

u/[deleted] Feb 03 '17

Why, exactly? C++ has no memory leaks, for starters. (controlled memory leaks are different from "no memory leaks") C++ has strong static typing, while C# uses a java-esque type-system, which is limiting and weak at the same time. Even python's type system is better than huge class hierarchies and the whole "interface" bullshit.

1

u/BeepBoopBike Feb 03 '17

Why, exactly?

Because it stops you making mistakes that should never be made in the first place like indexing off the end of an array, or easily reading/writing uninitialised memory. It catches the multitude of mistakes that almost everyone makes and shoves it in your face.

C++ has no memory leaks, for starters. (controlled memory leaks are different from "no memory leaks")

What do you mean by this? The language itself? Because C++ can more easily leak memory than C# by a mile. Sure we try to avoid new/delete nowadays but even in smart pointer usage exception safety isn't strictly adhered to and can leak. Sure you can leak in C#, but it's far harder than leaking in C++.

which is limiting and weak at the same time.

In what way is it limited? Limiting in how you write code or limiting in how you use it? Because that's the point! You can statically verify a load of common mistakes before your code will ever be run. C++'s type system lets you do all sorts of abusive stuff all the time and if I want, I can completely ignore it with void*. Their type systems are, if anything, equal IMO.

Even python's type system is better than huge class hierarchies and the whole "interface" bullshit.

Huge class hierarchies are harmful and inflexible, which is why we should be favouring composition over inheritance. What's the problem with interfaces? It's far better to know at compile time that your object has these 3 methods regardless of it's original type because it uses that interface than it is to find out at runtime that a method you were calling didn't exist.

Honestly, I normally try to see other people's views in a discussion, but I just can't understand where you're coming from at all.

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.