Why is C++ so popular?

I’ve never quite understand the attraction of C++ given its (growing) complexity and the many ways to get into trouble. Here is another reason, its verbosity. The following is a fragment of code from an open source library which will remain nameless. The purpose of this code is to read an XML file and validate using a schema.

In C++:

try
{
   xml_schema::properties props; // to store information on the XSD for validation purposes
   props.schema_location ("http://myurl.org/libX/pd/0.1", "../resources/X.xsd");

   auto_ptr myXobject ( libX::pd_0_1::X_ (Xmlfile, 0, props) );
   // [..] do something with myXobject
}
   // validation errors result in an exception being thrown
   catch (const xml_schema::exception& e)
{
   cerr << "X-ML parsing error: " << e << endl;
}

The equivalent in Java is:

MyObj obj = ObjUtil.readFromFile(Xmlfile, "../resources/X.xsd");

and the equivalent in C:

MyObj* obj = readFromFile (Xmlfile, "../resources/X.xsd", &error);
if (!error) {
   // [..] do something with obj
   free (obj);
} else
   printf ("%s", getLastError());
This entry was posted in Programming, Software. Bookmark the permalink.

2 Responses to Why is C++ so popular?

  1. hsauro says:

    You missed the point. I can see the difference, one person used 6 lines with lots of punctuation and the other one didn’t. C++ is riddled with excessive punctuation and additions in an attempt to make it relevant and modern. Granted the catch statement was unfair to include, but that still leaves three unreadable lines:

    xml_schema::properties props;
    props.schema_location (“http://myurl.org/libX/pd/0.1”, “../resources/X.xsd”);
    auto_ptr myXobject ( libX::pd_0_1::X_ (Xmlfile, 0, props) );

    If the Java one were expanded to include the innards I am still certain it would be more readable than the C++ version. I can’t see how you can claim that C++ is more readable than Java. Even the C version is easier on the eyes than the C++ version.

    As for your last comment, that didn’t really add anything useful did it.

  2. facepalm says:

    I could write an XML API for C++ that looks exactly like the Java one. What you’re comparing is APIs instead of languages.

    If you can’t see the difference then honestly I think you’re new to (or really bad at) programming (also the error handling that is skipped in Java but not in C++, etc).

Leave a Reply

Your email address will not be published.