
Fear of Macros - ranit
http://www.greghendershott.com/fear-of-macros/
======
vinceguidry
After a few years of using and abusing Ruby's meta-programming, I've come to
understand it as an anti-pattern, you incur technical debt when you use it and
should refactor it away as soon as you know what you're trying to do with it.

It's one thing to enshrine it as a part of the language, like C++ templates
and the C preprocessor, because that forces you (hopefully!) to think about
things like maintainability and concern separation.

It's quite another to ad-hoc what amounts to a language extension to paper
over quick-and-dirty design. And there's not much middle ground between the
two. I'll ask myself the question, "do I want to extract this into a gem and
maintain it separately?" and run pretty quickly into the normal coding
equivalent of "am I making a game engine or a game?" My vision has to be
pretty darned awesome for the answer to be "both."

Meta-programming is great to have, having access to a credit card is way
better than not having one if you have the discipline to use it properly. It's
essentially why I stick with Ruby rather than move to a statically-typed
language, reflection and duck typing are tremendously useful when you don't
really know what it is you're building yet but need to execute quickly. You
can move at the speed of thought.

But I still consider it an anti-pattern. As the system matures I refactor it
into plain-old boring Ruby, easily replaceable with Go or whatever if the need
presents itself. I also like to reduce dependencies on libraries whenever
practical.

~~~
davexunit
Ruby's metaprogramming features are a far cry from Scheme's hygienic macro
system. By saying metaprogramming is an anti-pattern, we're saying that us
lowly programmers ought not to put on our language designer hat and let the
professionals take care of it. Without syntactic abstraction, we're at the
mercy of the language designers and waiting for the new language version to
include the new syntax we want. And what about the syntax we'd like to have
that isn't so generalizable to be included in a core language implementation?

For example, it's great to finally have a 'let' form in JavaScript, but any
one of us could've written it ages ago if there was a macro system, but
instead we've been writing (function(x, y) { ... })(z, w) which is the ugly
version of what 'let' essentially expands into.

I've written small embedded domain specific languages for coroutines and
functional reactive programming. They are small bits of syntactic sugar that
make talking about cooperative multi-tasking or values that change with time
much more natural, and I didn't have to invent a whole new programming
language to do it. A more well known example of an EDSL is miniKanren, a
language for relational/logic programming embedded in Scheme.

PG's "beating the averages" essay argues that you should be using the most
powerful language you can. The most powerful languages have macros. Good
macros allow you to build up your programming language to be tailor fit to
your problem domain. Let's not write off a powerful tool because it's possible
to write bad code with it.

~~~
jarcane
For that matter, I actually _have_ invented an entirely new programming
language mostly with just Racket macros. It's an incredibly powerful tool for
experimentation and research as well, something the PLT folks have been using
to their advantage for some time now.

I'm currently in the guts of several different candidates for building a basic
Lisp on Risc OS, and it's really struck home just how much of a lowering of
the barriers a really first-class metaprogramming system is when it comes to
language development.

Building everything from scratch takes time and often, a lot of repeat effort
on stuff that's already been written a dozen times before. Macro systems,
especially weapons-grade full-blown language development kits like what Racket
can do, are a pretty hard thing to argue with on that front.

~~~
agumonkey
Dick Gabriel said in a podcast that late 70s language research was mostly lisp
with macros. And when the PhD student was close to the finish line, he'd wrote
a concrete syntax so it looked like a real-er thing.

------
keithflower
This is a great article about Racket's advanced macro systems.

Greg has done great work, from Cakewalk days right up to the present. He is
generous, kind, and a wonderful teacher. Many of us working with Racket are
using his fantastic Emacs major mode for Racket:

[https://github.com/greghendershott/racket-
mode](https://github.com/greghendershott/racket-mode)

~~~
epidemian
Indeed, it's a great introduction to Lisp-style macros in general. The author
goes to great lengths explaining every concept that might be new to people
unfamiliar with Lisp (like the use of car and cdr functions and their
variations cadr, caddr, and so on).

Side note: i really like how the syntax highlighted code includes links to the
docs. You can quickly explore the definition and documentation of any function
you see. It would be great if the code snippets were themselves editable and
executable, enabling an even more exploratory approach to learning :)

~~~
59nadir
> Side note: i really like how the syntax highlighted code includes links to
> the docs. You can quickly explore the definition and documentation of any
> function you see.

This is a built-in feature of Scribble (a documentation language created in
Racket), so this is ingrained in the community. It's one of many things made
possible by Racket's stellar macro facilities.

------
mjcohen
Sure would be nice to have either the complete thing in one file or the "Next"
button at the end of each section instead of only at the beginning.

~~~
greghendershott
The all-in-one-page version is [http://www.greghendershott.com/fear-of-
macros/all.html](http://www.greghendershott.com/fear-of-macros/all.html)

Also, the source is [https://github.com/greghendershott/fear-of-
macros](https://github.com/greghendershott/fear-of-macros)

------
octatoan
What CMS/platform does the website use? Anyone?

~~~
octatoan
Found it. [http://docs.racket-lang.org/scribble/](http://docs.racket-
lang.org/scribble/)

Edit: Anyone know how to use Scribble to render math, preferably with MathJax?

~~~
soegaard
Copy these files:

[https://github.com/soegaard/mathbook/blob/master/mathbook/ma...](https://github.com/soegaard/mathbook/blob/master/mathbook/mathjax.rkt)

and

[https://github.com/soegaard/mathbook/blob/master/mathbook/de...](https://github.com/soegaard/mathbook/blob/master/mathbook/defaults.rkt)

