Wednesday, May 23, 2007

Different ways to program

I've been told that there are different ways to program. One way is procedural, where you work with something (let's say a shopping cart), and it does one thing, then it does another, then another, and so on until oblivion. Then there is object-oriented programming, where you have objects that you deal with (like in real life!) and not just random things that you happen to write code about. So your shopping cart has attributes, like it can hold items, you can remove items from it, and when it is time to checkout you remove all of the items from the cart and pay for the items. And just like at the grocery store, you leave the cart at the store (hopefully).

I guess the difference programming wise is that procedural programming you just write a whole slew of functions that can be applied anywhere and it is the programmers job to decide when and where he should use them, whereas in OOP you have classes for your objects, and each class has specific functions to deal mainly with instances (objects) of the class. I'm sure you can have the guise of OOP and still be doing procedural programming.

I only mention all this because I was looking through a website that the client I am working for said to look at and try to mimic what they were doing with some javascript showing and hiding. It all boiled down to that they were toggling the CSS display property on DOM elements between "block" and "none" (the show() and hide() functions in Prototype). Well, I finally found that out after looking on in amazement at a 1211 line function that seemed to show and hide everything on the entire website! Notice that I said function. They just used this massive catch all function to do all the showing and hiding, instead of trying to somehow abstract it into some showing/hiding class thingy (don't ask me how I would do it though, because I have no idea). Maybe I would have done it like that if I wouldn't have gone insane trying to remember exactly where I was in the 1200+ lines of code.

And as a side note, look at this code:

if (getFormObject("reslease","taxes")[0].checked) {
if (getFormObject("reslease","taxesTenant")[0].checked) {
} else {

if (getFormObject("reslease","taxesGovt")[0].checked) {
} else {

} else {

That was from line 1070 to 1086. After almost 1100 lines of madness, how do really know that you want the first element from the "reslease" form? And what is a "taxesTenantDescDiv"? My mentor has been drilling it into my head to make whatever your coding self-descriptive, so that when you go back you never have to wonder what the "taxesTenantDescDiv" is, or where it lives.

No comments: