Falling Into The Pit of Success

I’m currently studying Comp Science at University and this year have to program in Visual C++. Handles, pointers, references, values… things very counter-intuitive for a new programmer. I’ll admit c++ has alot of virtues attributed to it… but you definitely need to invest a huge amount of time to learning it well.

I also have to program in Java this semester (and previous semesters) which I’ve found to be VERY intuitive. It’s easy to pick up, learn from scratch, and run with it… developing (relatively) simple programs in no time at all with very little effort.

Visual C++ on the other hand… I had to create a few basic classes yesterday to draw a circle and a square, and spent 90% of my time debugging errors which as I see it… should be picked up by the IDE. Like I said, I need to spend time learning and practicing - but it is still a very difficult language.

Oh yeah, I wanted to make another quick point about Q and semicolons and statements:

If your language ends statements with both line breaks AND semicolons, and uses a continuation character to continue onto the next line, your IDE can protect you from errors related to missing semicolons. That’s because when you split a line into two, the IDE can easily check whether the statement is being continued on the next line or no (if there is a binary operator, or a parenthesis is unclosed, or whatever), and will automatically insert the continuation character and indent your next line. However, with a semicolon language, the IDE’s job would be to insert semicolons for you, and it won’t know, in some cases, where exactly to do that! It would have to second-guess you.

Reasoning like this is what led to a bunch of features in Q. Do you think it’s sound, or am I just blabbering away? Honestly :stuck_out_tongue:

Greg

Following on from my previous comment and Webview’s. I also really like the distinction that C+ makes between objects, references, pointers and the ability to pass them around as constants with constant functions. This plus the type safety make it possible to write most code that simply will not compile if you’ve written it wrong. This is opposed to Java where you don’t know until you run something whether it’ll work or not because you’re casting (because you’re forced to not because you’ve explicitly stated to) to the wrong type or modifying something you shouldn’t be because you can’t make it const, or a class doesn’t exist that you’re trying to use etc. In C++ the syntax also shows you in plain sight exactly what you’re doing with your types, you know if something is a reference or not and thus if using it will affect something else.

Operator overloading is a beautiful invention too, I love the clarity it can give code. The only arguments I’ve ever heard against them is that people will forget to check the operator on a type when trying to track down a bug, thus making the bug harder to find. Which is frankly a silly argument because if something isn’t a built in type it might have it’s operator overloaded, so go look.

Header files for function definitions I like too. Other languages seem to rely on the IDE to provide a nice list of methods of the class you’re in to the side which I disagree with. Having all the functions in one place listed like in a header file is just nice and isn’t a burden to maintain.

The thing I’ve found with my little exposure to languages like Ruby that favor convention over explicitly stating something, is that you have to remember exactly what the conventions are. Reading the code does not tell you what the conventions are, you have to know them. I find that quite difficult to pick up, I’m not the sort of person that can just accept at face value that something is going to “just work”. It’s all voodoo to start with.

I seem to remember reading an article on here or somewhere else that advocated understanding the boiler place code that IDE’s write for you with class generators, but Ruby et al. goes beyond on not doing that and take it to a whole new level. C++ tells you exactly what it’s doing and I like that.

Adding a massive amount of additional libraries like Java and c# have would probably be the single best thing that the language could be added. This is probably the overriding factor that makes Java so appealing, there’s just less to do yourself. But that would be a difficult thing to do because of the differences the language faces moving between platforms. Plus C++ has never really been emphasized at application programs, even though it’s favored for a lot of them for various reasons.

If you don’t want to use C++, use Java.

The issues mentioned above happen if you don’t code C++ properly. If you use arrays instead of vectors or manually call new and delete instead of using RAII, you will have problems. Your code won’t even be exception safe.

The biggest problem with C++ is that there is so much to learn. It’s way too expert-friendly a language.

Regardless, here’s my guide to choosing the language for your new project: Which language has the library support you need?

Library support is far more important that language trivialities. With luck, a good library has solved nearly all of your program for you, and you just need to fill in the details. Same goes for prior code-base or purchasable code-base (don’t code it when you can buy it).

It has been proven time and time again that smart programmers can create good products in any language and crappy programmers can create crappy products in any language. Hire good programmers and choose languages based on available libraries and code.

Jeff,

I agrees with Tod McKenna’s comment. It’s depends on the person who uses it. For me, C++ is a comfortable language. I started programming 4 years back and I hadn’t a chance to work with “complex” implementation. But I worked with different medium sized projects with enough complexities.

