Sunday, December 02, 2007

Metaprogramming and conjuring spells

"A computational process is indeed much like a sorcerer's idea of a spirit. It cannot be seen or touched. It is not composed of matter at all. However, it is very real. It can perform intellectual work. It can answer questions. It can affect the world by disbursing money at a bank or by controlling a robot arm in a factory. The programs we use to conjure processes are like a sorcerer's spells. They are carefully composed from symbolic expressions in arcane and esoteric programming languages that prescribe the tasks we want our processes to perform."
- Abelson and Sussman — SICP

Among the plethora of metaphors that plague our field, I find computing as incantation of spells one of the least annoying. Some fellow with a long beard writes odd-looking prose that will later, through some magical process, turn lead into gold or help vanquish some inept demon. Man, that show sucked, I don't know why anyone would watch that shit, specially the reruns Tuesday to Saturday at 04AM and Monday to Friday at 12PM on channel 49. Well, anyway, what's interesting about the analogy is that it shows the dual nature of software: it is both a magical process and a bunch of scribbled lines. To mix metaphors a bit, we can say that software is, at the same time, a machine and the specification for that machine.

This is what makes metaprogramming possible and, also, what makes it unnecessary. By the way, when I write "metaprogramming", I'm specifically thinking about mutable meta-structures, changing the tires while the car is running metaprogramming, not mere introspection. Throwing away that mundane car analogy and getting back to our wizardry metaphor, metaprogramming would be like a spell that modifies itself while being casted. This begs the question, beyond fodder for bad TV Show scripts, what would this be useful for? My answer: for very little, because if the wizard programmer already has all the information needed for the metaprogram, then he might as well just program it... To make this a little less abstract, take a simple example of Ruby metaprogramming:
01  class DrunkenActress
02 attr_writer :blood_alcohol_level
03 end
05 shannen_doherty =
06 shannen_doherty.blood_alcohol_level = 0.13
By the way, I'm not a Rubyist, so please let me know if the example above is wrong. The only line with meta-stuff is line 2, where a method named "attr_writer" is being called with an argument of :blood_alcohol_level. This call will change the DrunkenActress class definition to add accessor methods for the blood_alcohol_level attribute. We can see that it worked in line 6, where we call the newly defined setter.

But the programmer obviously already knows that the DunkenActress class needs to define a blood_alcohol_level attribute, so we see that meta-stuff is only applied here to save a few keystrokes. And that is not a bad motivation in itself, more concise code often is easier to understand. Then again, there are other ways to eliminate this kind of boilerplate without recursing to runtime metaprogramming, such as macros or even built-in support for common idioms (in this case, properties support like C# or Scala).

There may be instances where the cleanest way to react to information available only in runtime is trough some metaprogramming facility, but I have yet to encounter them. Coming back to Rubyland, Active Record is often touted as a poster child for runtime metaprogramming, as it extracts metadata from the database to represent table columns as attributes in a class. But those attributes will be accessed by code that some programmer will write — and that means, again, that the information consumed by the metaprogram will need to be available in development time. And indeed it is, in the database. So ActiveRecord metaprogramming facilities are just means to delegate the definition of some static structure to an external store, with no real dynamicity involved. If it were not so, this kind of thing would be impossible. Also note that recent Rails projects probably use Migrations to specify schema info in yet another static format.

To summarize, runtime mutable metaprogramming is like that bad purple translucent special effect, it is flashy, but ultimately useless. Anyway, that's my current thinking in the matter, but I still need to read more on staging.

[EDIT: corrected a mistake relating to the code sample]


Unknown said...

Mywatchhasrundown, can replace it General quartz, electronic watches need to replica watches uk provide power battery, battery life is generally around 1-2. When your watch appears Hands tremble, slow down, stop and go and other abnormal phenomenon, it is necessary to consider the replacement of breitling replica the battery, the button battery is not expensive, but open the watch is a technology live, not It is recommended to disassemble the watch and replace it, preferably to a professional repair table. Attention, watch the power exhaust should be replaced or removed as soon as possible, so as not to damage the battery leakage movement. How long is the watch life The service life of the watch has not been able to use the exact digital representation. Generally speaking, the life of the watch and the wearer are properly used and guaranteed.Watch related. For example, Peter Pace wear watches a bath, watch the appearance of dirty, watchband length discomfort, bad environment, serious impact, will make the watch is easy to damage, shorten the Service life。

mary Brown said...

Great Article. Thank you for sharing! Really an awesome post for every one.
Project Centers in Chennai

JavaScript Training in Chennai

Final Year Project Domains for IT

JavaScript Training in Chennai

john said...

فرزاد فرزین
علی یاسینی
شهاب مظفری
احسان خواجه امیری

hamed221 said...

دانلود فیلم ماجرای نیمروز 2 رد خون

saim said...

user friendliness and appearance. I must say you’ve done a excellent job with this. Also, the blog loads super fast for me on Internet explorer. Excellent

Isfahan Egyptian Desert said...

دیدنی های کویر مصر

negin said...

تنگه ارغوان

James Tunker said...

Qdexi Technology provides a full complement of web2.0 design and development services in USA. Our experts provide solutions for both web design and web developers platforms.
Email id:-

Cyber Security said...

Nice Information Your first-class knowledge of this great job can become a suitable foundation for these people. I did some research on the subject and found that almost everyone will agree with your blog.
Cyber Security Course in Bangalore

Cyber Security Course said...

Writing in style and getting good compliments on the article is hard enough, to be honest, but you did it so calmly and with such a great feeling and got the job done. This item is owned with style and I give it a nice compliment. Better!
Cyber Security Training in Bangalore

Data Science Training said...

Really nice and interesting blog information shared was valuable and enjoyed reading this one. Keep posting. Thanks for sharing.
Data Science Training in Hyderabad

Data Science Course IND said...

I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
Data Science Course in India

Institute Programs said...

This blog is truly one of the best with unique information found valuable and resourceful.Very good work thank you.
Data Scientist Training in Hyderabad

Tech Institute said...

Top quality blog with unique content and information shared was valuable looking forward for next updated thank you
Ethical Hacking Course in Bangalore

Data Science Course IND said...

I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !!

I really liked each part and also bookmarked you for new information on your site.

Data Science Course in India

data science course said...

I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
I am very happy to have seen your website and hope you have so many entertaining times reading here. Thanks again for all the details.
Data Science Course Syllabus

Cyber Security Course in Bangalore said...

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!

Cyber Security Course in Bangalore

Python Training in Bangalore said...

We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work.

Python Training in Bangalore

Mallela said...

Thanks for posting the best information and the blog is very informative.Data science course in Faridabad

Mallela said...

Thanks for posting the best information and the blog is very helpful.Data science course in Varanasi

Pallavi reddy said...

i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
best data science courses in bangalore