Code generation is one of the software development techniques I wanted to exploit for a long time. It is currently underused or is used in the wrong way. Examples are:
- Form designers
- ORMs
- Domain-specific languages
- Software factories
Drag and drop form designers generate code in behind for controls and widgets. ORMs generate POCO classes from table schema. Domain-specific language is a tiny programming language that is used to solve a specific problem. Software factories support model driven, pattern oriented approach.
My desired approach is a combination of all above but it avoids their weaknesses. Form designers and ORMs solve problems in one layer only, view and data, respectively. Domain-specific language cross cuts the layers but is specific to one problem domain. Software factories are predestined to become rigid and bloated behemoths. Also, they all hide generated code from developers which can’t be a good idea.
They are useful for developers who don’t have enough knowledge in the area so they use generators instead of learning to code. It’s useful at the beginning, but with disastrous effects later when their ignorance starts to creep out.
My approach is to generate code that the developer would write anyway, but without code duplication and repetitive writing. Every team could write a generator for their project to enforce their architectural decisions and coding conventions.
Code generator should generate:
- SQL statements
- DTO objects
- Business logic template
- Test templates
- Placeholders for user interface widgets
- Validation logic
Code generator should:
- Support Command-Query separation
- Avoid CRUD design
- Help with code maintenance