# Best practice code generation

**URL:** <https://discourse.julialang.org/t/best-practice-code-generation/122384>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 7, 2024, 4:00pm UTC](https://discourse.julialang.org/t/best-practice-code-generation/122384 "2024-11-07T16:00:07Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 7, 2024, 6:02pm UTC](https://discourse.julialang.org/t/best-practice-code-generation/122384/6 "2024-11-07T18:02:54Z")

</div>

Why generate files at all? Julia has built-in metaprogramming where you can just generate the AST and eval it. (Modulo the usual caveat to [be wary of metaprogramming](https://discourse.julialang.org/t/how-to-warn-new-users-away-from-metaprogramming/35022).)

And you don’t have to do this ahead of time. You can do all the generation in the package code itself, e.g.

```julia
module Foo

include("generator.jl")
generate_and_eval_stuff()

...

end

```

This will only happen once while the package is being precompiled, so in practice the overhead of code generation is unlikely to be a problem.

---

_[View the full topic](https://discourse.julialang.org/t/best-practice-code-generation/122384)._
