A Warning About Generated Code

I was just working with a bit of code generated from Illudium when I ran into the following error:

Oops!
Message Bean creation exception in model.snippets.libs.libService
Detail Cannot declare local variable tag twice.:Local variables cannot have the same names as parameters or other local variables.

A quick look at the code showed me the following:

<cffunction name="createtag" access="public" output="false" returntype="model.snippets.libs.tags.tag">
<cfargument name="tagID" type="uuid" required="true" />
<cfargument name="tag" type="String" required="false" />
         
<cfset var tag = createObject("component","model.snippets.libs.tags.tag").init(argumentCollection=arguments) />
<cfreturn tag />
</cffunction>

My 'tag' table has a column 'tag' - so the generator does what it always does - made my bean with the arguments supplied to it. Unfortunately my decision to name both the object and the column 'tag' led to the duplicate variable naming and thus the error.

Thankfully I caught the error rather quickly (which gives me a bit of confidence that I'm starting to "get it").

Bottom line is - like many have said - don't rely on code from a code generator to work "out of the box". Make sure you fully understand what's going on before you simply plug it in to your app.

Comments
Can I just say that having a table with a column of the same name seems a little strange to me? How can a tag have a tag as an attribute? :)

But, yes, code generators can create some very subtle, hard to figure bugs because of naming conflicts so this is a good post for others to be wary of.
# Posted By Sean Corfield | 7/9/07 12:22 AM
Ah - typo above! It should have read my 'tags' table has a 'tag' column. My object name 'tag' (representing a single tag in the tags table) still caused the conflict though. May have been better to name the column tagName to avoid this all together.
# Posted By todd sharp | 7/9/07 8:46 AM
As you know, the many who say that the generated code is intended to be modified by hand include me.

The generator itself knows nothing about your app...it just applies table metadata in the form of XML against templates...so it isn't going to notice naming conflicts. It also has no idea, of course, of the intent of your app. All of that is up to you to code into there. The goal of my generator is simply to give you a head start and eliminate some of the most tedious portions of the coding.
# Posted By Brian Rinaldi | 7/9/07 9:09 AM
Of course, the alternative is to add some fairly simple checking into your generator. I'm not saying this is what Illudium should do, but one of my projects for our in-house generator is to compile lists of reserved words and other validation issues so we don't generate code that won't work out the gate.
# Posted By Peter Bell | 7/9/07 4:35 PM
Would have responded sooner, but somehow your comment was flagged as spam Peter. Have you blogged about how you handle that? I'd be interested to see how you pull that off...
# Posted By todd sharp | 7/10/07 9:12 PM
Hey Todd,

Haven't done it yet, but I treat metadata like any other kind of data which means I can use the generic validation, transformation, ORM and other code I have to perform operations on my metadata before allowing the user to save it and passing it to the generator. Because of that, I'd just have a custom validator called MustNotMatchList with an enum of values that the name of a column property wouldn't be allowed to match (or the PropertyName property if you make the column name identical to that in your metabase). If it did, the user would be shown their form with the error if they were using the cms to enter the metadata, or an import error would be thrown if they were trying to import an XML file they'd written containing the metadata.

Make any sense at all? The only problem is that I work with metadata SO much, I have an awful lot of tooling that I take for granted . . .
# Posted By Peter Bell | 7/11/07 10:01 AM
Yep. Makes sense. This is all in LightWire?
# Posted By todd sharp | 7/12/07 12:29 PM
Nope, afraid not. LightWire is JUST Dependency Injection (think ColdSprings ugly baby brother). This is all in an in-house framework called LightBase which I will probably not be releasing only because I need to keep some of my software to make money selling to my clients :->
# Posted By Peter Bell | 7/12/07 1:12 PM
# Posted By Ouz Demirkap1 | 10/2/07 11:31 AM

Calendar

Sun Mon Tue Wed Thu Fri Sat
  12345
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31   

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex misc model-glue off topic personal project learn slidesix sql

Recent Comments

More CF+Java: Compiling Classes And Persisting Objects
Getburl said: I have been attempting to get Db4o working in my CF application and I have not succeeded. I would lo... [More]

Thoughts On Ajax Frameworks And ColdFusion/Adobe
Erast said: http://fanniecollins.10gb... emo http://gracetrevino.phree...... [More]

Extending Ext With Ext Extensions
Erast said: http://fanniecollins.10gb... emo http://gracetrevino.phree...... [More]

CF Needs An Open Source Contact List Importer
Kay Smoljak said: Heh, the fact that sites DO it doesn't mean they SHOULD. To us it's ok, but to a non-tech-savvy user... [More]

A Few Project Updates
Helena said: Now punctually what is the situation ? [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com