I never went to managed world. When I was thinking about MFC, I thought it’s really slow. But .NET is even slower. It’s depends on such a heavy framework. Microsoft’s approach on .NET versioning seems not good enough. The hell things they’re saying indirectly that you need to buy our new version of Visual Studio to work with new .NET framework. From the top to the bottom, Microsoft’s strategy on .NET and it’s related languages/extensions is not good… What do you think?
The other point is that C++ compilers are now matured enough. Hope Microsoft will make things light and easy. Moreover, a language should be able to satisfy different aspects of programming from lower level to higher level.

So, how do you do this? And how do any of the languages/processes you name do this?

There’s an easy way to avoid falling into the pit of C/C++ despair. Watch where you’re f’n walking! You only need a railing to keep you out if you are reckless, stupid, or blind. Most experienced developers rarely encounter the basic problems listed at the beginning of this article, or if they do, they are able to find the bugs pretty easily. Most bugs come from higher level problems that most programming languages have to some extent or another (logic errors, thread issues, etc.)

C++ is only dangerous to people who have only half learned it.

Why do people expect to be able to write production-quality software in a language they have only half learned?

Why do people WANT to be able to write production-quality software in a language they have only half learned?

Well I agree with you Jeff, JAVA and C# are nice languages and so are other high-level languages. So you would think, why would anybody be so silly and still use C++, when there are nicer and cleaner alternatives around.

BUT the fact remains, that C++ is still the most used language and almost every big, serious $ successful Software product is written in C++. You won’t see anybody do Office.NET or Photoshop in Java. Just look at this small list of Apps written in C++. a href="http://www.research.att.com/~bs/applications.html"http://www.research.att.com/~bs/applications.html/a

Also in professional game-development, there simply is just nothing else than C++.

So in conclusion, Java and C# are nice, but still viewed as “fancy C++ wrappers” by the industry, and every serious thing will still be made in C++.

One of the big reasons I did Win32 programming in Delphi, and many a good commercial app was written it in.

Still remember my C++ friend who was amazed that a program I wrote in it had no memory leaks (it was not due to my skills).

It is amazing how C++ spread so far. Hopefully Delphi can have a revival with native Vista apps…

I come to defend C++ not to bury it.

Quite frankly I’m tired of these complaints. Fact is that every language comes with more than a syntax. It also includes:-

  • a “programming model”
  • idioms and,
  • best-practices,

You have to learn all three of these as well as syntax to use the language effectively. If you don’t know what these are in your chosen language, you shouldn’t be asking people to pay you to program in it.

This article - and many others like it - is just a whinge by an author who is insufficiently versed in these elements to survive in a commercial environment using C++. Poor craftspeople blame their tools, good ones don’t.

Complaining about pointers, references, inheritence, etc is just a sign that you don’t understand the language enough to be commercially productive.

If you know the C++ programming model, the idioms and the practices you won’t have any trouble producing trouble free code as a matter of routine. I know (and have hired) several people who have a proven track record over many years now of producing upwards of 500-1000 lines of working, tested code into production. [YES - I’ve got the stats, and no, you can’t have their names].

If you don’t know these things you’ll be reinventing the wheel and making it up as you go along. That is very definitely the road to blowing your leg off.

Ok - got that out of the way.

With C++, there’s one more thing. Are you talking about “windows” C++ (where the Windows API’s and libraries are involved) or “unix” C++ (where the Unix/Posix environment is used). Sometimes the terms “server-side” and “client-side” are used, but they essentially refer to the same idea.

Each has a different set of idioms and best-practices, and anyone who knows one will find it difficult to operate in the other environment. If you doubt this, talk to a recruiter or better still a good technical lead who hires developers. It is common for such people to make this distinction and refuse to consider a candidate with a “client side” backgroud for a “server side” position (and vice versa). I know that’s my practice, and I know it’s also
a widespread practice in the market generally.

In my experience a server-side guy who’s read Coplien (and the rest of them), and who knows what the hiding rule is, is someone worth hiring. A client-side guy who’s read Petzold (and the rest of them) is worth it for the client side. Anyone without these knowledge sets is a very dubious hire.

Bottom line. You either know the language or you don’t. If you know it, you’re a hire. If not, you write articles like this.

C++ is a fine, very fast and efficient language that is perfectly safe when used properly by a knowledgable and skilled developer.

You may give up some performance when you choose to code in C#, Python, or Ruby instead of C++. But what you get in return is a much higher likelihood of avoiding the miserable Pit of Despair

