Joel Spolsky characterized C++ as \"enough rope to hang yourself\". Actually, he
ID: 653069 • Letter: J
Question
Joel Spolsky characterized C++ as "enough rope to hang yourself". Actually, he was summarizing "Effective C++" by Scott Meyers:
It's a book that basically says, C++ is enough rope to hang yourself, and then a couple of extra miles of rope, and then a couple of suicide pills that are disguised as M&Ms...
I don't have a copy of the book, but there are indications that much of the book relates to pitfalls of managing memory which seem like would be rendered moot in C# because the runtime manages those issues for you.
Here are my questions:
Does C# avoid pitfalls that are avoided in C++ only by careful programming? If so, to what degree and how are they avoided?
Are there new, different pitfalls in C# that a new C# programmer should be aware of? If so, why couldn't they be avoided by the design of C#?
Explanation / Answer
The fundamental difference between C++ and C# stems from undefined behavior.
It has nothing to do with doing manual memory management. In both cases, that is a solved problem.
C/C++:
In C++, when you make a mistake, the result is undefined.
Or, if you try to make certain kinds of assumptions about the system (e.g. signed integer overflow), chances are that your program will be undefined.
Maybe read this 3-part series on undefined behavior.
This is what makes C++ so fast -- the compiler doesn't have to worry about what happens when things go wrong, so it can avoid checking for correctness.
C#, Java, etc.
In C#, you're guaranteed that many mistakes will blow up in your face as exceptions, and you're guaranteed a lot more about the underlying system.
That is a fundamental barrier to making C# as fast as C++, but it's also a fundamental barrier to making C++ safe, and it makes C# easier to work with and debug.
Everything else is just gravy.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.