Revolutionary (versus Evolutionary) Code Generation
secretGeek .:dot Nuts about dot Net:.
home .: about .: sign up .: sitemap .: secretGeek RSS

Revolutionary (versus Evolutionary) Code Generation

It's just about time to throw out all my code and rewrite it in {x}. Again.

Many of the changes to the .net framework in the last few years have allowed me to get away with just refactoring some of my code, or perhaps discarding the ugliest portions of it. Over time it seems to reduce down, like a knob of butter simmering on a hot plate.

But with the syntax changes in 3.0, it seems that almost everything I've written can be rearranged into a simpler style. And the biggest effects will be felt in the data access code: this means my use of code generation is set to change drastically.

First up -- a short run down of my general approach in regards to code generation.

The Evolutionary Approach

1. Write good code, manually.

Even if you'll eventually 'generate' the code, you want to first be an expert at writing it manually.

This is the wax-on, wax-off part of the game.

2. Move logic away from the user interface, into 'business' classes.

User interface and 'business logic' will each go through big changes -- indpendently of each other. Separating the two is such common advice that it's often treated gospel to many. Either way, it's usually worth it.

3. Refactor code into small reusable portions where possible.

You want to avoid repetition. So where the same code is repeated in slightly different ways, you extract this into a small helper class, or a utility function.

The code is now smaller, and neater -- but it's not gone altogether.

4. Convert what's left into templates

Take the remaining code, look at what overall patterns exist and convert the code into templates (CodeSmith for me, kiddo!) to accomodate accordingly.

A fair rule of thumb is that you should have carefully written it by hand three times before extracting it into a template.

Now, when the code generator does its work, the generated code will be the best possible code.

In theory.

So if things are going well...

The goal is to have:

  1. no repetition in manual code.
  2. no need to customize generated code.
  3. no logic in forms/code behinds.
  4. 'business logic' in 'business classes'
  5. Generated code where necessary. Less is more.

And this is evolutionary because?

Over time, as the framework improves... the libraries and templates evolve, just a little bit.

For example, when nullable types were introduced, it became easier to talk to the database (where fields are natually 'nullable').

Hence, the templates were improved in places, and the helper functions were reduced or improved in places.

Generics were a big change. Writing 'type-safe' collections went from being 'code-intensive' to trivial, e.g.(List<Person>). This made the 'list template' simpler, but didn't get rid of it altogether.

The introduction of partial classes meant that rather than customizing a generated class through inheritance, it could be done by adding a partial class. This was neat -- but hardly reduced the code that was generated.

I mentioned a revolution...

Now, I'll probably move to using Linq to SQL -- and a lot of the fundamental code generation will now be handled by the Linq To Sql generator. This is a revolutionary moment: and revolutions are not without their bloodshed.

I'm expecting trouble. I'm expecting confusion. I'm expecting mistakes.

Linq to SQL will be generating code using the goodness of Linq. Great, wonderful, shiny. But. Unlike my own CodeSmith templates, it'll be generating the sort of code that I don't yet have the experience to write manually. That's a worrying shift.

And what of the generated code it replaces? Will it replace everything that my current DAL's do? And where it falls short, can I customise the generator? Or do I need to generate extensions through a separate tool?

How good is the Linq to SQL ORM designer? I've seen already that I can't right click on a table and 'refresh'. This was a glaring oversight that annoyed me in the first five minutes... What else is in store? Is this thing ready to be part of the core process in big 'line of business' apps?

Actually... shiny new tools to play with... I guess the answer is... Bring It On!

(am i the only one who accidentally types 'ling' when i mean 'linq'? (no))

Also -- by the way, since no one I've spoken to seemed to realise this: you can get Linq to work on .net 2.0 sp1 machine, if you include the right dll's with your project.





'Josh Bush' on Wed, 14 Nov 2007 12:07:03 GMT, sez:

Instead of rushing in with shiny new tools on existing projects, why not try new stuff on new projects to get a feel for it? You seem to have questions about things. There is no need to risk the solidity of an existing project with new untested stuff. Am I alone in this feeling?



'lb' on Wed, 14 Nov 2007 19:49:49 GMT, sez:

