CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

The ugliest code ever

Before I could stop my errant mouse clicking index finger this morning I stumbled into the generated code for a strongly typed DataSet this morning.  You know the orange bars that ReSharper puts into the vertical scroll bar to denote warnings in the code?  In the DataSet code it looked like a solid bar of orange sherbert. 

I was at a .Net user group last month at a talk on Subsonic.  When the speaker said the generated code was probably pretty good because it was generated code I almost spit up my coke.  Nothing against Subsonic, but my experience says that generated code is usually of atrocious quality and lacking in readability.

And now I'm going to have to go get gelato for lunch.  Bummer.



Comments

Jeremy D. Miller said:

So every static code analysis tool and test coverage tool has a means to exclude classes or assemblies from its reporting.  All just to keep strongly typed DataSet code from dragging your code metrics into the trough.

# September 18, 2007 9:09 AM

Mickaël said:

I usually generate code to avoid wasting time writting stupid code. That means it has to be excluded from anykind of analisys tool (except profiling). More over, if the generator is not that good...you get uninteresting code stupidly written :-)

# September 18, 2007 9:36 AM

Jim Bonnie said:

Jeremy,

Good thing you were not sitting up front :)

And the quote was closer to , what makes Subsonic's Generated Code better than code that I can generate using CodeSmith myself.

And the answer is because the Subsonic team has more expeprience with Data Access Code then I do.

But, I will have to see if resharper likes the code as much as I do...

# September 18, 2007 10:00 AM

Robz said:

The number of rules that strongly typed DataSets break in FXCop is kind of pitiful.

In VB, it also sets Option Strict Off at  the top of the file.

# September 18, 2007 10:03 AM

Bill Lang said:

Tsk tsk,

Didn't Microsoft ever tell you not to open that code file?  Blind faith my friend, blind faith.  

# September 18, 2007 10:06 AM

Damien Guard said:

Generated code can be pretty good it all depends on the code generation or template.

One way I've developed templates for both CodeSmith and SubSonic is to start by writing the class by hand and refining and refactoring it until I am very happy with it and then turn it into a template.

When revising it later, refactor the generated code then diff it against the non-refactored version and make the necessary changes to the template.

[)amien

# September 18, 2007 10:47 AM

Craig Bowes said:

Amern Damien.  Generated code is only as good as the original programmer who wrote the generator...

# September 18, 2007 1:41 PM

Jeremy D. Miller said:

@Craig,

Not to change the subject, but that's exactly why I'm dubious about Software Factories and any kind of large scale code generation

# September 18, 2007 2:41 PM

Steven Kelly said:

@Jeremy: somebody has to write the code. Organizations have good and bad coders, so if there is no generation you get a mix of good and bad code, and everyone spends time writing code. If an expert developer in an organization makes the generator, everybody gets code as good as his, and without even spending time writing it. If you later decide you want to change the code, you just change the generator -- rather than trying to write instructions to every coder to change their own code, and hoping they follow them.

Remember the move from assembler to 3GLs? The arguments are all basically the same, and code generation from Domain-Specific (Modeling) Languages only makes sense if you can ignore the generated code, because you trust the person who made the generator.

If you want to see some concrete cases, look at www.dsmforum.org. The key thing seems to be to make the modeling language and generator for a very narrow domain. This normally means making them internally, rather than buying them off-the-shelf -- we had enough of off-the-shelf generators in CASE and UML tools. When you have the freedom to change the modeling language and generator for YOUR needs, rather than begging some third party for changes, you can get the 5-10x increase in productivity that organizations like Nokia and Lucent have been enjoying for over a decade.

# September 19, 2007 4:25 AM

FransBouma said:

Of course, if the template sucks and the meta data is lacking, the generated code will suck as well, at least on the scale of suck used by the reader of the code.

So to get good code generated, you need to have good templates AND good meta-data. If either one of them (so a good template with sucky meta-data also gives bad results) is lacking in some area, you won't get stellar results.

That doesn't mean the concept of code generation sucks. the thing with code generation is that, IF the template and meta-data is OK, a code generator will beat any developer, simply because it won't make mistakes every X lines of code and won't get bored so it will take shortcuts nor will it get tired of the repetitive piles of goo it has to write.

I must say that I always find it a little funny that a developer declares code generation not that great a technique, so in other words: he himself is always capable of writing better code than a code generator can.

the thing is: that's a lie. You can't beat a code generator with proper templates and OK meta-data, simply because the code generator won't make errors.

But again: make no mistake: it is essential that the templates and meta-data are OK. this means that the templates have to be created from hand-written code, then the generated code has to be tested, profiled, checked for lame mistakes. This process has to be repeated till there are NO BUGS in the templates left and no lame lines of code left in the templates which make whinetools like fxcop throw up.

Code generators are just typing machines: they do the typing for you. If you look at them that way, there's no magic, they're just handy because who wants to type for weeks after weeks if a code generator can do that in a minute?

