- Distribution of poor quality and untested/under-tested applications.
- Poor, or even absent, documentation
- Code is not under version control.
- Absent or ineffective error trapping and exception handling.
So what is JSLint? The best place to find out is to check out the Read the instructions link on the JSLint web site. As the first line in the linked web page says, "
Here is a screenshot of this function after all the comments have been removed:
What does JSLint make of this code when it is pasted into the Source textbox? Actually, this code snippet generates a surprising number of JSLint errors and warnings.
All the default settings in JSLint were used for this example.
The function is the same as before in terms of what it does but to get from the original to the JSLint-compliant version, the following changes were required:
- Add /*global SpreadsheetApp: false, Browser: false */
- Add ‘use strict’;
- Remove space between function name “listSheets” and the empty parentheses.
- Indentation for all lines within the function body is re-set from 3 to four spaces.
- Use a single var pattern and move variable declarations out of the for loop to the top of the function.
- Replace the increment ++ with += in the for loop.
- Remove the spaces immediately after the opening parenthesis and before the closing parenthesis in the if statement.
- The "'use strict';" was added to ECMAScript version 5 and recognized by Google App Script. Its use was recommended in an earlier blog entry. It can be placed outside the function at the top of the script or within a function, when placed within the function, it only applies to that function's code.
- JSLint is quick to pounce on any indentation discrepancies.
- The first step is required so that JSLint does not regard these Google App Script objects like "SpreadsheetApp" as undeclared global variables. In the JSLint documentation, it states "Some globals can be predefined for you.", the first line in the JSLint version predefines the global Google Apps Scripting objects SpreadsheetApp and Browser in order that JSLint will ignore them. The "false" means that assignment to this variable is not allowed .
- using three rather than the default four spaces for indentation.
- JSLint's rejection of the increment (++) operator may be surprising but Crockford has pointed out that it can lead to problems so I'll drop it and the decrement operator in future.
The two code versions used here have been posted to GitHub link