@Josh
Yeh I agree completely. I really mean about trying to reuse existing techniques on new projects. SO when i say "throw out all my old code" I really mean "throw out all my old code techniques"

And in this case (upgrade to use Linq to SQL) i further mean "throw out all my old techniques for making evolutionary changes to code templates"





'Josh Bush' on Wed, 14 Nov 2007 21:22:03 GMT, sez:

I'd love to jump on this Linq2Sql bandwagon, but at my office we use Oracle. I'll have to wait for the official Oracle bits to be released before I can join in the fun. In the meantime, I guess I'll just have to stick with Entity Spaces.



'Max Guernsey, III' on Wed, 16 Jan 2008 13:41:46 GMT, sez:

@lb and JoshBlush:

Actually, there's no harm in throwing out some or all of your old code... it's your /tests/ that you want to keep.

If your DAL is covered in good, meaningful, interface-oriented tests, what's the harm in ripping some of it out and replacing it with a new piece of technology you think more aptly addresses the concern at hand?

...after all: if you can't get the tests to pass all you have to do is undo your checkout. The most you could be out would be some time and you would probably have traded that for valuable knowledge.

-- Max




name


website (optional)


enter the word:
 

comment (HTML not allowed)


All viewpoints welcome. But the right to delete any post for any reason is reserved. Don't make me do it. Aim for constructiveness. Comments may be republished, emailed to your loved ones or printed and used as toilet paper. Also, I get particularly nasty on comment spam. It's not worth even trying to post comment spam here -- your html is escaped, and your links are given a rel='nofollow'. By attempting to post a comment, you understand that if the comment is considered spam, at my absolute discretion, your IP address may be used as the target of a prolonged distributed denial of service attack. Your electricity might suddenly stop working. Your car tyres will go mysteriously flat. You will suffer permanent hairloss. Your dreams will be filled with terrifying monsters. And in any case I reserve the right to record and publish your IP address.

 

TimeSnapper is a life analysis system that stores and plays-back your computer use. It makes timesheet recording a breeze, helps you recover lost work and shows you how to sharpen your act.

 

NimbleText - FREE text manipulation and data extraction

NimbleText is a Powerful FREE Tool

Use it for:

  • extracting data from text
  • manipulating text
  • generating code

It makes you look awesome. Use it right now! Go on! Hurry! Don't walk, run!

 

Articles

