PhiLho
Easier code reuse in Processing
Jun 28th , 2008, 1:11pm
I discovered Processing some months ago, but at start, I only tweaked some existing code. Then I started more ambitious stuff, and being a Java programmer, I found myself writing classes to encapsulate stuff, reuse code and ease animation. I finally found that I can split a sketch in several files, Java or PDE kind. Or write a library, to package in a jar file. The later is a bit heavy, particularly in the early stages of development, when the classes are not finalized. The former is frustrating: if I write a generic class and want to use it in several sketches, I have to copy it in each and every project (sketch). For example I made a class to draw a parametrized stylized heart, and I want to use in a static sketch (to show side by side various settings), to animated it (changing settings in real time), etc. Several different sketches, so I have to copy the Heart class in each. I am aware that Processing aims at beginners, which don't need advanced features like namespace/packages and nightmares like classpath... But beginners evolve and improve, and eventually need more flexibility and code reuse. It would be nice to support some optional simple form of library. My idea is to define a special library folder. For example, make a folder with an arbitrary name in the sketch folder (eg. $classes, using $ to reduce risk of clashing with user defined sketch name) and/or define in the settings where such folder should go. There, you could define class files (.java and .pde files) at the root or in sub-folders, thus allowing kind of packages. Then in a .pde file, we could define at the start something like: include shapes.nonabstract.Heart; If in shapes/nonabstract/ folder we have Heart.java, it would be included outside of the Processing class. If we have Heart.pde, it would be included in the class. (Or always in the class, if it makes more sense.) It would need a bit more pre-processing: parsing the path, managing the imports of the library class if any, to move them at the start of the main .pde file, etc. But it might be worth the effort.