Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

closed as primarily opinion-based by GlenH7, gnat, MichaelT, amon, jwenting May 5 at 6:39

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

add comment

4 Answers

up vote 9 down vote accepted

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.

share|improve this answer
14  
I disagree with your second point. The generated code only needs to be good enough so that it works and doesn't create performance/security or any other issues. Since you should never maintain generated code by hand, it doesn't matter if it's not up to your usual coding standards. –  Hila Oct 4 '10 at 15:32
3  
That's fine for you to disagree. However, if you're going to take the time to create a code generator then why not take the time to make the code generated pretty? Once generated, I don't know who made it, how it was made, nor the intention of it unless it's just as readable/maintainable as all other parts. Sometimes generators are there only to create a starting point rather than the full-fledged finished product. –  wheaties Oct 4 '10 at 15:53
1  
Also, if the generation of the code is part of your build (i.e. it gets re-generated every build) then it doesn't make as much sense to produce "beautiful" code. But if you're going to generate the code once and that's it, then it's a different story. –  Dean Harding Oct 4 '10 at 23:15
2  
Hila, you should never maintain generated code by hand, but when the day comes that you need to change that code due to new/changing requirements, you need the code to be clear and comprehensible so you can easily make the necessary changes to the generator, and then re-generate. –  Carson63000 Oct 5 '10 at 0:47
6  
The generated code doesn't have to be pretty enough for maintenance, but it needs to be intelligible enough for debugging and for validation. –  Huperniketes Oct 15 '10 at 17:33
show 1 more comment

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.

share|improve this answer
add comment

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.

share|improve this answer
4  
The problem then become, only one person has the tool, no one else does, and therefore has to manually maintain the code. –  Mumbles Oct 4 '10 at 15:44
    
This is passing the buck. As software developers, our business is code - no matter how we produce it. –  Steve Evers Oct 4 '10 at 16:13
9  
@David, if only one person has the tool, you shouldn't be using it for a project involving more than one person. –  Matt Olenik Oct 4 '10 at 16:25
2  
@Matt, exactly. Generators are part of the project (comparable to build scripts) and should be stored in version control or similar central repository. –  Joonas Pulakka Oct 4 '10 at 17:17
2  
@SnOrfus: I think our business is to produce working products that people are willing to use and buy. That's where our salary comes from. Code is just a medium. –  Joonas Pulakka Oct 4 '10 at 17:26
show 1 more comment

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).

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.