Tuesday, December 2, 2008

Part 3: Thoughts of “How we do?” - In Programming

We have a problem, we want to solve with computer. The computer is dump and we have to write a program, but it will run that for us any number of times without tired. Today lot of programming languages has invented, we are in high level language era. That is, we write programs in a language more like English/Natural language. Then have to convert those programs, mainly those will contains instructions to execute to perform some tasks, into machine code which can be understood by the machine/computer. These programming languages will contain rules or syntax to follow, any body can learn it.

 

Even though, if we know the syntax, we can't write a program for a given problem. We have to think what are the instructions to be run and in which order. We called this as algorithm. Some how, we have to identify the algorithm and build up them as a full program. Writing a program is not that simple as writing an essay. Our mind has to tell us what should be written.

 

From a simple problem to a complex problem, we should be able to think and write a program. It may be difficult, but our way of writing program definitely lead our brain think faster and find the solution for us.

 

In programming languages we have lot of opening and closing, for example in Java, a class body will be opened with “{“and closed with “}”. Improper closing makes compile errors and lead to spend more time to find out them and close currently.

 

Say we want to write the following class:

class MyClass{

     void method(){

     }

}

 

we can write this first line (class MyClass{), then second line (void method(){), after that 3rd line only } and finally } to close the class.

Or

Firstly we can write “class MyClass{}” then move the curser inside the “{}” and press enter key two times and move the curser to the second line and write the “void method(){}” etc.

 

In the second method, we may have to do more work/press more keys, but in the first method we have to keep lot in our mind, such “have to close the class body”, the logic etc. In the second method, once we closed the class body, our mind is free to think the actual solution, no need to think any think about those unwanted language syntax etc. In programming we have lot of like this, () - for method parameter, “” - strings, [] array etc. So, if we closed all of them immediately we opened and then think only in side, after that we should be able to see good improvement in our performance.

 

You may wonder, this is a simple thing, and we don't need to even think about this because our favorite smart integrated development environment (IDE) (Eclipse, Visual Studio, etc) would do for us. Of cause, yes! Now a days, all this IDEs are doing this. Even if I happened to evaluate a new IDE, I used to check this first. But, I wanted to check ourselves more in the following two cases.

 

First one, in case if we wanted write a program without IDE or in a new programming language witch doesn't have a smart IDE so far, then are we going to write in which way from above? If we are not going to follow the second way, then we don’t learn that our IDE teach for me.

 

Secondly, I could see lot more opening and closing in our programs, but even a smart IDE doesn't do for us. For example say we want to write a method, which suppose return a list of objects according to the logic. So I will start with method.

 

List doSomthing(){

 

}

 

Fine, what is next, I have to create a List and fill that with objects according to the logic, then return. But here I could see another opening and closing, that is, creating a list and returning it.

 

So, I prefer to add following lines to the method first before start other coding.

 

List doSomthing(){

     List result = new SomeList();

     //Think and write the logic here

     return result;

}

 

Now we have to think only about the logic, and that also simplified like as, check what ever you want, but finally add into the result list. So now, we got a direction also for our thinking.

 

Next, if we want to write some thing into a file, first we have to open and write something according to our logic and then have to close, and then only the content will be written into the actual physical file. This is also another example for this kind of open/close scenario. It is great, if we get used to write the open/close statement first before writing the logic in between that. This is applicable for most of the input/output stream programming.

 

It is obvious that we should be able to find more places where we can find the same open/close pattern in our programming life. If we could capture those and bring into our practice definitely it will change our way of thinking and programming. And not only this pattern, we have more patterns/practices which to be looked into. I hope those will lead us to not worry much about “What we do” and to will make us focus on “How we do”.