Mind-boggling Demo of New Gaming Genre, aka Folder-Based Hangman, aka Fun with Recursion Mind-boggling Demo of New Gaming Genre, aka Folder-Based Hangman, aka Fun with Recursion
Got CSV in your javascript? Use agnes. Got CSV in your javascript? Use agnes.
I went to write down a book name and founded an internet empire instead. I went to write down a book name and founded an internet empire instead.
NimbleText: Origins NimbleText: Origins
The Windows 8 Mullet The Windows 8 Mullet
Cosby: spontaneous striped background generator Cosby: spontaneous striped background generator
Slides from WDCNZ: Live Coding Asp.net MVC3 Slides from WDCNZ: Live Coding Asp.net MVC3
MVC 3, MVC 3, "Third Times a Charm" references
Custom Errors in ASP.Net MVC: It couldn't be simpler, right? Custom Errors in ASP.Net MVC: It couldn't be simpler, right?
Anatomy of a Domain Hijacking, part 2: The Website Who Came In From The Cold Anatomy of a Domain Hijacking, part 2: The Website Who Came In From The Cold
Anatomy of a Domain Hijacking, part 1 Anatomy of a Domain Hijacking, part 1
secretGeek.net domain has been stolen. The site may go down. secretGeek.net domain has been stolen. The site may go down.
Boring article: 'untrusted domain' issue with SQL Server. Boring article: 'untrusted domain' issue with SQL Server.
Coding While You Commute Coding While You Commute
Test Driven Dentistry Is A Good Thing Test Driven Dentistry Is A Good Thing
The 'less crashy' release of NimbleText The 'less crashy' release of NimbleText
Rethinking Toolbars in Visual Studio (or any IDE) Rethinking Toolbars in Visual Studio (or any IDE)
Where shall we have lunch? Where shall we have lunch?
Setting up email for your microIsv Setting up email for your microIsv
The NO Visual Studio movement: Compiling .net projects in Notepad++ The NO Visual Studio movement: Compiling .net projects in Notepad++
ZeroOne: the editor for programmers who think in binary ZeroOne: the editor for programmers who think in binary
Mercurial workflow for personal projects (with a .net bias) Mercurial workflow for personal projects (with a .net bias)
I see you're using vim. Let me fix that for you. I see you're using vim. Let me fix that for you.
The worst recruitment spam I've ever read The worst recruitment spam I've ever read
A thank you I forgot to say A thank you I forgot to say
My new product, NimbleText, is live My new product, NimbleText, is live
Grabbing the free songs of Jonathan Coulton (with Powershell) Grabbing the free songs of Jonathan Coulton (with Powershell)
Using NimbleSet to compare lists Using NimbleSet to compare lists
Wanted: Wiki Lists (dot org) Wanted: Wiki Lists (dot org)
DOS on Dope: The last MVC web framework you'll ever need DOS on Dope: The last MVC web framework you'll ever need
JSON Query Languages: 5 special purpose editors JSON Query Languages: 5 special purpose editors
What then, is b? What then, is b?
SQLike: A simple editor SQLike: A simple editor
Yet Another BizPlan Generator. Yet Another BizPlan Generator.
HOT GUIDS: A hot or not site for guids HOT GUIDS: A hot or not site for guids
How does life get better? One tiny hack at a time. How does life get better? One tiny hack at a time.
24 things to do, and 100 things *not* to do (yet) for building a MicroISV 24 things to do, and 100 things *not* to do (yet) for building a MicroISV
Venture capital won't kill Jeff Atwood, it will only make him Jeffer. Venture capital won't kill Jeff Atwood, it will only make him Jeffer.
A handy workflow image for newbie mercurial users A handy workflow image for newbie mercurial users
Fractal Feedback, a diversion into recreational programming Fractal Feedback, a diversion into recreational programming
Hump-Jumping: How the Education of Computer Science can be Saved, err, maybe. Hump-Jumping: How the Education of Computer Science can be Saved, err, maybe.
Suggested User Experience Improvements for DiffMerge Suggested User Experience Improvements for DiffMerge
SQL Style Extensions for C# SQL Style Extensions for C#
The Movie Hollywood (And My Wife) Doesn't Want You To See: Weekend at Jacko's The Movie Hollywood (And My Wife) Doesn't Want You To See: Weekend at Jacko's
Sysi: the ultimate administrators toolkit Sysi: the ultimate administrators toolkit

Archives .: secretGeek :: Complete Archives
TimeSnapper -- Automated Screenshot Journal TimeSnapper.com    
Version 3.3: true productivity boost

Next Action NextAction
Managing the top of your mind

NimbleText -- World's Simplest Code GeneratorNimbleText -- World's Simplest Code Generator, Text Manipulator, Data Extractor

25 steps for building a Micro-ISV 25 steps for building a Micro-ISV
3 minute guides -- babysteps in new technologies: powershell, JSON, watir, F# 3 Minute Guide Series
Universal Troubleshooting checklist Universal Troubleshooting Checklist
Top 10 SecretGeek articles Top 10 SecretGeek articles
ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Realtime CSS Editor, in a browser RealTime Online CSS Editor
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker


[powered by Google] 


How to be depressed How to be depressed
You are not inadequate.



Recommended Reading


the little schemer


The Best Software Writing I
The Business Of Software (Eric Sink)

Recommended blogs

Jeff Atwood
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Rhys Parry
Joel Pobar
Thomas White
OJ Reeves
Eric Sink

Aggregated Links

proggit
dzone
hacker news
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
weekly link post

LinkedIn profile
LogEnvy - event logs made sexy
Computer, Unlocked. A rapid computer customization resource
PC Smart Buys - Computer Hardware in Australia
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

home .: about .: sign up .: sitemap .: RSS .: © Leon Bambrick 2006 .: privacy