-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Allow object literals for 2D-Shape functions in additional to positional parameters #7609
Comments
Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you! |
This proposal makes p5.js more beginner friendly and improves readability. especially for new users unfamiliar with function parameters. Should we add validation when using object literals? For example, if someone forgets a required key |
For missing parameter key we can issue an error, similar to what's done now.
--something like this for your example:
|
i agree. the parameters are hard to recognize and remember for new p5 users. using object literals can improve the readability. |
Thanks for the idea! I've removed the 2.0 phrasing from the title as no API extensions are considered for the upcoming major release (see timeline). After this, new features will be considered for p5.js 2.1 (or later minor releases, like 2.2 and 2.3). Except bugfixes (or similar exceptions), new features will not be considered for p5.js 1.x (even though it will continue to be supported and available until summer 2026). For all those reading: The prioritization of a feature, especially API extension, is not based on popularity directly, but if you want to see this implemented, please do comment! Besides popularity: it matters what the benefits/drawbacks of adding the feature, and the benefits/drawbacks of not adding it. @GregStanton also has a great set of API extension criteria:
In this case, I think although it improves beginner-friendliness for people who prefer to use objects, I think it's important to make the case that this approach wouldn't add new challenges for maintainability (on the code side), readability/consistency/predictability (why are there multiple ways to call basic functions? which is better?), extension/economy (what about when new users, using objects in params like this, start to use add-on libraries that use established style? It could be an unnecessarily confusing transition.) I'm open to a discussion here, but many (maybe most!) users who are starting with p5.js are new to programming and JavaScript generally, so they may not be comfortable with objects. |
this one is a tough call for me, and it's about balancing readability and writability. As a code reader, I'd rather read objects, since the keys basically act as labels. As a code writer, it's extra overhead and syntax. Here are some of the design principles I've been using so far for 2.0, which attempt to address some of the same underlying problems:
I'm not sure yet if this handles cases like arc, as mentioned in the issue, since it has a large number of required parameters, which don't have a natural order to them like x,y,z parameters. For that, it might make sense to move to object parameters for at least some of them in the future? For cases like line and triangle, we kind of get the chunking I described by using beginShape/vertex, so I'm also not sure where that leaves us for the all-in-one variants. |
@kens0 re your comment:
This is ment to be a new user friendly optional syntax. New users don't have to be comfortable with objects to benefit from this proposal. It's an alternative to the existing overload of parameters which can be confusing
note: a new user has to understand [h], [tl], etc.. is not valid javascript. Migrating to object literal syntax summary could look like this:
|
@ksen0 I disagree with new title: "Allow object literals for functions instead of parameters" |
Hi all! Thanks @jht9629-nyu for sharing your proposal! I'm really glad to see this issue because I think it's a good starting point for a discussion of object-literal parameters in general. This is a big and important topic. A lot of good points have already been made here. Before I add anything, it might be helpful to figure out how to approach a topic as large as this one. I'll start by outlining the breadth and depth of the issue, as I see it, and then I'll propose some possible next steps. BreadthOne reason for a wider discussion is that a change to one part of the API invariably affects other parts: users who become accustomed to using object literals in one area of the API will naturally expect them to be supported in related areas. For example, if we support object literals for 2D primitives, users will almost certainly expect that we support object literals for 3D primitives. If we don't, we'd introduce inconsistencies and hurt predictability. If the issue ended there, we might not need a larger discussion, but there's a reasonable chance that further review would reveal a much larger scope. More generally, discussions of the pros and cons of object-literal syntax tend to recur in a variety of areas. Here are just a few examples where the trade-offs of this syntax played a role:
The last issue is a good example for a couple of reasons. First, I think it was mostly tabled due to a lack of clarity about the pros and cons. Second, I was able to quickly come up with an alternative solution that might be more consistent with other parts of the p5 API, but only because I wasn't viewing that issue in isolation. A broader discussion of object literals, and a summary of the key points, could be a helpful resource for making decisions in these kinds of situations. DepthHere, I'll return to 2D primitives, to indicate some of the complexities involved. I'm about to make a big list of problems, so before I do that... I want to acknowledge that the contributors who worked on this area moved p5 forward. It's just that this area is in need of some attention. A sample of problems to fix before we consider extending the APIIn its current state, this may be one of the most problematic areas of the p5 API, so I think @jht9629-nyu is right to propose improvements of some kind. However, I think it'd make sense to resolve the existing bugs and inconsistencies before we consider adding overloads; otherwise, we'd just propagate the bugs to new parts of the API. For now, I'll share an excerpt of a comment I made on another issue, which contains a few examples of the problems (I've edited it lightly):
Unfortunately, some of these issues may require breaking changes to fix, including the bugs. This is an extreme scenario where the API actually makes it impossible for some features to work. Fortunately, I proposed an initial idea for fixing these bugs, and it seems unlikely the changes would affect a lot of users (if no one posted bug reports, then these features are probably underused). I'll quote @davepagurek on this, from a separate conversation:
Any thoughts, @ksen0? Of course, time is running out before the 2.0 release, but for a case like this, maybe there's a way to schedule a breaking change after the initial release, as long as it's before 2.0 becomes the default version in the Web editor? After the initial breaking change, there'd be more time to address the other problems. Otherwise, I suppose we could wait until a potential 3.0 release. No obvious one-size-fits-all solutionTL;DR When it comes to readability, If we can clean things up, then I think it'd make sense to consider how to make the API more readable, which appears to be the intent of the current issue. Parameter lists that contain a mixture of different kinds of data, like in A good rule of thumb for the maximum length of parameter lists is to use the limit on human working-memory capacity, which recent estimates suggest is (very roughly) four chunks of information. The p5.Vector objectsContinuing with the Object literals probably make less sense here, since we already have a built-in object with a user-friendly interface for representing exactly this kind of data. Moreover, using Even so, this is only a partial solution. It works well for Object literalsFor the other 2D primitives, object literals may be a better solution. We could try to ensure consistency by having each 2D primitive support primitive arguments, Another option might be to represent coordinates using Next stepsCurrently, object literals are used in the p5 API, but typically only in more advanced features. There doesn't seem to be a consistent guideline for when to use them, but Dave has already kicked off the discussion by sharing his own guidelines. I propose that after 2.0 is released, we pick up the conversation there, and then proceed in stages:
Note: For now, I'm assuming we'll have a little leeway to make breaking changes after 2.0 is released, in the extreme case I mentioned above (where existing features can't be fixed without an API change). If that's not the case, I can try to adjust this proposal. |
Thank you @GregStanton, @ksen0, @davepagurek for your detailed feedback. I'm new to p5 development process so all your comments are very helpful. I've changed title to emphasize the focus of this proposal is on the place where must p5 beginners start. I mistakenly had 2.0 in the original title thinking it was the best way to express what I'd find most useful in the next major upgrade based on my teaching experience. I plan to update the proposal with these important points:
Again I appreciate all the feedback and I'll be reviewing them all and updating this proposal overtime. |
function arc() Syntax
The text was updated successfully, but these errors were encountered: