Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FES: detect capitalization and spelling mistakes #4637

Closed
1 task done
akshay-99 opened this issue Jun 16, 2020 · 11 comments · Fixed by #4643
Closed
1 task done

FES: detect capitalization and spelling mistakes #4637

akshay-99 opened this issue Jun 16, 2020 · 11 comments · Fixed by #4643

Comments

@akshay-99
Copy link
Member

Most appropriate sub-area of p5.js?

  • Other (Friendly Errors)

I have seen that new programmers often take time to register the several naming conventions used in programming such as camelCase for identifier names, ALL_CAPS for constants, etc. And so capitalization mistakes are very common among them but they can spend way more time trying to debug it. For example one may call try to call createcanvas instead of createCanvas, Color instead of color and so on. These types of mistakes are easier to spot as the browser itself will show an error like createcanvas doesn't exist.
But mistakes in functions which are to be defined by the user such as setup, draw, mouseClicked, etc are harder to spot. For example

function Setup() {
  createCanvas(400, 400);
  background(200);
} 

This code snippet won't run as Setup has been defined when we needed setup. But there will be no error message displayed here and so it can take longer for a beginner to figure it out. Similarly

function setup() {
 ...
}
function mouseclicked(){
 ...
}

mouseclicked will not work but there is no error message.

Spelling mistakes can also be common for someone not that familiar with English, or there can be regional differences in spelling ( color vs colour )

I am thinking of using the FES to display a message whenever the user has made a capitalization or spelling mistake and show the nearest match using word distance algorithms. Something like
🌸 p5.js says: It looks like you have used mouseclicked. Did you mean mouseClicked ?
This won't run every time, but only when a spelling/capitalization mistake occurs. So there should be no negative effects on the performance as such. It will function similarly to helpForMisusedAtTopLevel

@limzykenneth
Copy link
Member

Sounds like a great idea to me. Implementation wise do you have an idea of how to do this?

@montoyamoraga
Copy link
Member

what about a spinoff of FAQ? like frequent code mistakes, super short?

  • in JavaScript, case matters. we use setup(), not Setup(), or SETUP(). JavaScript treats uppercase and lowercase as totally different characters.
  • for p5.js variables we use camelCase, such as createCanvas or mouseClicked. camelCase is writing two or more words, where the first one is lowercase and each new word starts with uppercase.

@akshay-99
Copy link
Member Author

Implementation wise do you have an idea of how to do this?

@limzykenneth
I have a rough flow in mind.
There are two types of mistakes that can occur.

  • One is when referencing a predefined p5 symbol like createCanvas, color, mouseX, etc. For this, the first step would be to build a dictionary of everything in p5 that can be referenced by the user. This is already done for misusedAtTopLevelCode so I think I can just reuse it. We set up an error listener and when a ReferenceError occurs like createcanvas is not defined, we identify the object in it (createcanvas in this case) and run it through a word distance search against the dictionary. If the returned minimum distance is below a certain threshold, we have a likely candidate.

  • The other type is when defining functions such as setup, draw etc. These get attached to either the window object in global mode, or the context passed in instance mode. We could then run through it and apply the word distance search. The dictionary used here is much smaller, only consisting of names that are to be defined by the user.

I am not yet sure about the exact specifications of the word distance algorithm. I have been reading on various ways it can be implemented to find something which fits here. I can also set it up so that capitalization has a smaller distance as compared to letter differences.

what about a spinoff of FAQ? like frequent code mistakes, super short?

@montoyamoraga Yes! We could also add this to the wiki I think.

@montoyamoraga
Copy link
Member

@akshay-99 that sounds awesome!

yeah i have been thinking about ways to have less people post issues here when they are having trouble with their code, i am thinking this FAQ lisit could live both on p5.js website and be referenced when people open any new issue :)

@limzykenneth
Copy link
Member

@akshay-99 I don't think I understand the second solution well but the first solution seems like a good enough avenue to start exploring. A good word distance algorithm can be rather complicated to setup as there are many many ways a typo can occur, what I would suggest is to start simple with just a tolowerCase() comparison and use it to judge the performance in terms of usefulness to the user.

@stalgiag
Copy link
Contributor

So excited for this! This could be a very powerful improvement to FES.

Just to echo @limzykenneth that a toLowerCase() comparison would be a great start. Then it might be a good idea to try out small npm packages (like this one?) for more granular word distance calculations to see how performance and package size is impacted.

Also, this is less important and maybe outside of scope, but I wander if you could easily do this same comparison for undefined variables that are close to user-defined variables.

@akshay-99
Copy link
Member Author

Thanks for the suggestions everyone! I will try out these things and see the results.

Also, this is less important and maybe outside of scope, but I wander if you could easily do this same comparison for undefined variables that are close to user-defined variables.

@stalgiag Are you talking about variables which are defined by the user as a part of their sketch but may not be related to any p5 functionality?

@stalgiag
Copy link
Contributor

@akshay-99 yes, just curious if the same solution for p5 functions could be easily expanded to help with a sketch like this:

let myX = 50, myY = 10;

function setup() {
  createCanvas(100, 100);
  rect(myx, myY, 10); // FES will say variable undefined, which is probably enough of a clue
}

This becomes a bit more of a fuzzy discussion about the scope of the FES and I don't think this functionality needs to be addressed. This was more of a passing consideration that I just wanted to think about in parallel to the work done here.

@akshay-99
Copy link
Member Author

akshay-99 commented Jun 16, 2020

@stalgiag The FES does not currently have this much insight about the sketch. I think this specific case would need to involve parsing the user's sketch, basically having one script ( p5 library) read the code in another script ( the sketch ) . I had come up with some ideas about this a few months back, about how the FES could use the parsed sketch to build more context for its messages, and how this could be done. But it would be a much larger task.

Also, when the sketch is run locally ( not on a server ), the execution environment in the browser will allow us to see read the code written inside functions in the sketch, but not the code at the top level in the sketch file. And since let and const do not imprint on the global object either, I can't think of a way to make the FES read let myX = 50, myY = 10;

@kjhollen
Copy link
Member

this is great. I will add that preLoad() vs. preload() is a common error I see when I am teaching, if you are looking for good test cases. :)

@stalgiag
Copy link
Contributor

@akshay-99 right - hadn't considered where we were catching things currently. Thanks for the breakdown!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants