The main cost of Drafts script actions at the moment is the rather slow (and slightly feed-back impoverished) cycle of testing and adjusting:
- Several levels of tapping to select and edit a script action, then
- ditto to read any console.log or error output, and
- often limited or absent error information.
An important culprit here is the use of the older var rather than the more current let.
Among other advantages, the newer let gives a beginner helpful error information, where the old legacy var gives none.
Imagine, for example, that we accidentally reuse a variable name.
var a = '123e4567-e89b-12d3-a456-426655440000'; // then, somewhere lower down ... var a = 42; // We've now lost the UUID, but we get no warning or explanation at all
- var just destroys the value in the first assignment, and gives us no warning of what has happened.
let is very much more helpful:
- it triggers a red ‘!!’ warning in the code editor, as soon as we make the mistake, and
- if we run the code, it gives us an explanatory error message, with line number:
SyntaxError: Cannot declare a let variable twice: "a"
Line number: 9
let a = "123e4567-e89b-12d3-a456-426655440000"; // then, somewhere lower down ... // We get an instant red !! warning in the editor if we now type this: let a = 42; // and an explanatory error message with line number if we run it.
@agiletortoise I’m not sure whether the use of var in the API documentation examples is a legacy thing (compatibility with an earlier Drafts/JS ?) but perhaps it would be helpful to do a (var -> let) search replace ? It would allow beginners to immediately get more help from the Drafts code editor, and from the error panel.
(In nearly all the examples, const would, of course also work, and might have advantages (we can declare an object as a const, and still update its properties – we just can’t change the reference to a different object), but perhaps simpler for beginners not to have to deal with that choice too soon ?)
- const is the ideal default (executes faster, encourages avoidance of unnecessary mutation)
- let is needed for any real mutation (not needed for any object property updates)
Both give helpful error messages, and editor warnings, where var gives nothing at all, and just leaves us in the dark.