Breaking rules

This post is more than 2 years old.

If you read my blog regularly, you know that I have certain "rules" I tend to harp on. Always var scope. Always check your input scopes. Etc. However, one thing to keep in mind is that there are perfectly good times to break the rules. Development rules and processes are there to ensure your code works well, is secure, and can be easily updated. When the rules get in the way of those things, it's time to reconsider.

Here is a good example. In my blogware, I support localization of the blog by use of a "resource bundle". This is basically an INI file with translations. So instead of having:

<input type="submit" name="cancel" value="Cancel">

I have:

<input type="submit" name="cancel" value="#application.resourceBundle.getResource("cancel")#">

What this does is look up in a translation file for a key named 'cancel' and returns the value. So one translation file exists for English, one for French, etc. (Actually, I don't ship a French file, but BlogCFC users can write their own.)

So this works great. But it is a major pain in the rear to type. So, I just added this to my application.cfm file:

<cfset rb = application.resourceBundleData>

Application.resouceBundleData is a struct of keys and pairs. I can now do this in my cancel example:

<input type="submit" name="cancel" value="#rb["cancel"]#">

This bugs me. First, the variable name is way too short. Secondly, there is no method call, so if the key doesn't exist I get an error. The "old" way would return a nice UNKNOWNTRANSLATION string to let you know. Third - I don't like setting Variables (note the cap) in the Application.cfm file, and when I do, I at least use the full scope.

So I don't like it. But I'm over it. My code is now a heck of a slot easier to read and work with. How do folks feel about things like this? I tend to be pragmatic so it doesn't really bug me much.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA