Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meta Programming, whats it good for?

So Meta Programming -- the idea that you can modify classes/objects at runtime, injecting new methods and properties. I know its good for framework development; been working with Grails, and that framework adds a bunch of methods to your classes at runtime. You have a name property on a User object, and bamm, you get a findByName method injected at runtime.

  1. Has my description completely described the concept?
  2. What else is it good for (specific examples) other than framework development?
like image 947
hvgotcodes Avatar asked Oct 15 '22 02:10

hvgotcodes


1 Answers

To me, meta-programming is "a program that writes programs".

Meta-programming is especially good for reuse, because it supports generalization: you can define a family of concepts that belong to a particular pattern. Then, through variability you can apply that concept in similar, but different scenarios.

The simplest example is Java's getters and setters as mentioned by @Sjoerd:

Both getter and setter follow a well-defined pattern: A getter returns a class member, and a setter sets a class member's value. Usually you build what it's called a template to allow application and reuse of that particular pattern. How a template works depends on the meta-programming/code generation approach being used.

If you want a getter or setter to behave in a slightly different way, you may add some parameters to your template. This is variability. For instance, if you want to add additional processing code when getting/setting, you may add a block of code as a variability parameter. Mixing custom code and generated code can be tricky. ABSE is currently the only MDSD approach that I know that natively supports custom code directly as a template parameter.

like image 198
Rui Curado Avatar answered Oct 20 '22 15:10

Rui Curado