BudhVaar

Budh = Mercury. Budh is also the root word for brain. Also Buddha. This day is mid-week. Most work done. This section will cover some of the interesting findings in different fieds of academics.

My Photo
Name:
Location: Delhi, Singapore

Sometimes difficult but mostly understanding; sometimes fun but often moody; sometimes alive and other times plain dead.

Tuesday, June 27, 2006

Programming like an Indian (Continued)

Algorithms

From the previous article we peeped into the very English meaning of programming. I had given examples of how everything around you is programmed to work the way it does. Every program or process has some inputs, some logic of dealing with the inputs and some outputs.

Let’s take the example of making an omelet. Well, I need an egg, some salt, 1 small onion and a non-stick frying pan. Now, you’ve seen mathematical equations where you write:
y = x + 2 = f(x)
where x is the input and y is the output. This means that if you put x inside the function x it will return y i.e. x+2.
Can we write a similar function for making our omelet? Yes, we’ll try:
Omelet = egg liquid + chopped onion + pinch of salt + heat
= f(egg+onion+salt+heat)

Using this we can write an algorithm as follows:
procedure make_omelet (egg 1, onion 1)
begin
omelet = fry(egg, onion)
output omelet
end
end procedure

While we can see that fry() is another procedure which is used in the algorithm above, it still puts the right idea and line of thinking behind any process. I believe therefore, that if you understand any process and the steps that are carried out to achieve that, you’ll know how to come up with the algorithm for it.

If I wrote the details of the sub procedure within this procedure, it will be something like the following:
procedure make_omelet (egg 1, onion 1, salt 1)
begin
new frying pan, spoon, knife, chopping board
chopped_onion = chop(chopping board, knife, onion )
egg_liquid = hit_open(spoon, egg, frying pan)
frying pan = heat(frying pan)

do {
omelet = fry(frying pan, egg_liquid, chopped_onion, salt 1)
}
while omelet is done

output omelet
end
end procedure

The second approach makes the entire process literally into steps, one after another with the basic action items still used as functions like chop, fry etc.
The do { } while is a conditional approach to carry on an activity until some condition is reached.
This second approach is called procedural programming. This is the basic style of programming which is used in Basic, Pascal etc. Here everything is seen like a string of steps one after another. What is the other form of programming then?

Imagine the above process to be very complicated. Maybe the required shape of the omelete the size of onions and amount of salt has to be very presise. In procedural programming, it feels like the same ‘person’ doing the job one step after another. There is no help provided to him. He may be good at chopping onions but may not be good in frying.

Well, object oriented programming focuses on the various things required in a process as objects and accomplishes a task by creating a lot of specialized units that take in certain objects and output certain objects. In the case I presented just now in the previous paragraph, it would be like the ‘person’ having help from an expert in chopping, and expert in frying etc to put their skills together to achieve one big task.

To summarize, I present the following way of thinking:

Procedural


Get the onion, egg, salt, frying pan, knife,
Chopping board, spoon

Chop the onions with chopping board, knife

Break egg with spoon

Put the two in the frying pan with the salt

Heat till its cooked

end

OOP

get onions, egg

call the frying_pan function With the above input and get the output

end

The actual process time might be the same for both but the one on the right looks more organized. If it was a big kitchen and the entire breakfast needs to be prepared. Someone following a procedural approach will lead to 5 cooks all needing the knife and frying pan at some point in their process as they follow the steps. On the contrary, the cooks using OOP approach will assign one cook as the person who chops, another as the person who fries etc and hence get the work done in a more organized and specialized way.

I will be expanding the concepts of both Algorithms and the two programming approaches in the next article. Do leave your comments and questions if any.

- Naveen Kumar
http://uglymoth.blogspot.com

0 Comments:

Post a Comment

<< Home