Code Generation Tips: Output code to a temporary directory
There are lots of different ways to use Code Generation in your project. Below is the top five reasons why you might want to output your code to a temporary directory.
- Cut and Paste the code you want
- Create code snippets
- Avoid overwriting your existing code
- Quickly test an idea
- Add new features to a legacy code base
Cut and Paste the code you want
Often when using code generators you don’t want all of the output code to be added to your project. By using a temporary directory and cutting and pasting the code, you can control what code is used in your project. Also this is still way more productive than writing the code manually.
Create code snippets
When programming you often need to perform similar actions on the code e.g. SQL Statement or Access an API over HTTP.
Some ideas for code snippets(a short piece of code)
- SQL Statements
- Serialisation code
- Exception Management Code
- Logging
- Object Validation
- etc
You need to use this code in your main project but using a code generator directly isn’t an option. By outputting this code to a temporary directory, you can then copy the code you require. The code generator file doesn’t even need to compile. It could just contain the code blocks you need. Then you just copy the code block you currently require e.g. SQL update statement.
Avoid overwriting your existing code
If you need to check or test that the code generator is correct ,output the code to a temporary directory. Then you can see if the code generator is working as expected before using with your project. Also always use source control on your project so it is easy to remove any unexpected changes!
Quickly test an idea
When you want to test
Add new features to a legacy code base
Often using a code generator on an existing project can be difficult. This is because there is a lot of existing code and you don’t want that code to be overwritten. By outputting the code generator to a temporary directory you are then in full control over what code is added and where it is added. enabling you to add new features quickly to a legacy code base