Variable Names — Verbs and Nouns parsed from top Javascript libraries
By: Jack Siu
Tags: JavaScript, Github, NLP, Coding, Nodejs

Variable Names — Verbs and Nouns parsed from top Javascript libraries

A word cloud generated by parsed verbs

The problem

When an error message is thrown in the error log or finding a solution to “sort json array by key python”, it is likely that I can find the answer or at least some hints on the Internet. But when it comes to naming a variable, it tells me to name a meaningful, expressive, self-descriptive variable. The variable names need to follow the convention. In Javascript, variable and function names should be written in camelCase. I should use nouns for variable names and verbs for function names. And never use a meaningless name such as foo and bar (it turns out many JS libraries are using them, but only for unit test or benchmark)

I know these rules. But the problem is not that my variable names violate these rules. The problem is I cannot come up with a variable name which I am satisfied. And I think it is because I don’t have enough “coding vocabulary” in my mind. Therefore, I decided to learn more vocabulary from other developers and tried to categorise them into verbs, nouns, and adjectives.

If I have seen further, it is by standing upon the shoulders of giants — Isaac Newton

In the Javascript world, the giants are React, Angular, Express, and many other top libraries published in NPM.

Method

Step 1. Clone popular NPM libraries

Based on this list which listed the top Javascript library, 390 repositories of the libraries were cloned to local machine.

Step 2. Parse source code and convert it to Abstract Syntax Tree(AST)

Screenshot from Wiki about AST

With Acorn(A tiny, fast JavaScript parser written in JavaScript), I parsed the .js and .ts files and got an abstract syntax tree object. From Wiki, Abstract Syntax Tree is a tree representation of the abstract syntactic structure of source code. For this task, the most important thing was to retrieve variable names and function names from the tree. A rule was added to exclude files under /build, /node_modules, /dist to avoid parsing minified code.

Step 3. Find function name and variable name

There were many node types in Abstract Syntax Tree. Two node types, namely, VariableDeclaration and FunctionDeclaration were extracted. Within VariableDeclaration, two special init types FunctionExpression and ArrowFunctionExpression were identified and treated as function names instead of variable names. The graph below illustrates the way I classified “Function name” and “Variable name”.

Step 4. Extract verbs/nouns/adjectives (with assumption)

After getting the Variable Name and Function Name, I split the name into words and identified verb, noun and adjective with the following assumptions

verb — first word in Function Name e.g. getAge, checkIsAdult, restore noun — last word in Variable Name. e.g. resolveValue, data adjective — first word in Variable Name which can be split into more than one words, e.g. isAdult, expectedJson

While dealing with nouns, I singularise the words and converted all characters to lowercase so that “Name”, “Names”, “name” and “names” are treated as the same word.

Result

A word cloud generated by parsed verbs

A word cloud generated by parsed nouns

A word cloud generated by parsed adjectives

Unsurprisingly, “get” is the most used verb. I also got some other familiar verbs like create, read, update, delete, and extract, transform, load.

Based on the result data, I built a small web app called Variable Name Dictionary to show the most used words with variable name example parsed from the libraries. Raw data in json format is available for download there.

What’s next

Having a list of words can expand my “coding vocabulary”, which is helpful when I don’t have any idea to name a variable. But more often, I already have a vague idea of what the variable name should be. For example, I may already think of a function name “getTotalPrice”, but later find out that maybe calculateTotalPrice can describe the function more accurately.

If I can calculate the similarity of different verbs and adjectives, it helps to refine the function name. I am checking some Python libraries like Gensim and NLTK to see whether I can get the semantic similarity(in coding context) between two verbs. Please leave a comment if you have any suggestions or ideas of getting semantic similarity.