MS' internal code generator isn't that great, as it doesn't use templates, it uses the CodeDOM. This isn't a great decision because you can't alter the template to make it better.

# September 19, 2007 4:28 AM

Evan said:

Or you can lose the macro code generation (like CodeSmith) in favor of micro code generation (ala TDD and ReSharper)

NHibernate is pretty cleanly written (in OO terms), and there's no macro code generation there (so it can be done).

# September 19, 2007 11:13 AM

FransBouma said:

"NHibernate is pretty cleanly written (in OO terms), and there's no macro code generation there (so it can be done)."

Sure there is, at runtime. You don't see it, but it's there. Don't think POCO classes are persistable without runtime code gen. (or you have to fall back on lovely reflection... )

# September 20, 2007 4:14 AM

Sean Chambers said:

Ya know, I never bit at the code generation carrot and this just reenforces that notion in my mind.

I can see where it would be beneficial. You have thousands of lines of code to write and only enough man hours and developers to do half that.

The catch is in the corporate environment (doesn't apply to non-profit situations), is that where do you draw the line where you start to sacrifice readability and maintability to get something done quicker?

Frans, you mention that good generated code isn't worth it if you don't have good metadata and good templates then all is lost, well then in my book you have to pick your poison. Do you want to make sure your templates and metadata are inline, or do you want to write code for all the generic boiler plate code.

Or...you can take a ORM approach and use something like nhibernate or the such. Sure there is runtime code generation, but not a big cow flop that you have to constantly re-generate to accomodate for a new dataset.

Anyways, I've argued this point to death elsewhere already. Firing squad....Fire away!! =)

# September 20, 2007 11:45 PM

Ollie RIches said:

The quaility of auto generated code is only as good as the effort YOU are prepared to put into the template.

I guess most devs think because it's auto generated code they don't have to put to much effort into the template becuase more than likely it's going to be boilerplate code that does show off their amazing coding abilities :)

I'm interested what Frans means by 'meta data' in respect to templates?

# September 27, 2007 7:34 AM

Shawn Oster said:

As someone that has actually used SubSonic and looked at it's templates quite a bit I can say they are very clean, very solid templates mostly for one reason; they don't try to do everything for you.  The reason most generated code gets so ugly usually is the developer gets bored and starts adding poo in thinking, "oh,this will be neat, everyone needs caching like I do, and what a cool provider model I think I'll just jam in here."

You know how you should read other people's code to learn new things?  I actually *enjoy* reading SubSonic generated code, unlike netTiers that about makes me cry or even .NET Web Controls which I consider an unholy bastardization.  Server-side controls are IMO pure evil.  If you can't write the html yourself you probably shouldn't be writing web apps.

Anyway, it's like anything else in programming, it's rarely the technology at fault, it's the human behind it.  Generated code isn't ugly because it's generated, it's because the person creating the templates got it wrong.  Saying generated code is bad is just as silly as saying it's good.

# September 27, 2007 7:48 PM

Craig Bowes said:

Jeremy,

"Not to change the subject, but that's exactly why I'm dubious about Software Factories and any kind of large scale code generation"

Well, if you consider that most non-generated code written by plain developers sucks, chances are that most generated code will suck too.  But its not a guarantee.  You can certainly pick up codesmith or mygeneration and create templates that DO in fact generate clean code, because the templates are written by YOU (or a Frans said, by someone else who can write readable code.)

This is way off topic, but one thing noone seems to consider about code-generation...the very fact that you have to have a seperate template and code generation language says something about the strongly typed languages we use: C#, Java, VB.NET etc.  Languages like Ruby and Lisp get this dynamic generation without going to a separate tool and template language.  You can modify a class at runtime in Ruby or Lisp (from what i've heard) pretty easily in the native language.  Code-generation is really just another way of writing an algorithim.  If we need programs that write other programs, shouldn't our languages do that natively without going to something clunky like reflection?

I love my intellisense and compile time checks as much as the next strongly typed developer, but part of me thinks that code-generation is a sign we're reaching the limits of our language of choice.

# October 8, 2007 6:22 PM

Jeremy D. Miller said:

"This is way off topic, but one thing noone seems to consider about code-generation...the very fact that you have to have a seperate template and code generation language says something about the strongly typed languages we use: C#, Java, VB.NET etc.  Languages like Ruby and Lisp get this dynamic generation without going to a separate tool and template language.  You can modify a class at runtime in Ruby or Lisp (from what i've heard) pretty easily in the native language.  Code-generation is really just another way of writing an algorithim.  If we need programs that write other programs, shouldn't our languages do that natively without going to something clunky like reflection?"

Craig,

In Venkrat's DotNetRocks interview a week or so ago he talks about this very subject.  This came up at ALT.NET a couple times.  I prefer the Ruby meta-programming model that puts everything in one place over the brute force codegen techniques or even AOP inside a static typed language.  I'd even say that Ruby provides some significant advantages over the stuff I do with Dependency Injection in C Sharp in terms of readability.

# October 8, 2007 10:22 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP