Smarter Code

As some of you know, programming is a constant quest for knowledge. It’s probably one of the reasons I enjoy it so much. Anything else, say, not coding or doing stuff that is less mentally stimulating and I fall asleep. This was especially true of jobs that didn’t offer anything stimulating after the initial learning curve was accomplished. Truck driving was extremely interesting at first but as it became routine I would find myself micro napping, smoking or pulling over and simply taking a nap to keep myself from driving off the road.

My initial project is coming to an end. I have the forms in place, the database assembled and everything is pretty much ready for testing, then staging, and then release. Honestly I am scared shitless. Today and tomorrow I am spending the days cleaning the code and optimizing it by removing unused functions and other things to make it self contained.

In the mean time I have started an AJAX based polling system from scratch. I feel like I am in a pressure cooker where I am cramming as much knowledge into my head as possible. And really, this is making for smarter code too. I wish I had some of the knowledge I have now back at my previous job.

For example, functionalizing things is one of the first steps in writing more efficient code. Its making more robust functions in themselves that takes a next step forward. I was faced with a problem where certain fields on a form be disabled if the radio button next to them was disabled. Here is the JavaScript code:


function disableFormElements(activeButton, activeElement1, activeElement2, 
         inactiveElement1, inactiveElement2, inactiveElement3)
{
    document.getElementById(inactiveElement1).disabled = true;
    if(inactiveElement2 != '')
    {
        document.getElementById(inactiveElement2).disabled = true;
    }
    if(inactiveElement3 != '')
    {
	document.getElementById(inactiveElement3).disabled = true;
    }
    document.getElementById(activeButton).checked = true;
    if(activeElement1 != '')
    {
	document.getElementById(activeElement1).disabled = false;
    }
    if(activeElement2 != '')
    {
	document.getElementById(activeElement2).disabled = false;
    }
}

Now for some of you this will be very elementary and for some of your this will be way over your head. I don’t think I am any sort of authority but I do want to share what I am doing. Instead of creating a new function for all the different sort of options available I made it scalable. If the element is blank then don’t consider it. This way I can call the function like this:

onChange="disableFormElements('itemThreeNew', 'itemThree', 'itemThreeImage', 'itemThreeSelect', '', '');"

And since the function looks to see if they are filled I can pass ‘ ‘ and it won’t break the function. Before, I would probably have written many smaller functions that were customized to each usage. This way I have a general function I can add to that won’t break other references to it. So now there is one function that is written in a way so that its usable elsewhere. I would still require some maintenance but I am sure that with time I will learn more about it. But before I get too wrapped up in the polling system I am going to start learning and implementing MySQL Stored Procedures. Anyway, there you go.

This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Smarter Code