I have seen alot of frameworks that generate stub applications, like Ruby on Rails for example, where most of the code is used for business domain logic, but why do we have generated code still. Wouldn't a library be better?
A library will consist of common functions or classes that may be used as is in many applications. These frameworks do include libraries of code.
The generated code takes care of the scaffolding that you will need to write yourself regardless. Since most of it is pretty standard, it can be generated and the intention is you modify the pieces that you wish to.
With techniques like LINQ I see (almost) no need for the frameworks like NHibernate and similar solutions. When performance is a real issue generated stub applications could be faster than techniques that rely on techniques like reflection.
Another benefit of generated code is that it is compiled and thus less likely to cause errors at runtime.
There are both justified and unjustified cases of code generation. However proper code generation can result to the following benefits:
- Optimal runtime code -- libraries process stuff during runtime, whereas you can eliminate a lot of runtime machinery by analyzing the codes structure during generation.
- The elimination of bugs introduced doing repetitive work.
- Better understanding of your code, generation generally leads to higher level "model"; where the model is used to represents what needs to be generated.
- Reduction in LOC -- thousands of lines can leads to millions of lines of output code.