0

We are using a cloud-based IDE to teach javascript programming to first-time coders. This IDE using jshint for linting, and allows us to override options via .jshintrc.

We are using the p5.js library in our curriculum. While p5 does many things that are unconventional style-wise, we've found it's still the best library to introduce students to javascript in an engaging and interactive way.

A typical p5.js "sketch" is a script that looks like this:

let screenWidth = 600; const screenHeight = 800; function setup() { // this function is called automatically by p5 createCanvas(screenWidth, screenHeight); } function draw() { // this function is called automatically every frame by p5 } 

With this script, jshint will throw warnings for setup() and draw() because they are defined and not used. However, the p5.js library "exports" these for us and uses the functions we've defined, so there's nothing wrong with the above script.

If I wanted to suppress this warning I could put /* exported: setup,draw */ somewhere in the file. However, this is a bit much to expect new learners to manage. There a dozens of other functions like this that p5 uses, so the learner must remember to add that function to the comment line each time. It adds a bit of cognitive load to have to explain why they need to do this.

The easiest solution we have right now is to simply disable the warning globally by setting unused to false in jshint. However, this isn't ideal because beginner students often misspell things while typing. For example, another such function is mouseMoved(). By defining this function in our script, p5 will call it automatically whenever the mouse is moved. However, if a student mistypes it as mousemoved() or MouseMoved(), it won't get executed, and they won't get any warning as to why (assuming we've disabled unused in jshint).

Is there a way to define these "exported" functions in the .jshintrc file? If so, we could simply put all the exported p5 functions in there without the student having to do this on their own.

1 Answer 1

0

I haven't used JSHint personally, but this looks promising: https://jshint.com/docs/options/#globals

Update

Sorry, I misunderstood your question. I though you were having issues with undefined variable references. It doesn't look like there is a way to configure JSHint to ignore certain unused variables by name. So the only solution may be as you point out, to disable this type of warning entirely:

https://jshint.com/docs/options/#unused

Alternately you could teach your students to use p5.js in instance mode. While this is more complicated for beginners, it is a better way to write a sketch when considering JavaScript best practices. However, this will not necessarily alleviate the issue of mistyped callback function names. It would be neat if the p5.js friendly error system would automatically search for common misspellings of the built in callback functions.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that's kind of what I figured up to this point. I thought about using instance mode, but I feel the syntax is also a bit more advanced than I'd like.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.