⬆️ ⬇️

The average identifier length in popular JavaScript libraries is 8.27 characters.



Identifiers longer than 15 are less common than single characters. The most common are 6-character names. Such results were obtained by Esprima, a parser for JavaScript written in JavaScript , after analyzing the source code of libraries such as jQuery, Prototype, MooTools, Underscore, and some others.



The five longest identifiers look like this:

prototype-1.7.0.0.jsSCRIPT_ELEMENT_REJECTS_TEXTNODE_APPENDING
prototype-1.7.0.0.jsMOUSEENTER_MOUSELEAVE_EVENTS_SUPPORTED
jquery-1.7.1.jssubtractsBorderForOverflowNotVisible
jquery.mobile-1.0.jsgetClosestElementWithVirtualBinding
prototype-1.7.0.0.jsHAS_EXTENDED_CREATE_ELEMENT_SYNTAX




With the help of such a script for Node.js, you can compare your style of naming variables and functions with classical samples for excessive verbosity or too harsh brevity:



 var fs = require('fs'), esprima = require('esprima'), files = process.argv.splice(2); files.forEach(function (filename) { var identifiers = {}, content = fs.readFileSync(filename, 'utf-8'), syntax = esprima.parse(content); JSON.stringify(syntax, function (key, value) { if (key === 'name' && typeof identifiers[value] === 'undefined') { identifiers[value] = value.length; } return value; }); for (var key in identifiers) { if (identifiers.hasOwnProperty(key)) { console.log(identifiers[key]); } } }); 


Run like this:

')

 node idlen.js /path/to/some/*.js | sort -n | uniq -c 




Source: https://habr.com/ru/post/143895/



All Articles