Code Snippets

edited March 2018 in Developing Processing

I think it would be nice to have a way to easily load and save snippets of code within the PDE. A folder in the Templates folder for Snippets could hold PDE files, each one being its own snippet of code. Maybe you could highlight some code in the PDE, right-click and save as a snippet. And you could load with the same right-click context menu. It is just an idea, but it would be maybe a nice way to organize bits of code.

Tagged:

Comments

  • Good idea!

  • @Bird -- is this something you would imagine being just a local folder, or being integrated with one of the code snippets web services / APIs?

  • edited March 2018

    @jeremydouglass I was thinking to a local folder, but a service or API would also be good. I have used Bitbucket for snippets, but not sure how to integrate it with Processing. It seems like the local folder option should be fairly simple to implement, if there is any interest, but I'm certainly not the one to do it.

  • but I'm certainly not the one to do it.

    Sure you are! Give it a try, you'd be surprised at what you can do in a weekend.

  • @jeremydouglass , this is a really good idea, something like the Add Library functionality that's easier to publish to and implement, functions/classes instead of libraries

  • @lmccandless -- the Add Tool list in Contributions manager seems like it could be a perfect way to test out something like this that could log in to an existing snippet webservice without needing to request it for the base application as Yet Another Feature.

    Remote snippets might have group applications too, like a team / class sharing of snippets.

  • It is unlikely the Processing development team would do this as part of the core functionality (busy, busy people) but implementing this feature as a Contributed Tool would be the way to go. Could start it off as a local repository then a personal on-line repository and then even perhaps a public on-line repository. :)

    Although code snippets are useful to show how something can be done I would be more interested in autonomous code units. By ACUs I mean self contained blocks of code that have no external dependencies e.g.

    • functions that only require the parameters and local variables to do their job (no accessing global variables)
    • classes whose state can only be altered by public access methods (requires attributes/fields to have `private` / `protected` or `public final` modifiers).
  • @quark Yes, a Contributed Tool would be an excellent way to implement this. And I also would be more interested in ACUs.

  • If using an existing snippet service it might be an interesting feature to check programmatically if something is an ACU or not, or allow filtering by is / is not ACU.

    Snippets are sometimes templates, and then having no restrictions on them makes sense -- for example, an example of a timed mousePressed code block that refers to a global variable. The snippet has to be integrated into code (or come with its own global variable line, which needs to be moved to the top of the code) -- but it might be beginner-friendly in a way that a fully encapsulated timer class would not be.

  • edited April 2018

    (or come with its own global variable line, which needs to be moved to the top of the code)

    B/c all ".pde" files are concatenated together as 1 ".java" file, global variables (actually PApplet fields) can be defined across any ".pde" file. :ar!

  • In fact the snippet could be anything the user wanted, including their shopping list ;)

    If the snippets were to be stored in a private repository then I see no reason to place limits on what the user can store in it and design the Tool accordingly. :)

  • @GoToLoop -- re:

    B/c all ".pde" files are concatenated together as 1 ".java" file, global variables (actually PApplet fields) can be defined across any ".pde" file.

    Interesting -- that is a really good point for if a snippet was loaded into a separate tab of PDE.

    I meant something a bit different. Say a beginner starts with a skeleton of a sketch, and then inserts a snippet. That snippet for example includes some simple mousePressed() timer code as well as a global variable. If the snippet is pasted in, clipboard-style -- say, pasting a mousePressed() snippet below draw() -- then the global variable line now appears below draw(). This has no impact on correctness, but is bad style for readability. The beginner would be encouraged to take the additional step of moving that global variable line up to the top of their code so that is readable.

  • edited April 2018

    Global variables right on top of a snippet's function aren't that bad. :-\"
    At least it is clear those global variables are related to that snippet. 8-|

  • edited April 2018

    At least it is clear those global variables are related to that snippet

    I agree with @jeremydouglass... even if it is not that bad I would prefer to encourage good coding practices.

    So I still don't wrap my head around the concept but still I find it interesting, even possible a GSOC task (?). So after you have several ACUs, there will be documentation from each of them, right? For instance, right now there are several libraries in Processing. Some of them are straight forward to figure out (name, common usage, etc) but other ones are obscure (but still useful). My question is, how would snippets differentiate from (small) libraries.

    Do snippest already exist in the forum that could be used as a demonstration? Would be this a good point to suggest snippet ideas? For instance, implementing the doubleClickEvent() could qualify as a snippet? @quark What about ACUs? (Yes, I could google it btw... just bringing some richness and needed details to the chat)

    being integrated with one of the code snippets web services / APIs

    @jeremydouglass Do you know of any webservices to consider as an example?

    Sure you are! Give it a try, you'd be surprised at what you can do in a weekend.

    @KevinWorkman @bird @quark @ everybody

    This could be a chance for some team building/bonding experience? I can see some amazing expertise here that could be funneled for something great[er]!

    ****EDITED

    Kf

  • edited April 2018

    In addition to interfacing with things like Github gists and GitLab snippets, here is a list of 18 snippets managers --

    https://www.slant.co/topics/7247/~code-snippets-manager

    See for example Lepton.

    (There used to be even more than there are now)

    Reusable code snippets tend to be a pro programming feature, so the interesting question for something like this in the context of Processing is whether this would be a very niche thing for a few power users or whether it would be addressing beginners and learners somehow and useful to many -- provided for tutorials and examples, or made available by teachers, etc. etc.

  • I can see snippets in modular teaching scenarios. I don't have experience myself, but talking to a facilitator in the local library, he says one of the challenges when teaching teens is the poor typing skills. Thinking about modular teaching, you could have a snippet level 01 where you demonstrate some basics. In another session, you can have snippet version 2 where it assumes level one was completed so it adds to it. And so on. The idea is that each snippet will be dependent on the previous one. You remove the typing part and you set to review the previous session and also from that one you can set to start a new session. A snippet could contain fields that needs to be fill out or specific instructions. Teachers could organize snippets in galleries so they could teach different languages and different levels.

    Kf

  • I checked the snippet manager. Thanks for sharing @jeremydouglass. If it gets implemented in Processing, I think it will require more work than what I anticipated. I mean, having snippets presented in the same way as the Dialog Box of Processing examples would not be too encouraging. After you accumulate >30 snippets, for instance, it will become hard to track. The manager that is shown above allows you to organize, pick and review them in matters of clicks. Neat concept nevertheless.

    Kf

Sign In or Register to comment.