Originally Posted by
GTDev
Since the introduction of QML, you can save vast amount of development time by only stepping into C++ for certain tasks that require it.
This is true to a certain extent, it depends on definitions of "require it".
Inexperienced developers often interpret it in the sense of "if performance requires it", forgetting about the much more common requirement of stability of code quality.
JavaScript is good for prototyping and demos, essentially code that needs to be written in a short time but won't have to be maintained.
Once code reaches the state of when it is expected to updated (fixed and/or new features) it becomes far more sensible to do it in C++.
There are far more tools available to analyze and check C++ code and a lot of JavaScript tooling is specific to its use in web browsers.
Even without special tools, the C++ compiler alone can prevent a lot of misery due to its far stricter handling of identifiers.
For example the following JavaScript code has a simple typo a C++ compiler would have generated an error for:
function add(param1, param2) {
var result = 0;
resut = param1 + param2;
return result;
}
console.log("add(1,2)=" + add(1, 2));
function add(param1, param2) {
var result = 0;
resut = param1 + param2;
return result;
}
console.log("add(1,2)=" + add(1, 2));
To copy to clipboard, switch view to plain text mode
A JavaScript engine will happy accept "resut" as a new local variable, leading to the rather surprising result that "1+2" is "0".
The equivalent C++ code
#include <iostream>
int add(int param1, int param2) {
int result = 0;
resut = param1 + param2;
return result;
}
int main() {
std::cout << "add(1, 2)=" << add(1, 2) << std::endl;
return 0;
}
#include <iostream>
int add(int param1, int param2) {
int result = 0;
resut = param1 + param2;
return result;
}
int main() {
std::cout << "add(1, 2)=" << add(1, 2) << std::endl;
return 0;
}
To copy to clipboard, switch view to plain text mode
leads to
/tmp/test.cpp: In function ‘int add(int, int)’:
/tmp/test.cpp:6:5: error: ‘resut’ was not declared in this scope
resut = param1 + param2;
^~~~~
/tmp/test.cpp:6:5: note: suggested alternative: ‘result’
resut = param1 + param2;
^~~~~
result
/tmp/test.cpp: In function ‘int add(int, int)’:
/tmp/test.cpp:6:5: error: ‘resut’ was not declared in this scope
resut = param1 + param2;
^~~~~
/tmp/test.cpp:6:5: note: suggested alternative: ‘result’
resut = param1 + param2;
^~~~~
result
To copy to clipboard, switch view to plain text mode
Cheers,
_
Bookmarks