Reputation: 1544
As a Java developer, I am used to being able to see all relevant information out of the code completion of the IDE already. Here is an example of code autocompletion from the Eclipse IDE. For instance, you can see that the return type of the function contentEquals
is a boolean and it expect one parameter with type StringBuffer
. Additionally, if the JavaDoc is there, you even get a nice explanation of how the function is supposed to be used.
Now I am looking for something similar when coding with JavaScript. I started using Webstorm as I wanted an IDE with the more advanced features but when you look at the code autocompletion, it either seems rather poor or I'm not using it right.
Can someone help me figure it out?
Here is a more specific example:
Let's say, you want to get the user's position by using the navigator.geolocation. Let's assume you know as much as that it can be done by calling the getCurrentPosition() method.
You go ahead and type in your IDE (Webstorm in my case):
navigator.geolocation.getCurrentPosition()
Here is what the Webstorm IDE makes out of it:
Ok, it tells me there is one mandatory and a couple of optional parameters to call this method. Let's say, you want to call this with only the mandatory parameter, which is supposed to be a function. So far so good, but what kind of function? Is this function supposed to take parameters? Does it return anything? There's no hint about it.
I found in various examples on the web, that this callback function actually does take one parameter, so I go ahead and write it:
function printPosition(position) {
// do something
}
But here comes the next question: what type is this parameter position
? What can you do with it?
So I try the code autocompletion on it:
Whooot? It gives me all kinds of stuff, but not at all what I'm looking for. So I'm ending up with google again.
And here it is how it is supposed to look like:
console.log(position.coords.latitude + ' , ' + position.coords.longitude);
Honestly, I never would have guessed that this is how it's done only by looking at the autocompletion which is very annoying because I am used to being able to do that when coding in Java.
Can anyone relate to what I am experiencing? Am I missing something? Any help would be highly appreciated!
Upvotes: 7
Views: 2328
Reputation: 1961
I also face similar issue while writing automation code in JS. I found that- using lightweight editors like- Visual Studio Code, I can get the limited number of Auto-complete suggestions.
Although its not as good as IntelliJ/Eclipse based auto-complete for Java or VSTS based IntelliSense for C#.Net for that matter. But still, it goes near as instead of myriad number of options, we get a limited number of suggestions.
Also, I have found that with TypeScript experience is much improved.
Upvotes: 0
Reputation: 3244
Javascript is a dynamically, weakly typed language: the type is loose and can change at runtime, consequently, the IDE can't provide accurate completion, unless it can find hints under the form of code documentation (JSDoc).
You should give a try at Typescript: that's a Javascript compiler, from Microsoft, that adds strong typings (with a C#-like syntax). It also allows to write code using the most recent ECMA implementations. It adds consistent type check at compilation + into the IDE.
Put differently, that's a way to write modern Javascript (with the support of classes, generators, spread operators etc...), along with typings (types, generics, interfaces), and some Typescript specific features (that are likely to make it into the next ECMA versions, given Microsoft influence concerning these norms).
That's the Typescript compiler that cares about transforming all of this code into pure and backward compatible Javascript code, that can run in older browsers/platforms.
Have a look at the handbook to see how the syntax looks: https://www.typescriptlang.org/docs/handbook/advanced-types.html
The limitations:
And a last word: VSCode is also a good IDE for Typescript, less exhaustive than WebStorm but free.
Upvotes: 3
Reputation: 26527
In a nutshell, because JavaScript isn't a strongly typed language like Java, you can't get the super granular autocompletion like you get in Java, unless the developer has included comments that WebStorm (or any IDE) can interpret.
You can implement those comments using Google Closure Annotation or JSDoc3. Using those, you can manually specify comments that will give you autocompletion.
Without those though, WebStorm really is just doing it's best to guess at what it needs, and it's usually not able to guess too well (which is why you get that giant list of basically everything in your last screenshot).
With JavaScript, you'll just have to rely on documentation.
Upvotes: 4