Handling The Junk Code

While writing applications most of us have the bad habit to abandon few pieces of code here and there in the process of optimization and performance tuning. Few lines of codes get completely replace by better ones and few need little tweaks here and there though, there is a good amount of code which is replaced by better ones not because they are useless but not suitable for that particular situation. Since we know this truth, we find it very hard to delete that code, instead we keep a copy of it somewhere in the same program in an area where few other codes are dumped. This place looks like a junk yard which has high potential but definitely not very organized one.

If somebody likes the code very much and does not want to throw it into the junk corner, they hide it under the comment section and put add a TODO or UTILS line above it, surely a very bad practice for production.

code

So, What to do with this Lovely Junk?

So what to do with this box full of good but unused code and where do we put it so it can be utilized in the later course of time? Definitely this box has to be kept far far away from the production code. Why? Since these codes have been replaced by others there is a big chance that the new code(generally a method, function or a Class) happens to have the same name and often same signature too. Its pretty clear that having multiple codes with same signature can have catastrophic effect in the later run when a new developer join the team.

A good solution would be to take the code away and put it in a repository where it feels safe and can be revisited and shared with other members. If the project is Open Source in nature, putting the junk code in an online code repository and in the organization’s own private code repository. One thing that has to be taken care of is that, it has to be put in a repository separate from the actual project. This will surely save time as developer does not have to re write the code again from scratch.

Happy Coding 🙂

Advertisements

Why I Hate Software Developers

Being a Software Developer I know how slow and counter productive developers get with time. Its not that we don’t get time to meet the deadline(although exceptions are always there), it is just our thought of thinking ourselves as highly efficient and over qualified.
We always hate testers since they put us back to work and make us clean up our own mess. But it is just their job to put developers on job and guide the software development on a right path. Sometimes I feel, it is the testers who put the developers on the right track.
But wait, sometimes even testers get lazy too. The code in alpha and pre releases gets their nod to be released but when the code is in production suddenly some tester would raise a finger on a very trivial issue that should have been found during the pre release stage.
This time I as a developer think how stupid and lazy the tester was for not to find this trivial bug, but eventually this thought comes back to me as a boomerang asking why on earth did the developer not notice such a blunder. At this time I feel serious identity crisis for myself.

 

The Dream:

There is a saying “Good developers write their own code, but the best developers utilize already written code“. Now having said that I must admit, almost every coder has a dream to right the code from scratch and never wants to manage and clean up someone’s mess.
Apparently very few lucky ones get that chance, rest of us have to cope up with the situation. The situation gets even worse when the code has already been changed by more than four or five different people. The person currently working on the code has to somehow understand and manage the coding styles and thinking of coders before him.
The best thing is that the current developer will surely manage to get the code working. This situation remains almost every time the developers change their jobs. There is a worst thing about this: no manager will let you get away with this silly argument, even though they themselves understand the situation very well.

 

What exactly did you do? This is just a minor change; This is a general question asked by several managers. The problem gets worse if the change is related to UI. The developer has to take care of several others elements nearby to move one simple button from left to right. In the outcome(from a user’s or manager’s perspective) it is just a re-positioning of a button, but it is the developer who knows the pain behind it.
Yes it is true that a good coding practice can reduce the amount of work, but don’t forget that the code has already been modified by several other developers who are not in the project any more, so the current person in the developer’s cap has to bear it.

 

Despite of all these things and situations we somehow learn to “MANAGE” and not to break any existing functionalities of the software. With time we get mature and acquire better skill sets to provide better solutions to problems and sometimes you get appreciations too for that. This little piece of appreciation helps developers a lot to again start thinking themselves as a better creatures, hence the cycle of fall and rise continues. We don’t get out of it and that’s why I hate software developers or more specifically myself.

When the Middle Finger and the Thumbs Down were locked out

mr

So here is how the situation goes:

One day the Middle Finger and the Thumbs Down were accidentally locked in a room. Tried everything to get out of the room but no help. Frustrated, tensed, and abject. They found themselves all alone with nobody around.  Thinking of their ego and superiority over each other none of the two was ready to work out for a solution or a way to get out of the room. Eventually fell into a spat accusing each other for the situation and finally, they agreed to have a conversation about their superiority and the less superior will have to look for the solution.

Below are some portions of that conversation. Now some of the readers might ask if they were alone, how did I manage to hear their conversation!! Well, if you have read the above lines carefully you should notice that “They found themselves all alone”, which doesn’t imply they were actually alone. I was just wandering there with my friends and happened to listen their talk. So just don’t ever lose your hope or you will find no way out of the problem. Also I tried to use short version of their names, so here is how I will be referring them:

MidF for Middle Finger and TDow for Thumbs Down

Now here is the conversation:

MidF: I am the new favourite of the world and you are just an outdated and OLD guy of which nobody even thinks. How are you superior to me? What have you got that I don’t have?

TDow: Well, you just said that by your self.

MidF: What did I say that makes you better than me?

TDow: You just said OLD guy.

MidF: Yes I said that, so what? How does that make you better?

TDow: Well, my friend, I accept I am OLD, but don’t you think being old means I am used by large number of people and that too for a long time span, which eventually makes me more experienced. Don’t you think so?

MidF: Aaaaah(what have I done)!!!! Okay fine, I accept you are more experienced. But do you know I am one of the most rapidly spreading gesture to show “dislike of ones actions or thoughts”.

TDow: True my friend, you are surely among the most rapidly gestures being used these days, but!!!.

MidF: But!!!! But what.

TDow: But, does it ever bother you about what the other person thinks about the person(using your gesture)?

MidF: Why should I care about what other person is thinking.

TDow: Exactly. You Don’t CARE. Lets me rephrase it for you a little “You are careless about others feelings”. This can lead to bad relations between people and sometimes even ruin them.

MidF: Aaaaah(again I did it again)!!!! So what??? What about you? What do you do to care about others feelings?

TDow: Not much. Showing a “thumb down” makes the person think a little about their action. For example, What was the reason that people didn’t like my act or speech?. 

MidF: Is that all you have got or do you have something else to add?

TDow: Nothing much, as I said “thumbs down” does not hurt others feeling, its not provocative to be displayed in public, unlike You(Middle Finger) my friend, who can be interpreted as provocative and vulgar by general public and cause a big chaos.

MidF: Okay Fine Fine….. I accept all these charges on me, but my friend don’t forget there is always another side of the coin.

 

To be continued….

English And My First Computer Program : How I learn(ed) them!!!

Back in mid 2007 I just started my graduation in Computer Science. Prior to that I had not much knowledge about several things related to computers. If I compare myself(of that time) to some of my classmates, truthfully I have to give myself negative points in several fields whether it was computers or it was about English language like speaking, writing, listening or English songs. In fact before one of the cultural events in my graduation college, I didn’t even know about the existence of English songs. In that event one of my classmate sang an English song, and I was awestruck by the performance and at that time I was feeling the most illiterate person in the whole auditorium. The reason might be that I didn’t belong to English medium school. This reason is the last resort of almost every Non-English school students who can’t speak English as good as others. All this lead me to listen and memorize some of those songs. I am now able utter few English songs too. I had to do this because the college had only English as the official language.

Now in case of computers I at least had no doubt about their existence(as in the case of English songs) as I myself had heard and even seen some of them. I liked them very much. I specially remember the one that one of my relatives had brought to our home. I as a high school student was not allowed to touch it; but I was quite fascinated by it. By the time I got admission in my graduation college I had only done some painting using the then favorite MS Paint. I also knew about how to play with the screen savers; putting my name on it and trying to rotate it randomly.

Having said that the foremost question arises: why did I choose enroll for a Computer Science degree without any good prior knowledge of computers. As I said above I was very keen in computers and was not allowed to touch the computer brought by the relative guy, I had decided at that time, to have my own computer someday. So that fascination drove me and I eventually landed to a Computer Science degree.

We had a period of programming lab with the most famous one: the C language. I still remember the first day in the computer lab. The instructor expected all of us to have some knowledge of basic programming, but for me(I am sure their were few others too) a “LANGUAGE” of computers was completely an alien idea. By my good luck I had a friend who had done some programming earlier too. I was so relived just by that fact.
After entering the lab we were asked to write a program that produces the “sum of two numbers”. Just after listening the instructions I thought to my self “What!!! How stupid is that!!! Why the hell do they need a computer and a program to sum two numbers??? Addition is a very simple math to do. I can do that even on my fingers.”
This thought really bugged me for a while, but finally I went up to my friend and asked him the same question. He smiled and said we need to do it and tried to abase my curiosity, but whatever he said all passed above my head.

But as days passed, I wrote programs to identify the leap year and program to convert Celsius to Fahrenheit. Slowly I got to notice that these programs are just the minimal things that a computer can do and were there just for the demonstration of programming languages. We got to know about Internet and Orkut.com by the end of the first semester. Things went good and I learned(although I still struggle in them very much) C and Java and the HTML and JavaScript languages a bit and finally I got my degree.

Since then I still get amazed by every new language I encounter. Similarly the English language makes me very nervous when a good English speaker is in front or when I have to write an official email. But at the end of the day I feel a little satisfaction and happy in my heart as I know that learning is just a continuous and parallel part of life which can’t be avoided and there is know life without learning.
So I just learn them(some day very little and some day a good amount) everyday will continue to do so as I progress in my life.

