views:

1416

answers:

14

I recently took a look at MyGeneration, and I think it might be a great tool to use. This was the first time I looked at a code Generator, and I would like to know about your experiences from other products, the pros, the cons and did you consider the tool usefull.

I will explain a bit how I plan to use it, so you might be able to answer more accurately to my question.

I create the tables in the database.

Autogenerate CRUD stored procedures. Autogenerate DAL.

Maybe autogenerate some parts of the BLL.

I am using Oracle and Visual Studio.

+20  A: 

What’s the best code generator you have used

Me

Rex M
oh come on, that's hilarious
Rex M
I was going to say the same thing. Well, not about you, of course. I've never used you.
Paul Tomblin
Jokes aren't constructive answers...and I don't think it's very funny, either :)
Andy Mikula
Not constructive, of course. But modestly funny. Leave it at -1 or -2 and no one will mistake it for serious...
dmckee
If somebody can't figure out that's a joke maybe they shouldn't be trying to program.
Mark Harrison
And that's exactly who I blame when said generated code does not compute!
Chris Ballance
and also the worst code generator I've ever used
Triptych
+3  A: 

The best code generation is dynamic code generation. That is, don't use code generation. Instead, write code that can be used dynamically, adapted in the same way that the generated code differs for whatever input you give it. This may or not be possible in a given language, depending on the dynamism of your "generated" code, and the language's dynamic abilities. But often, putting the common code in a parent class will suffice.

The problem with generated code is what happens when you want to change the generated code. Say you've generated code for 100 files, all using the same generator. Then you find a bug in the generated code. Now you have to either edit 100 files, or regenerate all 100 files. But if you've edited any of the files, you'd lose any file-specific changes you've made. Not a good choice to have to make.

So I steer clear of generated code, with 2 exceptions. First, I'll use generated code as a starting template. This is usually just a blank class. In fact, I'm more likely to use code "snippets" than a code generator. The only other time I'd use a code generator is if the language I'm required to use is not dynamic enough to do what I need. But I've generally tried to stay out of situations where I can't use the proper language.

In my experience, code generators that require you to edit the generated code are *always* a bad idea. Code generators should create a file you can use without editing, otherwise it is a maintenance nightmare. In C/C++ this would usually be a .h file you can include in your hand-written code. In C++, it can be a .h file that contains a class with virtual functions you can override in a subclass.Whenever I write a code generator, it always outputs a comment that says "AUTOGENERATED CODE, DO NOT EDIT", and also makes the output file read-only.
KeyserSoze
+5  A: 

The question is, in a sense, unanswerable, because different code generators are used for different jobs.

At one level, I could answer "GCC" (GNU Compiler Collection, rather than GNU C Compiler), because it is by far the best object code generator across platforms and programming languages. For an individual platform, there may be better choices; across all, there is none better.

There are other more specialized code generators; there are many of them. Some of them are template systems for web work; others are code generators for a particular DBMS and its supporting languages; others are GUI generators that give you a start on the skeleton code that you need to write for a GUI. Then you have parser generators - programs like Yacc (and Lex) and ANTLR, and so on. Does template metaprogramming in C++ count as code generation?

If the code they generate is suitable for the task you have in mind, they are great starting points.

However, as Craig Buchek sagely notes, if the generator has bugs but you've manually modified the generated code, then redoing the code generation can be a big problem. Consequently, if you use a code generator, you need to understand the issues, and modify the generated code manually as little as possible (preferably, not at all). After all, you don't normally edit the output of a code generator such as GCC, do you?

Jonathan Leffler
I don't think "as little as possible" is a strong enough statement: you should never modify generated code, that's a recipe for a maintenance nightmare. You should also never check in generated code (or manually copy it to multiple machines), because if you do, you will invariably lose the ability to regenerate it. Your build system should be set up to automatically regenerate code when the dependencies change. On all developers machines and the build machine, and the build machine should do a 'clean' build at least nightly.
KeyserSoze
A: 

I agree with Jonathan Leffler, who said that different code generators are used for different purposes, but I'll throw a shout out for one that has helped me quite a lot recently: JAXB's XJC, which can turn an XML Schema into a series of java classes and a matching marshalling/unmarshalling engine. It's a Good Thing.

nsayer
+2  A: 

For file-based code generation, as opposed to in the editor code generation (ie CodeRush) there seems to be 4 primary choices for the.net/Visual Studio world

  • my generation
  • CodeSmith
  • T4

MyGeneration may in fact be a great code generator, but something about its website and its approach ends up turning me off.

CodeSmith is a mature product, with a lot of satisfied users - I've certainly generated tons of code on it. It does cost a few hundred dollars per seat however.

T4 is well worth a look - in particular combined with the T4toolbox, it has excellent modularity and Visual Studio integration. I've successfully migrated large multi-template code generation project from CodeSmith to T4 in under a day and have used T4 to generate schemas for both Coral8 and Kdb data providers.

In short - go with T4, T4 Toolbox, and the free T4 editor from clarius.

Scott Weinstein
A: 

Does intellisense/autocomplete count? Then my vote probably goes to Visual Studio. I can hardly live without the autocompleting of variable names, function names, etc. I also quite like the snippets functionality, for example prop [tab][tab] to create a property with a get and a set, cw [tab][tab] for Console.WriteLine();... not to mention the switch snippet. That in combination with enums is just... awesome. There are no other words :)

