13

One of my colleagues likes to use automatic code generators, which create large amounts of code that is poorly documented and very hard to maintain.

Is the cost of using a code generator worth the hassle in maintenance, for the reduced time to creation?

Maniero
  • 10,816
Mumbles
  • 469

4 Answers4

23

Code generated by a generator should never be maintained by hand. If it needs to be changed, then the generator and/or its settings must be tweaked and run again. Considering that, it doesn't matter if the resulting code is incomprehensible and undocumented as long as the generation mechanism itself is crystal clear. (Be sure to document the fact that the code is generated, and where the generator is and how it works.)

Analogy: while my computer's processor is always executing machine code, I don't need to know anything about it as long as I know how to create that machine code using high-level language and compiler. I've heard that GCC sometimes produces subpar machine code, but who cares, as long as it works perfectly. Database abstraction layers produce SQL to operate with the DB engine, but who cares what that SQL looks like, as long as the abstraction layer is clear and works?

When properly used, code generators can definitely save not only creation, but also maintenance cost.

11

Let's rephrase that:

Is the cost of a good automatic code generator worth it?

Yes.

Is the cost of a poor automatic code generator which creates more work for everyone else but it's author worth it?

Absolutely not. There is no excuse for poor code. If someone wants to be clever and use automatic code generation then they should take the time to ensure that the code generated is good code. Otherwise, what's the point of it? It's only pushing the buck down the rode and when it comes to code production, the buck should stop at the developer who wrote it.

wheaties
  • 3,564
6

A code generator is a type of compiler. You don't worry about how pretty the compiler output is, you just work with the source code. Using it and then hand-modifying the output is often harder than just writing it from scratch in a form comprehensible by humans, and means you can't use the code generator again without a lot of work, since you'll have to apply the same changes to the same incomprehensible code accurately.

Therefore, they can be fine if they're part of the build process, and documented as such. The input to the generator is then the source code, and whatever it produces is intermediate results, not to be messed with.

However, if somebody's using one to produce incomprehensible code that's supposed to be used as source, then that person's producing bad code. It doesn't matter if the person is producing bad code mechanically or by hand, it's still bad code, and you still have a quality problem with it.

Therefore, you need to treat this as any other developer cutting corners and writing bad code. I don't know how you handle that at your shop.

3

From the comments on other answers it seems like you're asking about team standards rather than code generators themselves.

A code generation tool should be included in the project and should (where appropriate) be part of the build process. An example would be on our team we make use of Subsonic 2.2 which we have generate classes from the database objects on build.

The exe that does this is checked into SVN as part of the project so that a new member of the team could get the new project from svn and immediately build it without having to figure out where all these database classes came from (in this example we don't even include the generated code in svn).