![]() ![]() ![]() Their primary purpose is to avoid the drudgery of writing any kind of parser. One of the classic examples is lex and yacc. Think "I see this pattern that's repeating all over my code, I can't find a way to refactor the code into something smaller and more reusable. Once again, in Ruby, you should look at ActiveRecord that is a great example of metaprogramming applied to ORM.ĭon't think "I want to apply metaprogramming, where could I apply it in my code ?". I think one good reason to use metaprogramming is to generalize some common pattern (pattern as something that repeats) that you have found in your code and that no other usual programming technics (inheritance, design patterns, etc.) can achieve.Īs said by Jordan, one typical use case is database handling and ORM (Object Relation Mapping). Don't use metaprogramming because "its cool" or "X said every developer should use it". Why should it be useful it I still have to code the generating code? Should I write a code able to generate different things depending on user input, so that I can reuse it over and over?įirst, metaprogramming is not a goal, but a tool. One book I enjoyed reading on the subject was Metaprogramming Ruby (if you know Ruby language)Įdit after the following question in comment : ![]() I don't like the idea of increasing my code base with something I can't control. Usually these files are not human readable and cannot be modified, so why bother with them. ![]() The little difference is that you don't end with source files that were automatically generated. This can be achieved with Lisp macros or Ruby dynamic program modification capabilities for example. I prefer going a bit further, and, instead of writing code that writes code, writing code that generates objects, methods, functions. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |