Thursday, April 07, 2005

Frustrating Geek moments

And yes, sushi boy, I called myself a geek.

There's a lot to be said for code re-use and avoiding hardcoding values. Heck, there's a lot to be said for a lot of coding standards that I never really thought about until faced with poorly written code.

First, I had a tiny little run-in with the old "GoSub" function. 5 pages of code for one subroutine in a notes agent. Even worse, all of this code is stuffed into the Initialize event. And I need to make changes to it. So, off I go, sure that this will be an easy process - after all, it is written in lotusscript and LS is one thing I understand.

But wait? What's this? GoSub? Hmmmm...I can still remember being taught about GoSub and other statements like it. Typically bad practice to use them. I sigh and continue on...How bad could it REALLY be? So, I jump my way around the code trying to figure out where the code exists that I'm looking for. Trying to mentally step my way through this. When I find a subroutine embedding inside another subroutine I stop and suppress insane laughter.

After sharing my pain with a couple friends who would understand. I print the code and head out for lunch armed with my pen and the code from hell. I finally make enough sense of the code - which for the record contained 9 subroutines, not counting error handling which is to be expected. Find where I need to change the code - woo hoo, I managed to eliminate an entire sub. 8...8 examples of bad coding Ha Ha Ha! (Wow, weird Count moment.) I seriously consider fixing at least the part of the code I had to work with and moving it into a proper subroutine. Then realize I don't want to mess with the additional testing that would involve to make sure I'm sending all the right values to the new routine.

So the bad code stays.

Code reuse. Two words that will forever be burned into my brain. Maybe I should get a tattoo to that effect. A heart, but instead of a name in it... "Code Reuse". Heh, how geeky is that?

I am currently working on a lovely piece of Javascript that validates a form submitted on the web. Inside this little script we have a ton of lines of code that basically create an array of values to display to the user if they neglect to fill out any of the required fields. Now this little piece of code is valuable. How often do you have forms you need to make sure contain all the required data? All the time. Most of our apps have some form of this. So what do we do? Make a new script each time and hard code the fields we're validating. Sigh.

I may not have agreed with everything my product manager came up with back at Roam. But I did learn a lot from him about code re-use. And Validation scripts are a great place to reuse code. What we did back then was create a function that would take an array of field names, process it, and spit a list of empty required fields back to the user. The array could be set in an admin section by the end customer, or it could be set in code and handed to the function depending on the needs for the app

It was a thing of beauty. And today, as I try to add and remove required fields from our application, which uses multiple forms resulting in multiple places for the changes. I wish I knew more about Javascript so I could write the function myself and be done with it.

Code maintainability. What a wonderful thing.


Post a Comment

Subscribe to Post Comments [Atom]

<< Home