Monday, February 22, 2010

What is in a mantra, and why do we have them?

You know, earlier I was helping a friend with some programming questions he had, and as I always eventually do. I shared some programming mantras that I live by while programming. Then I thought to myself, just what is in a mantra? Why is it that we ever were hear that word, it is almost treated with some spiritual respect? So I decided to look up the meaning of the word. What I found did not really surprise me.

The American Heritage Dictionary defined the word mantra as having a background in Hinduism. It said "A sacred verbal formula repeated in prayer, meditation, or incantation, such as an invocation of a god, a magic spell, or a syllable or portion of scripture containing mystical potentialities." source. I thought about this for a bit. Why do we have mantras that we live by? Are they not a code of life that we follow? In a small way, are they not a prayer to something? Even if it is so that we do not mess up? All of this was going through my mind as I was talking to my friend. I decided that I would like to share some of the mantras that I live by with you. Some have a very deep programming meaning, but others are more philosophically based.

The first one is when programming, "Change once, change many". I do not know who first coined this term, it might have been me or someone else might have said it and I just over heard them. But when programming it just makes so much sense. If your program is more then 500 lines long, and you suddenly have to change a value that you have used throughout your program, wouldn't you want an easy way to change it?
Things like global constants do this for you. If instead of putting that value every where you used it, and instead put the constant. Then all you would have to do is change the constant's value once and your entire code is updated! What could have been at best hours worth of work, at worst you have program destroying mistakes if you missed any. Now is done in less then a second. This term can also apply to good "code reuse". Where possible, use code already available instead of writing a new function. At best not doing this wastes hours of your time. At worst it makes you look like an idiot.

We've all heard of the golden rule, "treat others as you would have others treat you". What is so hard about this? I'd like to say this mantra is common sense. But then I am forced to remember that common sense is not all that common. Is it really that hard to fathom that if you treat others like a jerk, then they will be a jerk back to you? Even if its not the same person, word gets around. Soon enough you will be known as the jerky one and you can only hope to expect rude comments, or worse, being sent your way. The opposite is also very true.

Now this next mantra I learned from my father, who has always been a beacon in my life. He says that in programming "I love the maintenance programmer and I want him to love me". For those of you who are not programmers. When someone writes a program, eventually that code will be cared for by a "maintenance programmer". Someone who is paid to do just that, maintain the code. He is in charge of updates, keeping it current, responding to bugs and so on. Making his job easier is just a nice thing to do.

With everything in life there is always the polar opposite of it.This next mantra is no exception from that rule. Just as my Dad's mantra is motivated by love, this one is motivated by, interest in preserving one's own life. I got this mantra from some tip of the day picture about programming. The message reads, "Pretend that the person who will maintain your code is a serial killer and knows where you live". The message here is, if you write code in such a way that it drives the maintenance programmer crazy, he will come kill you because he knows how to find you. Can't we all just get along?

Now for you programmers out there. How many times have you looked at someone else's code and been like "What in the world is he doing!?". Yea, I had one of those moments earlier. You look at their code and you can't even fathom what is going on for a few moments because of how convoluted and strange it is. Have you ever had someone hobble together a else-if statement using a while loop and a bunch of if's? I subscribe to the Daily WTF. It's a really funny site about how some people just royally screw up. Sometimes the mistake is bad, but other times it just makes you want to burst out laughing. This guy on the Daily WTF said that his company had subcontracted a company to write a portion of their code. When they got it back they saw this inside of one of the functions:

while(true)
{
    if(mainType == 7)
    {
        subType = 4;
        break;
    }

    if(mainType == 9)
    {
        subType = 6;
        break;
    }

    if(mainType == 11)
    {
        subType = 9;
        break;
    }

    break;
}
The only reason for the while loop was so he could use the break command to jump over parts of his code when he needed to. The poor guy obviously had no idea how to use an Else If statement. A switch statement would have done the same thing he did here and would have been more eloquent.

So my fourth mantra that I try to program by is "The sign of good code is a low number of WTF's per minute". I did not make this up. Xkcd.com has all of the honors here. They even made a nice picture for it.
In today's world, the phrase "wtf" isn't always a bad thing. Ironically, it can be a good thing, like "wtf! that was a crazy basketball shot!". Personally I don't understand this line of thinking. I think it's a little crazy for this to be sign of a good thing. But in the case of programming, it is okay for your code to do some crazy things. In fact that is encouraged. It is what separates the good programmers from the bad. heh. I am not saying where I fall in this line up; because if I tried to, I know there are some others would put me in my place very, very quickly. But really. When programming. Keep it simple, keep it clean, keep it in order. Don't just write functions just anywhere, all namby-pamby like. Have some rhyme or reason to it. Maybe they're in alphabetical order. Maybe they're in execution order. It doesn't really matter what order they're in. Just make sure it is some order.

In everything you do in life, do it with some purpose. As some would say it "act, refuse to be acted upon". We choose our actions. We choose how we will act or react in certain situations. Truly, all we can blame is ourselves. No matter how bad life seems to get, always keep your head up. Everyone must suffer through pain, the nights of our lives. But with night, there is always the promised dawn. Look towards it.. Move towards it. Tell yourself that you will make your day come. Often the day's in our lives wait until we pursue them. No matter how bad something seems it will always get better. The pain will lessen, the tears will dry and we will find a time when we can look back and not cry. Remember to hold some mantra dear to you. It is your code of life. Your honor code if you will. Respect yourself and life will respect you.

1 comment:

  1. I don't know if I should be pleased or scared that I actually know what you're talking about.

    ReplyDelete