2. Jakarta
What is Metaprogramming
is the writing of computer programs that write
or manipulate other program (or themselves) asโจ
their data, or that do part of the work at compile
time that otherwise be done at runtime
โ
writing code that writes code
3. Jakarta
The Purpose
To have super power!
โข More elegant code
โข Less boilerplate code
โข Hide the how, focus on what
โข Essential in framework building
4. Jakarta
Background
โข My first encounter: Prototype.js, jQuery
โข Experimenting with my own parsing (zparse) and angular
like framework foo.js (2007)
โข Then created chain.js batik etc. (2008-2010)
โข Now maintaining Merapi and its plugins (for YesBoss /
Kata.ai)
I love Metaprogramming
5. Jakarta
Uses in Real World
โข Almost every framework uses Meta
Programming in some way
โข Domain Specific Languages
โข Add functionalities without littering
main logic (logging, measurement,
etc.)
6. Jakarta
Two Flavours
Dynamic Language Features
Program can inspect itself using built-in features of the language.
Code Generation
Translate meta features into generated code in target language.
examples: jquery
spring (java)
rails (ruby)
merapi ;)
examples: QT (C++)
Co๏ฌeeScript
React
Babel / Typescript
25. Jakarta
Performance
โข Code Generation (compile/transpile):
โข No impact performance (however depends on
implementation)
โข Runtime (processed during initialization only)
โข Could lead to slower initialization (still ok in most cases)
โข Runtime (heavy use)
โข Be careful!
26. Jakarta
Conclusion
โข It is like doing black magic:
โข know what you are doing
โข can have side effects if not used properly
โข Performance Trade-Off
โข Unclear Program Flow
โข Devs not familiar with the core can get confused