Monday 7 November 2011

Fundamentals of Coding.



The best way to define computer is that, computer is an electrical device which performs some computations on information in accordance to the instructions given to it. The instructions given to computer should be in compliance with the hardware architect of the computer.
But I am not going to focus on computer hardware organization or its architect, but the more important thing is writing instructions which gives computer its diversity and the ability to do wonders. From first generation languages like FORTRAN ,Pascal ,Cobal,Ada(named after first programmer in world ,Lady Ada Lovelace) to most modern and sophisticated languages like C#,Java and etc. , there have been a lot of changes but the pattern of writing instructions are same .Once I asked my teacher what are the fundamentals of writing a program, and his reply was that if you are able to use and understand control flows, conditional flows and data flow , you have learnt to write good programs and now after 4 years I realized how true he was , the programming revolves around these phenomena.
1. Data Flow:
            Computers perform computations on some sort of information, or simply on data.
So data is fuel for computers, and writing a program needs ample information about which sort of data you are going to use. For simplification we divide data in two categories mainly, one text and other numeric (data have more complex forms like images, audio/video etc). Both types have further classifications too. The most important thing is that when u are writing a code you must be aware of your data type (what sort of data you are using), scope of data and its flow.
Some languages are loosely bonded like PhP in which you are not required to mention the type of data when declaring them. In some languages like C you have to declare all variables (data) before writing the other instructions.
The scope of data refers to its availability in program. If you had declared a variable in a block than you won’t be able to use it outside that block. Same as some variables are declared with static or global keywords giving them access to be used in complete code.
You should also be aware of the flow of data , means if u are using   the data in loops and conditions etc. you should be able to know the points where the data is being updated especially if you are using pointer type , it helps you to debug the code properly and remove both logical and syntactical errors.



2. Control Flows:
                        As the computer instructions are read one at time (the instruction is executed at which Instruction pointer is pointing in memory), the flow of control is most imperative part in writing programs. By default computer reads instructions chronologically, but we can alter this flow by using the structures provided by computer languages.
Goto:
            The "goto" provides us the ability to skip the instructions and jump to the instruction which has been "label". The instructions within the goto statement and label are skipped and the control is shifted to the instruction after the label. The use of ‘goto’ is not considered as good practice in programming. It’s difficult to debug the code and it creates ‘spaghetti code ‘, analogy drawn from the curls of spaghetti.

Jumps:
            If you  r coding in machine languages than u can’t reject the importance of jumps. Jumps are same as goto or other way round (as machine languages are older). Jump as name suggests it transfers the control to the instruction where the label is pointing. Jump can also be used with conditions like jump when zero etc.

Functions:
            Functions are the art of writing programs. They add beauty in your code. Function is a block of instruction which can be run whenever needed by just calling its reference. The functions have ability to take some variables within and perform the operation on that and return the results. Its increases the readability of the code and gives the programmer to reuse its code whenever its needed. The function redirects the control to the code of the function and returns the control to the next instruction written after the reference of function.
3. Conditional Flow:
            Conditional structures are same as control flows, they decides which instruction to be executed but depending on some condition.
If-Else
            The classical example is “if else”, which is the most important and indigenous conditional structure in almost every language. The basic idea behind if else structure is that “if a condition verifies than run the instructions blocked in if or otherwise run the instructions blocked in else”.
But some time the code become complex and “if else “have to be used multiple time so to carter that we have another conditional structure named as switch.
Switch:
            Switch is very handy when you have to cope with multiple conditions. The basic structure of switch is that a variable is passed to switch and than it compares it with the ‘cases’ defined under the switch block, if any condition  mention in case verifies than the instructions under that case are executed. If none of ‘case’ is verify than the instructions under ‘default’ block are executed. One thing is important when using switch is that every case is ended with ‘break’ otherwise the cases after the verified case will be executed too.

Repetition:
Human beings get tedious after repeating some task, how interesting it may seem but computers are master in repeating the thing zillion times. They never get bored of repeating the same task. The repetition or loops are based on simple philosophy, repeat some thing for the time till condition is met. I won’t get into the loop variant, sentinel value and the techniques how to write good loops. There are two major loop structures and all other are their derivates. The first is ‘for’ in which we loop for some condition and until it’s met.
The other is ‘while’ which is same as ‘for’ structre but it’s the coder’s sense and good programming skill which tells which structure to use. But almost every ‘while’ loop can be written as ‘for’ and vice versa. The other structures like ‘do while’, ‘for each’ are derivates of basic for and while loops.
But as every skill is master through practice and there is a classical saying that, ‘it works perfectly in practice but it wont work in theory’. So the best thing to enhance your programming ability is to practice writing codes. That’s how you will learn to code programs which executes in lesser time and uses sophisticated data structures, and even complicated logics will be mapped in simple code.

#Though entry was made in 2011 but the reflections are from Fall 2006.

No comments:

Post a Comment