Have a happy learning:)

JavaScript Programmer ? Do you also ignore semicolon?

A little story of two JS Programmers ==> Alice and Bob

Bob: Hey Alice. You know what, I have learned JavaScript very well. It’s very easy and fun.

Alice: Good, but you still need to be careful while writing you JS program.

Bob: Are you kidding me ? Careful with JavaScript? Several times I escape putting the semicolons at the end of the statement and you know it still works like charm.

Alice: You must be kidding. You shouldn’t forget to put the semicolon after the statement.

Bob: I am serious, who wants to put that reoccurring and irritating semicolon.

Alice: Of course JS ignores such things at several places, BUT  it doesn’t mean you should abuse this behavior.

Bob: Why not? I can do the following and it works perfectly fine:

    console.log("Hello! I missed the semicolon.")
    console.log("Hello! I missed the semicolon too.")
Output:
    Hello! I missed the semicolon.
    Hello! I missed the semicolon too.

What harm does it do?

Alice: Well, I think nothing till now. Bob can you do me a favor? Can you just put both the statements in the same line as it is?

Bob: Sure!

    console.log("Hello! I missed the semicolon.") console.log("Hello! I missed the semicolon too.")

Output:

    SyntaxError: Unexpected identifier

Bob: What!!!!!!!!!??????????
Bob: But wait, putting two statements in the same line is itself not good.
Alice: Fine(probably he doesn’t know about minification). Ok tell me the purpose of this code:

    +("233434");

Bob: Its just a way to convert the string to number.
Alice: Exactly. Now what do you expect from of the following code written in your style:

    var a = 0
    var b = 1
    +("23456")

Bob: Its too straight forward: 0 is assigned to variable “a” and the value 1 is assigned to the variable “b”.
Alice: Are you sure?
Bob: Wait ! Wait ! Wait ! Wait !. Oh I see the problem.
Bob: Hmmmm! You are right I shouldn’t miss my best friend semicolon.

Generating Unique Strings (Ids) in JavaScript

Well, there are tools in JavaScript that allow you to create unique ids or strings.

I will be using to two ideas to create these unique strings.

Method 1: Memoization

The idea is to generate some random string and checking if it has already been generated the recursively generate another one and test again. If the generated string has not been already generated, then simply cache it somewhere and return it to the calling function.

Here is an example of it:

function getUniqueId() {
    getUniqueId.cache = getUniqueId.cache ? getUniqueId.cache : [];
    var randomString = "ID" + Math.random().toString().substr(2);
    if(getUniqueId.cache.indexOf(randomString) === -1) {
        getUniqueId.cache.push(randomString);
        return randomString;
    } else {
        return getUniqueId();
    }
}

Method 2: Closure
Closures are on of the coolest things available in JavaScript. Closures allow us to access local variables of a function when you are out of he scope of the function.
Doesn’t it sound a little crazy: “How can a local variable of a function can be accessed while we are not in the scope of that function i.e., the variable is out of the current scope!”.

Well its true that we cannot access the out of the scope variable of a function, But the variables and functions local to that variable can access it right. This is the concept we will be using to generate our unique Id or string.

Here it is.

    var getUniqueId = (function () {
        var local = 0;
        return function () {
            return local++;
        }
    })();

In the function above we are returning a reference to a local function, for which the variable local is in the scope and can still increment it. So every time we call the getUniqueId function actually the inner function also gets called and every time we get the incremented value.

Issues with these approaches:

Both of the above concepts are pretty cool to use, but you know nothing comes for free. Both have some performance and memory(space) issues with them.

If we carefully analyze the situation when we use closures, we are accessing an out of scope function and we all know that accessing out of scope variables is much costlier as compared to accessing local variables and functions in terms of performance as JavaScript engine has to search for the variable which is sitting far-far away from the current scope.
The situations worsens if the function has been defined in the global scope, because we have to go all the way to the global scope to get it.

Now if we take a look at the Memoization approach, we will see as the list of cached items increases, the function will tend to slow down. This slowness is because of the fact that before returning the the generated string, it has to traverse the whole cache. This slowness increases as the size of the cache grows.

There are some memory issues too with theses approaches because we in case of Memoization are maintaining a cache and in case of Closure the JS engine has to preserve a whole scope and its members.

So using these tool with care is necessary or our program would run into other issues.

PS: Lets get lazy a bit
Now here is the laziest(and easiest) way to generate unique strings:
 
Date/Time

function getUniqueId(padding) {
   return padding + (new Date()).getTime();
}

 
I hope this last function is very self explainatory.