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.

Arguing for code generation, I am looking for some examples of ways in which it increases code quality. To clarify what I mean by code generation, I can talk only about a project of mine:

We use XML files to describe entity relationships in our database schema, so they help us generate our ORM framework and HTML forms which can be used to add, delete, and modify entities.

To my mind, it increases code quality because human error is reduced. If something is implemented incorrectly, it is broken in the model, which is good because the error might appear sooner since more generated code is broken too.

Since I was asked for the definition of code quality, let me clarify this, what I meant is software quality.

Software Quality: It is not one attribute but many, e.g. efficiency, modifiability, readability, correctness, robustness, comprehensibility, usability, portability etc. which impact on each other.

share|improve this question
2  
What is your definition of code quality? –  Emmad Kareem Mar 30 '12 at 18:12
    
@EmmadKareem I added a short definition of it on the original question. –  platzhirsch Mar 31 '12 at 12:09
    
I think that automated code generation will help increase consistency and uniformity in your code. In some cases that does increase quality but I don't think it's a catch-all. –  joshin4colours Mar 31 '12 at 14:49
add comment

9 Answers

up vote 22 down vote accepted

Code generators cannot generate better code than the person who wrote the generator.

My experience with code generators is that they are just fine as long as you never have to edit the generated code. If you can hold to that rule, then you're good to go. This means you can reliably re-generate that part of the system with confidence and speed, automatically adding more features if needed. I guess that could count for quality.

I once heard an argument for code generators that a single programmer can produce so-and-so many lines of code per day and with code generators, they could produce thousands of lines! Obviously that is not the reason we are using generators.

share|improve this answer
    
+ the italicized section a thousand times. Code generators should function like your compiler or the C++ template mechanism: you should never have to manually edit their output. The only time you should ever read the output is if you suspect a bug. –  anon Mar 31 '12 at 23:38
    
It's a shame I cannot vote more... –  Fabricio Araujo Apr 3 '12 at 19:15
add comment

I'd argue the opposite -- presuming you are writing interesting applications, code generation decreases code quality. The nature of code generation rewards very cookie-cutter, overblown, over-specified frameworks that become very hard to deal with without continually reliance upon the code generation tool to continually generate bigger, more complex, uglier bunches of code. While it can be a good tool, it really shouldn't be the primary tool in the box.

share|improve this answer
    
Agreed, the garbage that comes out of some ORMs (garbage from the standpoint of someone who knows how to write well-performing database code) is a good example. It often sort of works enough for someone who doesn't know what they are doing to think it works. And new programmers don't get the skill to do the harder stuff that needs to be done outside the generator because they don't understand basic concepts. –  HLGEM Apr 25 '11 at 21:29
add comment

I think automated code generation and code quality are somewhat orthogonal and do not necessarily correlate.

Code generation is merely a way to solve a specific technical task. Whether it results in increased code quality very much depends on what you're doing.

Your situation is a good example of code generation resulting in increased code quality through early catch up of potential errors.

I can give you another example when automated code generation diminishes code quality. It's out almighty ASP.NET WebForms. It does automated code generation by translating an hierarchy of UI controls into HTML markup, which is everything but stable, predictable and manageable.

To draw the conclusion, automated code generation can help increase code quality when used properly.

share|improve this answer
add comment

Code generation doesn't affect code quality, per se, so much as code consistency.

Generated code will be consistent between instances of generation. If the generator is designed to emit good quality code, then the generated code will be of consistently good quality. If, however, the code generator emits bad quality code, then you'll get consistently bad code.

Code generation may also be used to build code faster. Faster, however, does not mean better... It could just mean you get your bad quality code that much quicker.

share|improve this answer
add comment

Increased code quality due to DRY (Don't repeat yourself).

The code generation rules are written once; they are not hard coded for every instance of code generated, and thus reduce the potential of human error in copy/pasting the content with slight modifications.

share|improve this answer
    
Unless you have to edit the generated code - which are not DRY at all.... I had to do that recently - it's not pleasant at all. If I had to manually edit an autogenerated code base again, I'll charge thrice!!! –  Fabricio Araujo Apr 3 '12 at 19:14
    
You should not have to ever edit that code; edit the code that did the generation itself and augment it with additional rules if necessary. Editing generated code should be the last resort. –  earlNameless Apr 8 '12 at 22:35
    
I would like to have that choice.. I didn't. –  Fabricio Araujo Apr 9 '12 at 17:51
add comment

Code generation is good if:

  • the generated code is not supposed to be edited
  • the code generator gives you enough flexibility to do what you need to do
  • the input language to the code generator is better (i.e. DRY) than what you would otherwise have to write
  • the code generator creates good reliable code that you don't have to worry about, even if it's wordy

When these are the case, the code whose quality you need to consider is the code that is input to the generator.

A simple measure of the quality is, for typical changes in requirements, how much manual editing do you have to do. The less, the better.

share|improve this answer
    
and that the generated code transparently maps back to the original, so you do not have to debug on generated code. –  user1249 Apr 2 '12 at 14:20
    
@Thorbjørn: I agree. On one app I've had to maintain there is generated Fortran. The need to be able to debug it was lost along the years, and I'm the only one stupid enough to still be around to field the service calls :) –  Mike Dunlavey Apr 2 '12 at 14:32
add comment

In addition to Martin's answer, I would add that SQL code generation is very good when you work in a record-by-record basis (select * from tab1 where tab1.pkcolumn = :parameter, update tab1 set [any number of columns] where tab1.pkcolumn = :parameter, etc). And your ORM will shine in that scenario, because of the SQL that needs to be generated is indeed repetitive.

My main worries is metaqueries - queries on object's properties that the ORM translate to SQL using whatever algorithm. Very similar metaqueries can generate SQL that's completely different - and have no guarantee that this generated SQL is performatic.

A metaquery language that translates to another language (SQL) that translate to a query plan to effectively execute the data gathering. And the generated result must be objects, so the ORM must instantiate the affected objects - so it can trigger another rain of queries to fill the attributes of the objects not brought by the metaquery itself...

share|improve this answer
add comment

I would say in your case it might increase quality a little bit, but reduces development time by a lot. Sometimes the generated code is flaky, awkward, or just plain bad. In those cases, teh generated code can decrease quality and add more testing / fixing / regression testing time to the project. And some tasks are just too complex to be easily generated - the generator becomes a whole separate system (possibly bigger and more complex than the main project) unto itself.

Code generators are fine, but be careful with them!

share|improve this answer
add comment

I used to work in a shop that relied on code generation heavily. In my mind it made the code for the project very uniform. And in that respect, the quality was OK.

However, when you are no longer allowed to write custom code because everything has to go through the generator then I think you lose some of the edge of being a programmer.

So I think this is a double edge sword topic for sure. Yes generators are great because they reduce errors and increase code standards, however, they also make "some" of the programmers dumb, because they are reliant on the generators instead of having to get their hands dirty.

Just my 2 cents.

share|improve this answer
    
Assembly language programmers used to say this about compilers. So I'm not sure this is a great argument. Being forced to get your hands dirty can be a good learning experience, but once you've learned you should use the most productive tool available. –  MarkJ Mar 30 '12 at 18:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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