DEV Community

Discussion on: Is Code Generation a Bad Idea? šŸ¤”

Collapse
 
fjones profile image
FJones

It really depends hugely on the use-case.

Generated code is great for APIs: Making sure that at least one end is generated from a specification makes communication a lot easier.

Generated code is also great for repetitive boilerplate. We wrote a simple PHPDoc-based code generator to expand DTOs and their Builders with optional aspects - child models that may or may not exist on the instance, and thus implement a trait and interface to type check against, for each combination of child models. So you'd have DTO A with aspects B and C, and there'd be resulting A, A+B, A+C, A+B+C DTOs. A beautiful idea for PHP7.4, making it clear on the class which features it supports. But horribly annoying to implement manually when there's six or seven aspects.

But there are so many use cases where code generation is used that are only really worth it if there's an out-of-the-box solution (e.g. project creation or migrations)