Aistina
A: 

ANTLR grammar generator.

FerranB
+3  A: 

No one will get this reference (well, Jonathan might), but the best code generator I ever used was the FourGen code generators for screens and reports using Informix 4GL.

These generators had some unique properties that I simply haven't seen anywhere else.

1) They were an organic and first class part of your build. That is, when you worked with this tool, you constantly used and reused it. It wasn't a "wizard" that does a simple "one shot" and you're done.

2) You had full control over the generated code. Specifically, if the code generator did something you didn't like, one of its capabilities was letting you add, change, or simply replace generated code.

On the one hand, you interacted with it at a reasonably high level (basically tying code to events), but on the other you also had an almost "sed(1)" like ability to tweak and change code.

If the high level facilities didn't work, you could simply patch the resulting output 4GL, key that for the generator, and it would reapply your patch at build. The code was also organized in a very granular fashion, baring all to the tool.

For example, when it generated the SQL load a form, this seemingly simple process of building a basic select statement was broken out in to 4 or 5 steps (columns, join, join clauses, filter clause, order by). This fine granularity is part of its power, meaning your individual changes have the smallest impact. I could completely replace the logic to build up the filter part of the SQL, without affecting the tools ability to build up the columns and mappings, etc.

This is a simple, almost crude facility, but it offered key value over many other code generator systems. With other systems, if the code generator did something you didn't like, and you had to hack the resulting code, then the value of the code generator vanishes on a future change. Rerun the generator, lose your changes. The more you changed, the more painful the process of rerunning the generator. With FourGen you were able to capture those snippets and make them part of your build, and rerunning the generator happened, literally, all the time -- it was intrinsic to the build.

The more you hacked, the more you took control away from the code generator and accepted the burden. But this was a gradual process and even with some of the most complicated programs, 9 times out of 10 the code generator continued to provide real added value to the dev process over the lifespan of the project. Rarely did I have to just "throw up my hands", annotate my forms with the "keyword of death" namely "do_not_generate", and accept the entire burden on myself.

While modern systems offer a lot of power with their reflective techniques, there's something to be said about being able to "hack the result" when necessary just to get that specific bit of functionality that the framework authors simply didn't envision or tool for.

Hard to convey the real power here, it's a dead tool for a dead language. But, frankly, I haven't seen anything come close to it in 15 years. Believe me, I've looked, and I still go "why is this so hard"?

Will Hartung
Jonathan recognizes FourGen - and it was one of the generators I had in mind referring to DBMS code generators (not the only, but one of them).
Jonathan Leffler
A: 

You've never heard of it. It was named cEnglish, and it consumed dBASE-like procedural syntax, and emitted C source code intermingled with your choice of embedded SQL statements.

Think a little bit (but only a little) like accepting Access/VBA source and generating .NET and LINQ/SQL.

le dorfier
A: 

Jonathan has it right.

You only get real value out of code generation if you never modify the generated code.

I think code generation is a big win, and I do it a lot, but I've never used a "tool" to do it, so I guess I don't understand why that is a question.

I just write a program A that writes program B (and C, D, etc.). It starts from some input usually in the form of a simple parsed language, or DB tables, or whatever. The code I generate can be C, C++, C#, SQL or really whatever I would otherwise have to write by hand.

It's a big win for two reasons:

1) Performance, because the final code is custom and doesn't have to wade through a lot of options that don't change at run time. The code generator does not need to be fast, because it is run so seldom, and anyway it usually is fast because it doesn't have that much work to do.

2) Development time is short. The reason is, if your input contains two parts, A that changes very seldom, and B that changes often, the generator is simple because it only has to deal with A, and the generated code is simple because it only has to deal with B, as opposed to a monolithic program that has to deal with A x B. This can shorten development time by an order of magnitude.

There's a big word for this, by the way: "partial evaluation".

Mike Dunlavey
+1  A: 

The issue of re-generating can be avoided or delayed if instead of adapting the generated code, we'd adapt the generator itself: by providing more options and modifying the generation logic. This is rarely done because generators are either difficult or impossible to adapt. Some tools are not that opaque such as Tankogen, Acceleo, Mia-Generation

Personally I am satisfied of the code quality and the time gained by using this generative approach.

Oliver
A: 

I can't say this is "the best" because, as many pointed out, different tools are right for different jobs. But the ANTLR project has a great little templating engine called "StringTemplate". You can use it without using all of ANTLR. ANTLR generates all its parser artifacts with it, so you know it has been exercised and has broad capabilities.

Try it, you'll like it.

Charlie Flowers
A: 

Common Lisp.

It makes me more productive than any other programming language or environment I've ever tried. The only downside is that it's a huge downer having to do any work in what's usually called "OO" today (i.e., procedural with Simula-style OO), and everybody thinks you're crazy.

Ken
A: 

Lot of generator available on internet. generator can even generate whole asp.net application i would recommend to use . eXtremecode generator and Asp.net maker. both generate well formed asp.net application. extremecode generator is free of cost but for asp.net maker2, you have to expand some bucks.

Abdul Wahid