Saturday, November 28, 2009

RTP#42: Programming

I’ve done quite a number of programs and I’m very sure that I’m still going to do a lot of programming in the future. And what I like most about programming is that I’m getting my chance to execute my plan. And the hardest part in programming is when you’re only starting to do it because it requires one thing that is said to be God given and something you can pull out inside you for you to develop it is called logic. Another thing is how you analyze and view things because in programming you would have to look closer and to dig deeper in order for you to come up with the solution to the problem. Is programming complicated? Yes, but it would depend on the problem and how are you going to handle it.

Try asking yourself “Why is it like this?” or “What is this for?” or “What if it doesn’t have this?” These questions are the same questions that you’ll be answering when someone asks you about your program. Similarly, questions like “What if I’m going to do this before this?” or “What if I’m going to use this instead of this?” are the ones that you should ask yourself when you’re creating a program.

Now in analyzing you should be efficient because even though you can analyze very well there are times that you’ll overlook some details which are very important. This is the most critical part of when creating a program because the result would depend on this stage. If you’re going to interpret the problem correctly then that’s great but what if you went the other way then most probably the things that you’re going to do next might become just a waste of time. Another key is to just deliver what you’re asked to. Print out the required output the way they wanted to and that’s it.

Formulating the solution is only second to the hardest part when you’re creating a program. In this part I’m talking about coming up with a solution not using programming language yet but still the English language. I recommend you to go through this step so that you’re going to have a concrete picture on how you’re going to put everything together in the end. This part would be like the planning part and it’s always better to have one than not have one.

Then here comes the most frustrating part, the encoding. This part would be like the planning part and it’s always better to have one than not. Basically, this is translating your formulated solution to the programming language you’re using. Sounds easy? But it isn’t because you should know the right functions to use and how are you going to use them. And the most difficult part would be figuring out how to combine these functions so that they’ll complement each other and produce the desired result that you want to have. One good thing about programming is that you can have different solutions to one problem. It’s up to you to choose the best solution.

The shorter the better that’s how you’re going to determine a good program. Of course a shorter program that produces the exact output as the longer one is always better. Not only that it would consume less memory on your computer but it shows that the program is more efficient. For example if you’re asked to determine a vowel from a consonant. There are two ways to deal with this and (1) one is to set a conditional statement for to determine if the input is a vowel and (2) the other is to do the same thing to verify if the input is a consonant. Which one is more efficient? If you chose (1) then you’re right because for that one you’ll only have 5 comparisons unlike in the other way where in you’ll be comparing the input among the 21 letters. Here’s the algorithm for that example:

Input “Enter a letter”; letter$
If (letter$ = “a” or letter$ = “e” or letter$ = “I” or letter$ = “o” or letter$ = “u”) then
Print “The letter is a vowel”
Else
Print “The letter is a consonant”
End if

Now that code isn’t perfect because it should still compare for the uppercase letters and it should determine if the input is indeed a letter and not a number. If you’re going to input a number when you run this program the output would be “The letter is consonant”. Same principle applies to the symbols and punctuation marks. My point here is to make each program as perfect as possible. Find all the loopholes in your program and fix each of them. Bugs or loopholes are inevitable but they can be destroyed. The key is to try all possible inputs that would make the program respond differently.

To conclude this post let me share several things that I do when I’m creating a program.
- I make sure that my mind is focused and I’m in the mood to program. If I don’t feel like programming then I don’t program because forcing myself to do it would only frustrate me in and I wouldn’t be able to accomplish anything in the end.
- I drink Coke or iced tea or sweetened cold black coffee because it makes me think faster.
- I listen to my play list while I’m programming at home but in school I don’t.
- For difficult problems I simulate everything first on a piece of paper.
- I tend to close my eyes to think deeper in order to come up with the solution and to avoid distractions.
- I think about someone that will motivate or inspire me so that I won’t give up if I can’t figure out how things should be done.

No comments:

Followers