VAST Tips – Compiling methods programmatically

I’ve been working on several projects over the several months and years where I’ve helped improving development work by establishing techniques and tools that have become common place in many new projects. Some of these had never been established in old Smalltalk projects because these were expected to be switched off within the next few months or years. Since there are some companies which didn’t switch off their Smalltalk projects and decided to continue the projects in Smalltalk, there’s a lot ot catch up with: QA had not been done for a few years, unit testing was never introduced and config management and packaging had become tasks that noone dared to do if the packaging wizard was out of the office…
Besides coaching people in how to manage their code (stream concepts) and getting techniques like unit testing and QA in place, we’ve often tried to make life easier by providing tools that help automate builds, lower the risk of errors and make tedious, repeating jobs easy and fast.
There are some techniques and tipps that can help making life a lot easier, and I’ll share a few here from time to time…

There are many situations in a development project in which you code methods that are basically not much more than a collection of metadata of some kind, like object-relational mapping information, some information needed for any kind of framework that simply sits inside a method that just returns a Collection of objects and such. Often such methods can be derived from existing information that is stored somewhere already or should be easily editable in a Browser or Workspace.

In such cases, compiling a method into a class could be extremely helpful: have the user type in some information and generate the code based on it.

In VAST (just like aby other Smalltalk) this is easy, once you know where to look.

To add an instance method to an existing  Class named MyClass you can use:

MyClass compile: 'testMethod
^OrderedCollection new'
notifying: Transcript
ifNewAddTo: MyApplication
categorizeIn: #('Great Tip from Joachim').

This will compile a method into the class MyClass in Application MyApplication. The method will be catgorized in all categories in the array handed over in categorizeIn: . Since an (Sub)Application is a class, you can simply use the MyApplication class name as a parameter to ifNewAddTo:.

In order for this to work, the class must already be defined or extended in MyApplication – either programmatically or by hand. VAST or better envy will take care of creating a scratch edition if necessary.
If anything goes wrong, errors and/or warnings will be logged to the Transcript, but you can also hand in any kind of Stream to collect messages on.

If you’re interested in our help for your Smalltalk project, please visit our website (only available in German…) and contact us to talk about more details.