Do you use code generation tools? [closed]
Asked Answered
A

18

10

Do you use code-generation tools (aside from those used to generate proxies and from designers built-in to visual studio)?

What part(s) of your application do you generate?

Do you typically roll your own generator? If so, what type of generator do you write (asp templates, coddom etc.). If not, what 3rd party tools do you use?

I am currently working on a few different projects wich all use a custom code-generator that handles everything from generating the database structure, business entities, DAL, and BLL. I am curious about other peoples experiences are with these kinds of tools.

Adler answered 27/9, 2008 at 15:43 Comment(0)
A
9

I'm in the philosophical camp that considers code generators to be "wrong", because they indicate something that should be made part of the language.

But it's been a big part of the Pragmatic Programmer's ethic to write code that writes code, and in practice code generation works well if the generated code is hidden by default. No matter how philosophically pure you want to be, the language will never evolve as fast as the problems you want to solve.

The code that gets generated when you build a Windows Form in Visual Studio comes to mind. You can look at the generated code if you want, but it's a better idea not to. Moving to a declarative language with WPF was superior, however, because it's cleaner and more reliable to manipulate declarative code programmatically than imperative code.

They should have done the same thing with LINQ-To-SQL classes. They need a declarative language for classes that just have properties and no custom behavior. It probably would make it easier to make those entity classes dynamic--changing automatically when the underlying database schema changes.

We tried using CodeSmith to generate .NetTiers classes for all the tables in our database, but ran into two issues:

  1. .NetTiers was bloated, and the code generated was enormous. I think code generation tools make it too easy to creature feep.

  2. Because the schema was being actively developed and revised, we had to regenerate a lot, too, and that ended up making it very difficult to keep everything in source control because all the files were being regenerated and replaced. I ended up being unsure if the generated code ought to be in source control at all.

The best place for code generation should be in the compiler or the build phase, not the design phase. When you use an anonymous type or method in C#, the compiler is doing code generation on the fly. If you generate code during the design phase, you get a chunk of stuff that must be regenerated every time the underlying parameters change.

Avogadro answered 27/9, 2008 at 18:21 Comment(6)
You have a very interesting perpective. I am currently dealing with the same source control issues with the project I'm working on currently. We use an in-house code-gen tool. Everytime a schema changes we are forced to regenerate everything from stored procedures on up. What solution do you have?Adler
The solution, in that case, was to drop ORM and the code-generator completely. We found that it was counterproductive to use n-tier design for a system that was discovering its requirements as it was being developed.Avogadro
or in the case of NHibernate, drop the sprocs and generated DAL completely..that's what works best for meLoot
I'm not sure I understand why it matters that NetTiers generates a lot of classses. My reaction is more like "so what?" The fact that it's generated means you didn't write it.Capybara
You should not keep generated code in source control. Instead you should keep the generating code. Keeping generated code is worse than doing nothing, because it creates the possibility that others will commit manual tweaks that will be lost the next time you commit generated code.Laurenlaurena
I use code generators every day. It has dramatically reduced the time I need to write the boring/tedious part of the application. I can highly recommend using Ruby as a code generator tool. I generate both C++ and C# code from Ruby. The input to the code generator is a simple internal DSL. I have added the generated code to source control. It works fine in this case because the code generator is only executed when asked. It is not run every build.Distress
U
7

Not that we are working in the .net/web domain, but home-made code generations tools from various home-designed languages are a crucial part of our development tool chain. We have two major such tools (with grammars and parsers and formal definitions), and a host of minor one built on macros like m4 and perl. They all generate plain C in the end, which is natively compiled.

Domain-specific languages are one of the key tools for programmer productivity for any large-scale software endeavor in my experience. If you are building things like compilers, simulators, or other very complicated software with many recurring patterns that have no support at all in the basic languages (which typically means portable C and sometimes C++), code generating tools is the way to go. I view domain-specific languages as the next step in generalization: first you break out common computations into functions (or subroutines to be historical), then you break out common functions into templates or generics if such a facility is available, and then you break out even more commonality and repeating code into a full-blown custom language.

It is all about reducing the volume of code you actually write, and removing any form of tedious repetition and non-value-added code from the programming process. As soon as patterns repeat, apply a domain-specific language!

Undershrub answered 27/9, 2008 at 20:12 Comment(1)
I second you about code generation. Couldn't have said it better!Hohenlinden
L
5

I started rolling my own generators (data access, sprocs, etc) back when I was doing classic asp work (circa 2001). I slowly moved to CodeSmith, since it was much easier to deal with. I was still primarily just generating all the Data Access layer type stuff (including sprocs) for my .NET code.

A couple years ago, I made the jump from Macro Code Generation (i.e. CodeSmith) to Micro Code Generation.

The difference is that with CodeSmith I was generating huge swaths of code for my app, all generic, and all at once. This became problematic for edge cases and regenerating when changing the source for the template (i.e. table structure). I also ran into cases where there was a high inventory of carrying code that I wasn't using, but was generated from my template. Did all those methods work? Maybe, maybe not. Going in and cleaning up the generated code would have been a huge amount of work (i.e. after more than a year on the same codebase).

Micro Code Generation, in contrast, allows me to generate exactly the classes I need, in exactly the right scenario I want. The primary tool I use to do this is ReSharper. The way I do this is by writing my unit tests before writing my production code. In that scenario, ReSharper uses my Unit Test as a template to auto-generate the skeleton for the production code. Then it's just a matter of filling in the blanks.

For Data Access, I'm no longer generating anything. I've found that a good O/R M replaces everything I used to put in my data access layer (i.e. NHibernate). Given that, I will never write or generate another data access layer in my life (I refuse to).

