3 ms·
I always write my own code generators. It’s fun, it doesn’t take that much work, and the results are exactly what I want and need. I have developed business app
by mbrodersen 4y ago
I always write my own code generators. It’s fun, it doesn’t take that much work, and the results are exactly what I want and need. I have developed business applications where 80% of the code was auto generated. All I had to write was the core business logic and a bit of special case UI code. Code generation is a real super power.
- whiskey14 4y agoThis sounds really interesting. Do you have any examples on GitHub that you could share?
- mbrodersen 4y agoUnfortunately not. It is all proprietary. However the key idea is simple enough: specify your domain model using JSON or your own parsed language (what I do). Read the specification and generate C++/C#/Java/Typescript/… code. That’s it. There are many examples out there you can look at. Google buffers for example. The generator I implemented at work started out doing something like Google buffers 5 years before Google buffers became a thing. But it has then been expanded massively by colleagues to do so much more. It has saved man years of programming work and has removed a lot of the boring repetitive work. I think that is why it was become so popular among colleagues. It is the first thing they install when starting a new project.
- jollyllama 4y agoI'm conflicted about this. I've worked with code generators built by coworkers with a great many years of experience. There are two key parts of the learning curve when approaching this situation. First, learning to use the code generator for simple tasks as intended. Second, extending or modifying it as needed to deal with bugs or unanticipated use cases. It can take a couple of years to get to that point, and it's difficult because a bespoke code generator won't have the resources that an open source, widely adopted library will. Also, you'll be responsible for building whatever connectors you need, as opposed to more popular libraries making plugins or expanding their capabilities. That said, when done right, they can be extremely powerful and well suited to their particular use cases.
- mbrodersen 4y agoI introduced a code generator at work and it was quickly adapted and modified/upgraded by colleagues. It started out as a simple protocol generator (think Google buffers but 5 years before Google buffers became a thing) and was then expanded over the years by a number of people to handle a lot more (databases, model specifications etc.) generating C++, CMake, Typescript, Java, and C# code. There is no open source generator out there that can handle all the things this tool does. It has now become the first thing people install when starting new projects. I think the key to its success is that the code generator itself is really simple. So you can figure out how to improve it in no time. Also, the generated code is super pretty. It even includes comments explaining what the generated code does. This was a design choice by me to ensure that people trusted the generated code. Without it I don’t think the generator would have been successful. If people can’t understand the generated code then they don’t trust it and is less likely to use the generator.