
Writing code that writes code – Hack Codegen - zeroonetwothree
https://code.facebook.com/posts/1624644147776541/writing-code-that-writes-code-with-hack-codegen/
======
PretzelFisch
In my experience when codegen shows up as a solution, you took a wrong turn
somewhere and are walking down the path of very brittle code that becomes a
nightmare to maintain over time. Several times the code generation libraries
don't get updated and your are left in a difficult spot when migrating your
code to a newer version of your platform. but hey maybe that's just a .net and
java thing.

~~~
fizx
There's sort of a survivorship bias in play here, where you only noticed the
brittle codegen being brittle.

~~~
fapjacks
At the risk of sounding obtuse, then, where is the robust codegen code?

~~~
nostrademons
On a continuum from "so robust you don't even consider it codegen" to "well,
some people use it": microcode, compilers, virtualization, visual interface
builders like XCode and Android Studio, dynamic HTML pages, ORMs, protobufs &
other schema definition languages, autoconf & friends, compile-to-C languages
like early versions of C++ and Go, ES6 transpilers like Traceur and Babel,
compile-to-JS languages like CoffeeScript. Probably others I've forgotten or
don't know about.

When it works, you forget that the only thing that computers understand is
their native instruction set, and _everything_ on top of that is built on some
form of codegen. When it doesn't, you curse out the programmer who left you a
pile of buggy scripts to maintain.

~~~
kazinator
Nice easter egg: _" autoconf & friends"_ buried in the middle of a paragraph
that appears to be about robust code generation.

~~~
nostrademons
"Well, some people use it." Particularly toward the end of the list, there'll
be more and more examples where people hate the code generation aspect (for
example, I've personally had bad experiences with any JS technology that
requires a build step). That doesn't change the fact that they're widely used;
autoconf, for example, was mandated for all Google-owned open-source C/C++
libraries, because despite its kludginess, it will build on virtually
everything.

------
FractalNerve
Cheers for the efforts, but that's a poor DSL or an atempt to create an
Aspect-oriented version of Hack. You've gorgeous tools at facebook like for
example 'pfff' [0] to play with ASTs, these could be used to build wonderful
things given enough time. I suppose the ultimative goal is to allow business
people to design workflows and let the tool generate the required code
automatically. (I guess that's what Flow Based Programming is trying to
achieve.) Kinda possibly with high-level specifications defined in languages
better suited to the task like Racket, Rebol/Red, Xtend/Scala etc.

There is an interesting presentation by ThoughtWorks – Neil Ford: Building
DSLs in Static and Dynamic Languages [1]

However I thought you might find this the EPFL / ETH Summer School on DSL
Design and Implementation very useful. They not only come with presentations,
but also code in Scala, Racket, Haskell et. al. I am currently working in a
huge project with about >30 people working on building an MDD (Model Driven
Development) platform for visually/declaratively writing things for the
systems we use internally. These will be able to create models out of code and
even fix API incompatibilities automatically. [2]

Excuse the Unicode, I got bored

⬬══════━━━━━━──⋯𝅖܅܅܅܅﴿⌁⏧⌁﴾܅܅܅܅𝅖⋯──━━━━━━═══════⬬

[0] [https://github.com/facebook/pfff](https://github.com/facebook/pfff)

[1] [http://www.intertech.com/resource/usergroup/Neal_Ford-
Buildi...](http://www.intertech.com/resource/usergroup/Neal_Ford-
Building_DSLs_in_Static_and_Dynamic_Languages-handouts.pdf)

[2] [http://vjovanov.github.io/dsldi-summer-
school/program.html](http://vjovanov.github.io/dsldi-summer-
school/program.html)

------
jeanetienne
Am I the only one to be bothered by the fact that both examples of
codegen/generated code shows that you have to write more codegen to generate
less actual code? In the first example there more than twice the amount of
code written in the codegen than in the generated code... I know they are
simple example but still, if I have to write more code for all the simple
getters/setters, it defeats the purpose of the codegen which is to "reduce
boilerplate". Am I missing something?

~~~
zeroonetwothree
Each codegen is run multiple times. Imagine you want to generate 100 similar
classes with similar code except for a small difference.

~~~
epaga
But that would be a code smell akin to copy-pasting code wouldn't it?

I'm having trouble seeing any real-world scenarios for this kind of codegen
but since it seems to be in wide use at Facebook, I must just be missing
something...

------
hnruss
"Notice that the manual section uses an ID to match it with the corresponding
section when regenerating the code so that is placed in the same location."

    
    
        public function getName(): string {
          /* BEGIN MANUAL SECTION User::geName */
    
    

Wonder how well that works if there's a typo in the section comment?

~~~
zeroonetwothree
I believe the comment is generated by the codegen, so it won't have a typo.

~~~
hnruss
That makes more sense, thanks.

------
EGreg
Real codegen would be like "Computer, make me a companion." "Here you go,
would you like to customize this hologram?"