I choose to program in Delphi because it gives me the execution speed AND low-level memory management capabilities of C++. The difference is that with Delphi I can CHOOSE to use low-level memory management, and I am not required:

http://en.wikipedia.org/wiki/Reference_counting#Delphi

One language that uses reference counting for garbage collection is Delphi. Delphi is not a completely garbage collected language, in that user-defined types must still be manually allocated and deallocated. It does provide automatic collection, however, for a few built-in types, such as strings, dynamic arrays, and interfaces, for ease of use and to simplify the generic database functionality. It is important to note that it is up to the programmer to decide whether to use the built-in types or not; Delphi programmers have complete access to low-level memory management like in C/C++. So all potential cost of Delphi’s reference counting can, if desired, be easily circumvented.

That and I don’t need a framework/interpreter installed on the machine.

Oh, and Delphi 2007 still is the only IDE with full native support for Windows Vista.

Or, if you’d like to write programs for Linux and OS X (and Windows too), try Lazarus. It’s the open source IDE for Free Pascal (open source compiler) that aims to be Delphi compatible: http://www.lazarus.freepascal.org/

Regards,
Mick

Too bad Delphi couldn’t live up to its name and show you the future of unemployability.

I agree with you to a point, but have also found that many languages that try to build a bridge across the pit of despair often times lead you to a new pit. Take C# for example.

I used to code a lot of C++, and only made use of heap allocation when it was necessary. This had the advantage of limiting my chances of forgetting a new, or double deleting. C# solved the problem of memory leaks by putting nearly everything on the heap, and replacing delete with Dispose.

So now instead of deleting memory, I have to remember to dispose of it. Granted there is Using… and the GC will eventually call a destructor which hopefully calls Dispose for me… but there is a larger problem. Object!

Everything is a freaking object now! So now instead of having a memory leak to deal with, I have tripled my chances of having an invalid cast exception, or a null reference exception because things I might normally declare on the stack are now allocated on the heap. I’ve traded one problem for another.

I suppose you could argue that this is a case of replace slow failure with immediate failure, but it doesn’t seem like encouraging success to me.

“Bottom line. You either know the language or you don’t. If you know it, you’re a hire. If not, you write articles like this.”

Ouch, JM. I seek to find this magical company for which you work where programmers never make errors and bugs never find their way into a software release. I respect what you’re saying about how seasoned, solid programmers are less likely to make gross errors, but once code gets big and complicated and good, solid, but-totally-green-to-your-app programmers get introduced to it… all bets are off. I think that’s what Jeff is talking about.

While not yet mainstream, some languages are just a little better at herding their programmers away from disaster.

"BUT the fact remains, that C++ is still the most used language and almost every big, serious $ successful Software product is written in C++. You won’t see anybody do Office.NET or Photoshop in Java. Just look at this small list of Apps written in C++. "

Most of these application predate Java and .Net/C# and rewriting them with Java/C# would require spending millions if not billions of dollars without immediate benefits.

Most new applications are web applications and only very few of them are written in C++, most are written in Java/ASP.NET/PHP/Ruby/…

I’m mostly a C++ coder, but I also have a healthy share of C# work. While there are definitely a lot of parts that I like about C#, I’ve spent more time cursing C# than anything.

For example, the GC. Yes, it does a good job most of the time, but when it doesn’t it REALLY doesn’t. And, there is no alternative. If you find that the GC isnt handling memory well enough for you, too bad. Rewriting your app in C++ is your only alternative, but by the time you find you have serious memory problems it’s too late for a rewrite. So you pepper the code with GC.Collect()s, it gets even slower, but it “works”.

And C# isnt without it’s own “pit of despair” - reflection. Reflection is absolutely great - unless you use it. It can make debugging and tracing through code a nightmare. But in the right hands, it’s an awesome tool.

I’ve also found like coding in C# is much more like coding against a black box. But that may just be my C++ side complaining. Even coding against Microsoft’s MFC you can still step through the MFC source if something crazy is happening. If something funny is happening in the .NET libraries you can only hope that Google has something to say about it.

I’m gonna have to agree with JM. I’m dismayed at the generation of “programmers” brought up on java or C# who have no idea how computers work at all.

stuff like .NET breeds bad programmers.

“If you use arrays instead of vectors or manually call new and delete instead of using RAII, you will have problems. Your code won’t even be exception safe.”

In reality every non trivial application will use several libraries/framework with their own mutually incompatible auto_ptr, string class, collection classes, exception classes
or OS APIs (written in plain C) and none of these techniques will actually work.