C++ or JavaScript?

[Original blog post by Gil Moskowitz - 09/08/2015]

A software guy I know recently said that he prefers PHP to C++, something about C++ being "too fussy". I didn't comment at the time but the issue is an interesting one and is relevant to the haxTuple bug derby and xTuple application scripting.

The primary language for the xTuple ERP core is C++ while the language to use for xTuple extensions is JavaScript. The Developer Zone lists criteria for when you should script an extension and when you should modify the application core. It does not describe the trade-offs you make just from the language difference. 

JavaScript and C++ are closely related languages - isolated bits of code look alike to the casual reader. So let's look at a couple of lines:

var tmpid = _warehouse.id();  // JavaScript
int tmpid = _warehouse->id(); // C++

Both of these set a temporary variable tmpid to the internal ID number of a warehouse. Both call the id() method or function of the _warehouse object.

What changed to give the following code?

var tmpid = _warehouse.id;  // JavaScript
int tmpid = _warehouse->id; // C++

No parentheses after the id. What happens when we try these?

In the JavaScript case, the tmpid variable gets set to some strange value but the script keeps running as though nothing is wrong. According to the language, nothing is wrong - the script said to set tmpid to whatever _warehouse.id happens to be. That might be a null or undefined value, a bit of text like "id() function { internal }", or something else entirely, depending on how Qt's script engine represents that function. The only thing that's definite is that it won't be the numeric id of the warehouse.

The C++ line won't even compile, so you won't be able to run it to see the incorrect behavior. This is C++ being fussy. Instead of trying to do what you've asked, the language just complains that you made a mistake - something about type mismatches, casting pointers to int, or nonexistent variables - and does nothing.

Which is worse:

  • A script that's quick to write and tries its best to do what you've asked and sometimes gets it wrong?
  • A C++ compiler that says that you're not making any sense and makes you fix basic problems before it'll even try?

This is akin to driving a car with someone else reading the map. You approach an intersection and ask which way to go. Your JavaScript navigator tells you to go straight while your C++ navigator says the map doesn't show this intersection and won't answer the question.

Memory management is another area where the languages differ. JavaScript has built-in garbage collection while C++ requires manual memory management. Scripts can create objects at will and the script engine will take care of them when they're no longer needed. C++ code must explicitly delete objects when it's done with them to keep memory from filling up.

As an analogy, JavaScript has a housekeeping service that comes in every once in a while and empties the trash cans. Meanwhile, C++ makes you take out the trash yourself.

Does language choice matter? Often

Is one better than the other? Often

"Hey!!!!" you say, "That doesn't help!"

I know. That's why we have and use many programming languages. Developers have our favorites, of course, but the solutions to some problems are better expressed in certain languages than others. In addition, certain kinds of bug just cannot appear when you use certain languages and other kinds of bug are easy to introduce:

  • It's really hard to create a memory leak in a JavaScript program but trivial in C++.
  • It's tough to misuse variables in C++ and impossible to create them accidentally, but JavaScript can do either and keep going without complaint.
  • Lest you think this is particular to this family of languages, consider SQL: It takes about half as much typing to wipe out an entire table as a single row. On the other hand, either would require at least a score and possibly hundreds of lines of C++ working directly with the disk files, what with locking and all.

There's no substitute for understanding your programming language - its power and its shortcomings.