In the previous article First Step to programming - Programming is an Art
, we had a look at the basic concept of programming and how it should be understood. The article was intended to introduce you to a new world of innovation where you have full control in your hands and you decide what should happen and how. Continuing with the series, today's article is intended to help you in writing your first program. Today I will discuss with you how you should think while starting the write up of your first program. What are the crucial elements and steps that you should be aware of and you should follow so as to become a successful programmer. So lets start with the ideology:
In the past 8 years of teaching and training experience, I have seen that most of the students are hurried in writing a program. As soon as a problem statement is given to students, they just start hitting the keyboard keys and write off a generally useless program or wasting a lot of time in writing the correct solution. The primary reason behind is that the students understand the problem statement and prepare a quick picture in their minds about the target solution and forget to create the path towards target. Creating the path implies building up the logic, planning the variable and the sequence in which statements should be written and executed. So while writing code, students create a lot of variables and redundant statements while mostly become a puzzle and also affects the sequence in which statements should be executed.
Following are the key points which you should keep in mind while writing your program:
1. Understand: Don't hurry in hitting the keys. First understand the problem domain and the problem statement. Then, try to relate it with a real world situation. This will help you find our the required resources and participants more easily and accurately. Try to get involved in the situation and solve the problem in your mind or on paper. Now you know the solutions and mandatory requirements in your mind/paper, you are ready to go for a formal approach.
2. Follow the Algo: A program is nothing, but an implementation of an algorithm under a specific programming environment chosen by the development team. Algorithm is a finite set of statements written in proper sequence that can solve a problem statement efficiently. So, always proceed with an algorithm based approach before commencing your program code.
3. Plan a Flowchart: Flowchart is a graphical representation of your logic and program flow. It helps you in analysing your logic and figuring out whether the things would go fine or will there be any erroneous jump. So working with a flowchart before writing a code is a recommended approach.
4. Plan your variables and program structure: Isolate the participants and entities in your solution and write them separately. Then plan the structure of your program and the variable that would be compulsory to use.
5. Decide the life cycle of variables: Make decisions about variables, if you need them till last or are they going to be used just for once or few times. In case of a variable being used for few times only, try to reuse it in the further code for a new purpose rather than creating a separate variable for each purpose. This will save your time, system memory and CPU time consumed in allocating memory to variable, maintaining heap of memory locations and accessing them. Also remember, the less variables you have, lesser are the chances of a memory leak problem.
6. Decide the life cycle of statements: Try to figure out if a bunch of statements needs to be executed only once or a couple of times. Try to put the repeatedly executing statements into a function rather than rewriting it again and again. Also, try to optimize the location of writing statements which may also affect your program performance.
7. Programming constructs: Go through as many programming language facilities and constructs as possible. This will help you decide the best construct for a situation. For Example, a nested if-else can sometimes produce better results than a if-else-if ladder and vice-versa varying from situation to situation.
8. Concentration: Now since you have planned everything, concentrate on your logic and keyboard. Make sure you are not distracted while implementing the code otherwise you may loose the right statement flow.
9. Comments: Comments are the description lines that are written just for the information of developer. These statements are not compiled or executed by the compiler. You should write comments about every isolated piece of code and even every variable so that it is easy for you to understand the code later on while your are working in maintenance phase.
10. Test: Once the program is written, don't test it only for right cases. The program may work in right situations but all of your clients may not always fill in right data. They might press wrong buttons or might fill in inaccurate data or the data format that is not compatible with your current code. Make sure you try to maximize the test cases and also generate a response code for the same.
After following the above steps, it is assured that you will be able to write more efficient and productive programs with great ease. I hope you have enjoyed this post. Keep visiting for more.....