Plus, I get the benefits of having a large unit test suite, among other things

Loot answered 27/9, 2008 at 15:59 Comment(0)
S
5

Since Fog Creek Software's in-house language, Wasabi, has compile-time code generators built in, we use them to automatically create the meat of our entity classes that map to database tables. So instead of writing a class with a dozen different properties and methods, we can just write:

<ActiveRecord("Kiwi")> _
Class CKiwi
End Class

and CKiwi will have Load(ix As Int32), Commit(), and fields/properties for every column defined in its underlying schema for the Kiwi table. It keeps us from having to have huge O/R M libraries, but still allows us to quickly add a table to our products.

Slat answered 27/9, 2008 at 18:13 Comment(0)
A
4

Code generation in the spirit of compilers can be great. Code generation in the spirit of "wizards" has uniformly turned out to be a bad idea.

Andreeandrei answered 27/9, 2008 at 19:36 Comment(0)
B
1

We used to use CodeSmith to generate our NHibernate hbms, our entities, and a few other things. After a while we got sick of this flow so we ditched it.

The T4 generator is free and worth looking into for generation.

We still use the Castle CodeGenerator for MonoRail link generation.

Bel answered 27/9, 2008 at 15:50 Comment(2)
When you say you "got sick of this flow" what exactly do you mean? Thx!Adler
Well the flow started out w/ a UML tool. Having to go into the UML tool to add a new entity was a pain. It was much more natural to do it from the IDE.Bel
A
1
  1. We use Code generators for Exceptions
  2. Generating DAO for CRUD operations
  3. use JAXB to genereate code
  4. Use XDoclet to genereate EJB local/home interfaces
  5. Use Velocity templates to generate documentation for business models
  6. Use Apache Axis to generate WSDL stubs
Archespore answered 27/9, 2008 at 16:8 Comment(0)
S
1

I create my own tools for some tasks. Its fun to do and even saves time in the long run. For very dull tasks, it even saves your sanity.

Schertz answered 27/9, 2008 at 17:59 Comment(0)
P
1

Home-brewed code generators work great for building unittest cases from end-user spreadsheets that contain examples of how it should work.

See Tooling to Build Test Cases for one example.

Panga answered 27/9, 2008 at 19:50 Comment(0)
P
1

We use LLBLGen to produce our data access layer. You point the generator at the database your using, select which tables you want to use and it churns out the classes needed. It is all very quick and easy.

Perfectible answered 27/9, 2008 at 22:28 Comment(0)
C
0

We have an in-house built code generator that takes care of database access. One writes stored procedures and gets corresponding methods abstracted in a gateway class.

We also generate web services in order to properly interface with Flash -- i.e. handling exception in a sane manner.

Finally we have an exception generator that takes away the drudgery of exception best practices (tons of constructors, etc...)

Chickweed answered 27/9, 2008 at 15:54 Comment(2)
Can you explain more how you used a generator for exceptions? I'm just curious what exactly the situation was and what your generator did. Thx!Adler
We use a resource file with the name of the exception and some other generation parameters. Then we generate code with an XSLT.Chickweed
B
0

At a previous employer, we had a home-grown VB.NET application that would turn an XML Schema Definition file (XSD) into a static C++ library. This made it much easier to work with C++ data types (bool, std::string, etc.), and all of the interesting XML code was hidden inside of these generated classes.

Backhander answered 27/9, 2008 at 15:57 Comment(0)
B
0

We just started using Grails here in the office. Previously, we had a set of in-house JSF/Hibernate CRUD generation scripts.

... Grails wins. The code generation from Grails is very nice and can get you a CRUD app going in about 15 minutes, without actually putting the code in the code files!

Of course, it CAN generate the actual code into the code files, when you want to modify it. Most of the time, for regular CRUD, you can get away with just changing the views.

Beeswing answered 27/9, 2008 at 16:4 Comment(0)
V
0

I've used one to generate serializable data objects which could be reformed across different platforms (windows, linux, solaris, mac, bsd, etc). It was an in-house solution.

Vitelline answered 27/9, 2008 at 16:44 Comment(0)
F
0

I wrote a lovely tool in which the experts in the data format I'd written a parser for, could submit their own samples through a web form, look at the output, and tell me whether it was correct.

From that, a jUnit test would be generated. Lovely.

Except that not a single person bothered to use it, and I gathered no test cases whatseover.

Feuchtwanger answered 27/9, 2008 at 17:9 Comment(0)
L
0

Like some others here, we have also created our own code generator (Inon Datamanager/Viewmanager) for data access, HTML form handling and certain business logic operations. A key to having this work well is to design it so you never have to touch or look at the generated code.

In this way, it almost does become part of the language - the language (Java in our case) is extended to include a domain model specification and a viewmodel, and then you just fill in custom business logic with real Java code.

This gives us the right tools to communicate with analysts and business users, while still having the power of Java to set up the details of underlying behaviour.

Louie answered 27/9, 2008 at 19:26 Comment(0)
S
0

If interested in LLBLGEN, which is excellent, you might also evaluate subsonic. Maybe even see what Rob Conery has to say about any overlap or interaction between subsonic and t4.

Shaum answered 24/10, 2008 at 15:32 Comment(0)
C
0

I have created a custom code generation framework that generates proxy classes for web services in several languages such as Java Script, Action Script, Java, C# and Objective C, I use no templates or tools just plain C# code that generates code with some helper classes, Code generation can really save a lot of time, but I think the generated code should be as simple as possible and should not be overused.

Cosmorama answered 20/4, 2013 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.