I started being a developer using Java and got used to powerful IDEs which support me when writing my source code. Being more precise, e.g. when trying to use a function that does not exist in Eclipse, I immediately get an error when typing this, without running my application. How does Eclipse does this? By pre-compiling my code?
Nowadays developing JavaScript or Node.js, I still don't understand why such features are not existing in an IDE using an interpreted programming language. I understand that an interpreter analyzes the code during execution. But why is it not possible e.g. to build a plugin for an IDE as Visual Studio Code which pre-analyses my code while writing it? I would really appreciate getting an error when trying to call a function in JS that does not exist, for example. Also a linter does not help in such cases.
Anyone can support my understanding with this? Thank you a lot :)
An IDE can analyse your code, and it could warn you that you have appear not to have defined a function which you appear to be calling. But in many cases, you might want to tell the IDE to take a hike and stop bugging you, because the function definitely exists. It's just not visible to the IDE.
C++ and Java don't permit that case. If you call a function by name, the compiler must be able to see that function. (In C++, the compiler doesn't really know whether or not that function will eventually be included in your project; it may be that all it knows is whether you have included a header which declares a prototype for the function. But you must have done at least that, and so the compiler -- and the IDE -- have good reason to assume that you intend that function to be part of the executable.
Java is even stricter. If the compiler can't see the other class modules you're using, it won't compile your function. (With C++ you could compile your function (as long as you have the header with the functions it is calling) before you even write the functions being used, although you cannot create the executable until you have all the necessary pieces available.)
But Javascript doesn't require declarations. Javascript doesn't need to know what the argument or result types of a function you're calling are. So it doesn't need to force you to provide that information, ever. When you eventually get around to running the program, the JS interpreter will have to find the functions you are calling, but they don't have to be in the text of the program you wrote. They could be in a different file which you dynamically added to the execution environment, or they could be in a web page into which your Javascript program has been loaded. Or they could even be created on the fly and added to the execution environment with eval
. (Or in other ways.)