How to program – Some essential tips

If one follows the arrow of time from the early 1980s to the present day, the number of programming problems have not only proliferated but have also become more difficult. Fortunately  programming in itself has  become more manageable with massive increases in computing horsepower, smarter tools and instant availability of information on the internet, typically with the click of a mouse.

Learning to program is no easy task, but can be done with the right mix of attitude, curiosity and interest. Becoming adept at programming, however, is something else. An interesting essay in this context is Peter Norvig’s ‘Teach yourself programming in 10 years’

Back in the 1980s when I wrote my first Fortran program on my college Mainframe, programming was a lengthy exercise, spanning several days.

1

My first program was to plot a sine wave of characters on a computer printout. Running this program required the following several steps

  1. Enter the program on a teletype terminal and create a stack of Hollerith (punched) cards
  2. Submit the stack of cards to the computer center
  3. The computer center would do a batch execute in the evening on the Mainframe
  4. God forbid, if your program has a syntax error. If you did find an error, go back to step 1, the next day.
  5. Assuming everything is fine, the computer center would run your program and your output (printout) would be placed in the appropriate pigeon hole which you would need to pick up the next day.

The whole exercise to write a small-sized program could take anywhere between a couple of days to a whole week.

In the early 1990s things got a little better where one could code, compile, link and execute sitting at one’s desk. However while the programming itself got much simpler than before, certain tasks were still difficult.  Till the late 90s programs of any sort had to be written using a regular text editor (vi , emacs etc.)  You would then have to go through the process of compiling, linking and executing.

An angry compiler would typically spew forth venom at missing semi-colons, undeclared variables, and uninitialized values. This would happen till you are able to iron out all syntax errors.  Then you would link, get undefined symbols and have to include appropriate libraries etc. And then finally you would execute your code, only to have it crash. The process of debugging would then start.

Luckily technology has made life a whole lot easier except for the last step where you could still  run into an execution errors . In these days an IDE (Interactive Development Environment) like Eclipse will flag syntax errors, missing definitions/declarations etc. as you write your code. Moreover Eclipse can also indicate which libraries (imports) you would need to include in your package for it to build. The only missing step in IDEs of these days is the ability to predict possible execution errors in your program.  I wouldn’t be surprised, if in future, like Microsoft Word,  the IDE is able to tell you if a programming construct does not make sense.

So things have gotten a lot easier for the programmer. The following tips for are particularly useful as you progress along in programming

  1. These days when you are learning a new programming language it is not necessary to know the language from cover to cover by reading a book. In those days when we learnt C it was necessary to know everything from bit structures, macros, pragma etc. The reason being that every syntax or execution error one had to rush to get the textbook and thumb through it for the answer. Not so, in these days of Google. You have the world’s library at your fingertips.
  2. To get started it is necessary to learn just the most important programming constructs of the language say structure, class, car, cdr besides the usual suspects like loops, conditions and case constructs
  3. Download and install an IDE for the language. In most case Eclipse will work
  4. Try to write a simple program and test out your code.
  5. To do any sort of programming these days you will necessarily need to make 3 friends
    1. Google
    2. Stackoverflow
    3. Git & GitHub
  6. Honing your Googling skills is very important. There are answers to almost any sort of programming problems out there. You would be surprised to know that there are many others who did exactly the same stupid mistake that you did out there. Also googling will take you to interesting tutorials, blogs, articles that discuss different aspects of the programming language and the problem you are trying to solve
  7. Stackoverflow is really a God send to all programmers. There are so many questions on so many aspects of every programming language on earth there. If you spend time searching Stackoverflow you are bound to find answers, code snippets that you can readily use in your code
  8. Post your questions in stackoverflow when you don’t find the answers there. You are bound to get quick answers. Thanks to the gamification of Stackoverflow (points, upvotes,badges  etc) that has been created on Stackoverflow.
  9. Git & GitHub: I would suggest that you download and install GitHub for Windows. This will provide you with version control on your desktop. You can modify code while being to switch back to an earlier version with Git. Read up a good tutorial on Git for Windows
  10. Once you have working code you push it onto GitHub and share with other programmers

Now that you have the basic setup here are few other extremely important tips

  1. The most important criteria for programming is ‘attitude’. Initially you are bound to get frustrated, angry, irritated etc. But it is necessary to look at the errors that you get with the right attitude. Know that an error is telling you something. Usually the answers to your mistake are in the ‘error message’ itself. Look at it closely and try to understand it. You will learn a lot more when you learn from errors than from copy-pasting from somebody else’s code, even if works right the first time around!
  2. Make sure you do something different each time. As Einstein said “ If you keep doing the same thing, you will keep getting the same result’
  3. There are different ways to debug your code. You could use the debugger and single step through the code and keep checking the values of the variables. I personally prefer print statements to localize where things are going wrong. I then try to narrow down the problem to a few lines of code and try to take it apart.

Hopefully the above tips are useful. Programming can be creative activity and will be indispensable in our future.

Above all have fun coding, there are so many possibilities these days!

Also see

1. Programming languages in layman’s language
2. The common alphabet of programming languages
3. The mind of the programmer
4. Programming Zen and now – Some essential tips -2 

You may also like
1. A crime map of India in R: Crimes against women
2.  What’s up Watson? Using IBM Watson’s QAAPI with Bluemix, NodeExpress – Part 1
3.  Bend it like Bluemix, MongoDB with autoscaling – Part 2
4. Informed choices through Machine Learning : Analyzing Kohli, Tendulkar and Dravid
5. Thinking Web Scale (TWS-3): Map-Reduce – Bring compute to data
6. Deblurring with OpenCV:Weiner filter reloaded

Programming Zen and now – Some essential tips-2

The Science of Innovation

Mankind’s progress is measured by the depth of creativity and the number of innovations in every period. The human race has an irresistible urge to make things better, faster, cheaper. While modern technological marvels like the airplane, the laptop or an LCD TV continue to amaze us, people in these industries will most probably concur that most of the developments that have happened in these domains have been in small increments over long periods of time. Barring a few breakthrough discoveries like calculus by Isaac Newton or the Theory of General Relativity by Albert Einstein most of the innovations that have happened have been incremental and are the result of careful analysis, sudden insight after several days of thoughtful deliberation, or sound judgment.

Creativity need not be sensational nor even path breaking. Creative ideas can just be incremental. Not all ideas happen in the spur of the moment or are serendipitous.

Innovation and creativity can be deliberate and well thought-out. There is neither a silver bullet for innovation nor a magic potion to inspire creativity. However there are certain eternal principles of innovation that keep repeating time and time again.  Almost all innovations are based on the constant need for simplicity, reliability, being disaster proof, extendibility or safety. “Invention is the child of Necessity” and innovations happen when there is strong need for it. This article explores some of the eternal principles behind the various innovations of the ages.  It looks at the motivation and the thought processes behind those inventions. Various common day-to-day gadgets, processes or devices are explored to determine the principles behind the particular improvement or advancement.

Some of the principles behind innovations are looked at below in greater detail

1. Simplicity

Several innovations are based on the principle of simplicity. Smaller, simpler, faster has been the driver of several innovations. .For e.g. RISC (Reduced Instruction Set Computer) design strategy is based on the insight that simplified instructions can provide higher performance. The philosophy behind this is that simplicity enables much faster execution of each instruction as opposed to CISC (Complex Instruction Set Computer). In favor of simplicity, are regular English alphabet text based protocols like HTTP, SIP as opposed to the more esoteric telecommunication or data communication protocols which send and receive messages in binary 1’s & 0’s and have complicated construction rules. The tradeoff for simplicity, however, is the consequent increase in bandwidth or the increase in the communication pipe capacity. To counter the increase in bandwidth there are several innovative techniques to compress the text message. These compression algorithms compress the text messages before sending them on the communication pipe. These compressed messages, on reception are uncompressed to obtain the original message.

2.Safety

Some incremental changes result from the need to increase safety. It is based on the principle of disaster-proofing the devices or to provide safety to us human beings and protect us from freak accidents. For e.g. the safety pin is an incremental improvement over the regular pin. The electrical fuse and the safety valve in a pressure cooker are two such innovative improvements that are based on the principle of safety. The electrical fuse is an interesting innovation and is based on the principle that when there is an abnormal surge of current in the fuse wire, the resulting heat produced will melt the wire, thus breaking the electrical connection. This protects the gadget and the more expensive internal circuits from serious damage. Also the safety valve in the pressure cooker will give way if the pressure inside the cooker goes beyond safety limits. This protects the cooker from bursting due to excess pressure and causing damage to humans and property.

3.Judgment:

Some innovations are based on the principle of sound engineering judgment and to large extent common sense. One such innovation is the LRU technique of an Operating System like Windows or UNIX. This technique or algorithm helps the computer in deciding which specific “page” or section of a large program in the computer’s Random Access Memory (RAM) should be moved out or swapped to the disk (only a limited number of pages can be in the RAM at a time). The computer makes this decision based on the “Least recently Used” (LRU) technique. Obviously, amongst many pages in the computer’s RAM, the page that has been used the least, in the recent past, can be moved out to the disk under the assumption that it less likely to be used in the future.

Another wonderful innovation, based on sound engineering judgment, is Huffman’s method of coding.  This technique is used in computer communication for the most efficient method of encoding the English text. It is based on a simple rule that more frequently occurring letters in the text should be assigned a smaller number of binary digits  (0 or 1) or bits and less frequently occurring letters be assigned a larger number of binary digits. Since the letter ‘e’ occurs most frequently it is assigned the least number of binary digits and the letter ‘z’ number of binary digits. The resulting binary string thus obtained is most efficient and optimal. This technique clearly shows the ingenuity in the algorithm.

4.Paradigm Shift

The move from vacuum-tube based electronics for e.g. the diode, triode to the more compact, smaller & less power consuming semi-conductor devices was a major paradigm shift in the realm of electronics. So also the move from cassette tapes to compact discs (CDs) and from VHS video tapes to the now compact DVDs were game changing innovations. Similarly the move from film-based cameras to the now digital cameras is a huge paradigm shift. Innovations which are based on a paradigm different from an existing philosophy truly require an out-of-box or a lateral thinking coupled with great perceptiveness and knowledge of the field.

5.Logical induction/deduction

In the mid 19th century many great and powerful inventions/ discoveries were made by giants like Faraday, Lenz, Maxwell, and Fleming in the field of Electro Magnetism. One the finding was that, there was a production of a voltage, and hence a current, across a conductor situated in a changing magnetic field. This consequently led to the invention of the Electro-mechanical Generator for generating electricity. Conversely, it was found that if a current carrying conductor is located in an external magnetic field perpendicular to the conductor, the conductor experiences a force perpendicular to itself and to the external magnetic field. This principle, which results in a force on the conductor, promptly led to the invention of the still prevalent Electro-mechanical Motor.

6.Cause-Effect –Cause

There are many inventions which are based on the cause-effect-cause principle. In this category of innovations the cause & effect are interchanged in different situations to handle different problems. For e.g. thermal energy may be converted to electrical energy as in the case of a steam turbine. Or conversely electrical energy may be converted to heat for e.g. in the electric cooker. Similarly there are inventions, where light energy and electrical energy are transformed based on the specific need.   A recent investigation into the possibility of a remote power outlet led the author on an interesting journey. Since electrical energy can be converted to microwaves (microwave cooker) the author wondered whether the reverse is possible i.e. could microwaves be transmitted across space and at the receiving end be converted to electricity. An internet search showed that this is very much possible and has been thought of more than a decade back. This is known as Microwave Power Transmission (MPT). However practical applications of this are not possible because of the radiation hazards of microwaves. However, MPT is used in outer space.

7.Feedback principle

An excellent example of this is the room Air Conditioner (A/C). The room air-conditioner maintains a constant temperature. If there is an increase in room temperature it increases the cooling and if there is a drop in temperature it decreases the rate of cooling. There are many day to day inventions that are based on the principle of feedback where the result is fed back and manipulated internally such that resultant output is maintained at a constant level.

8.Insight & Ingenuity

Vacuum tubes and the semi-conductor transistor can be made to toggle between 2 distinct states (on & off). This property and the insight and knowledge of  binary arithmetic soon led to utilization of electronic devices for binary arithmetic. This soon led to the development of electronic circuits for simple addition, subtraction, and multiplication etc of binary numbers. Invention after invention and innovation after innovation led to development and metamorphosis of the now ubiquitous Personal Computer.

Conclusion

In conclusion innovations can be incremental. There are numerous examples of human cleverness and innovation all around us. One just needs to notice and pretty soon we begin to appreciate human ingenuity. We need to reflect on the thought processes behind each incremental innovation which have made our lives more pleasant and convenient. Hopefully, as we become more aware, someday, some of us may have a defining “eureka” moment.

Find me on Google+

Learning to innovate

Published in Associated Content

Mankind’s progress is measured by the depth of creativity and the number of innovations in every period. The human race has an irresistible urge to make things better, faster, cheaper. While modern technological marvels like the airplane, the laptop or an LCD TV continue to amaze us, people in these industries will most probably concur that most of the developments that have happened in these domains have been in small increments over long periods of time. Barring a few breakthrough discoveries like calculus by Isaac Newton or the Theory of General Relativity by Albert Einstein most of the innovations that have happened have been incremental and are the result of careful analysis, sudden insight after several days of thoughtful deliberation, or sound judgment.

Creativity need not be sensational nor even path breaking. Creative ideas can just be incremental. Not all ideas happen in the spur of the moment or are serendipitous.

Innovation and creativity can be deliberate and well thought-out. There is neither a silver bullet for innovation nor a magic potion to inspire creativity. However there are certain eternal principles of innovation that keep repeating time and time again.  Almost all innovations are based on the constant need for simplicity, reliability, being disaster proof, extendibility or safety. “Invention is the child of Necessity” and innovations happen when there is strong need for it. This article explores some of the eternal principles behind the various innovations of the ages.  It looks at the motivation and the thought processes behind those inventions. Various common day-to-day gadgets, processes or devices are explored to determine the principles behind the particular improvement or advancement.

Some of the principles behind innovations are looked at below in greater detail

  1. Simplicity

Several innovations are based on the principle of simplicity. Smaller, simpler, faster has been the driver of several innovations. .For e.g. RISC (Reduced Instruction Set Computer) design strategy is based on the insight that simplified instructions can provide higher performance. The philosophy behind this is that simplicity enables much faster execution of each instruction as opposed to CISC (Complex Instruction Set Computer). In favor of simplicity, are regular English alphabet text based protocols like HTTP, SIP as opposed to the more esoteric telecommunication or data communication protocols which send and receive messages in binary 1’s & 0’s and have complicated construction rules. The tradeoff for simplicity, however, is the consequent increase in bandwidth or the increase in the communication pipe capacity. To counter the increase in bandwidth there are several innovative techniques to compress the text message. These compression algorithms compress the text messages before sending them on the communication pipe. These compressed messages, on reception are uncompressed to obtain the original message.

  1. Safety

Some incremental changes result from the need to increase safety. It is based on the principle of disaster-proofing the devices or to provide safety to us human beings and protect us from freak accidents. For e.g. the safety pin is an incremental improvement over the regular pin. The electrical fuse and the safety valve in a pressure cooker are two such innovative improvements that are based on the principle of safety. The electrical fuse is an interesting innovation and is based on the principle that when there is an abnormal surge of current in the fuse wire, the resulting heat produced will melt the wire, thus breaking the electrical connection. This protects the gadget and the more expensive internal circuits from serious damage. Also the safety valve in the pressure cooker will give way if the pressure inside the cooker goes beyond safety limits. This protects the cooker from bursting due to excess pressure and causing damage to humans and property.

  1. Judgment:

Some innovations are based on the principle of sound engineering judgment and to large extent common sense. One such innovation is the LRU technique of an Operating System like Windows or UNIX. This technique or algorithm helps the computer in deciding which specific “page” or section of a large program in the computer’s Random Access Memory (RAM) should be moved out or swapped to the disk (only a limited number of pages can be in the RAM at a time). The computer makes this decision based on the “Least recently Used” (LRU) technique. Obviously, amongst many pages in the computer’s RAM, the page that has been used the least, in the recent past, can be moved out to the disk under the assumption that it less likely to be used in the future.

Another wonderful innovation, based on sound engineering judgment, is Huffman’s method of coding.  This technique is used in computer communication for the most efficient method of encoding the English text. It is based on a simple rule that more frequently occurring letters in the text should be assigned a smaller number of binary digits  (0 or 1) or bits and less frequently occurring letters be assigned a larger number of binary digits. Since the letter ‘e’ occurs most frequently it is assigned the least number of binary digits and the letter ‘z’ number of binary digits. The resulting binary string thus obtained is most efficient and optimal. This technique clearly shows the ingenuity in the algorithm.

  1. Paradigm Shift

The move from vacuum-tube based electronics for e.g. the diode, triode to the more compact, smaller & less power consuming semi-conductor devices was a major paradigm shift in the realm of electronics. So also the move from cassette tapes to compact discs (CDs) and from VHS video tapes to the now compact DVDs were game changing innovations. Similarly the move from film-based cameras to the now digital cameras is a huge paradigm shift. Innovations which are based on a paradigm different from an existing philosophy truly require an out-of-box or a lateral thinking coupled with great perceptiveness and knowledge of the field.

  1. Logical induction/deduction

In the mid 19th century many great and powerful inventions/ discoveries were made by giants like Faraday, Lenz, Maxwell, and Fleming in the field of Electro Magnetism. One the finding was that, there was a production of a voltage, and hence a current, across a conductor situated in a changing magnetic field. This consequently led to the invention of the Electro-mechanical Generator for generating electricity. Conversely, it was found that if a current carrying conductor is located in an external magnetic field perpendicular to the conductor, the conductor experiences a force perpendicular to itself and to the external magnetic field. This principle, which results in a force on the conductor, promptly led to the invention of the still prevalent Electro-mechanical Motor.

  1. Cause-Effect –Cause

There are many inventions which are based on the cause-effect-cause principle. In this category of innovations the cause & effect are interchanged in different situations to handle different problems. For e.g. thermal energy may be converted to electrical energy as in the case of a steam turbine. Or conversely electrical energy may be converted to heat for e.g. in the electric cooker. Similarly there are inventions, where light energy and electrical energy are transformed based on the specific need.   A recent investigation into the possibility of a remote power outlet led the author on an interesting journey. Since electrical energy can be converted to microwaves (microwave cooker) the author wondered whether the reverse is possible i.e. could microwaves be transmitted across space and at the receiving end be converted to electricity. An internet search showed that this is very much possible and has been thought of more than a decade back. This is known as Microwave Power Transmission (MPT). However practical applications of this are not possible because of the radiation hazards of microwaves. However, MPT is used in outer space.

  1. Feedback principle

An excellent example of this is the room Air Conditioner (A/C). The room air-conditioner maintains a constant temperature. If there is an increase in room temperature it increases the cooling and if there is a drop in temperature it decreases the rate of cooling. There are many day to day inventions that are based on the principle of feedback where the result is fed back and manipulated internally such that resultant output is maintained at a constant level.

  1. Insight & Ingenuity

Vacuum tubes and the semi-conductor transistor can be made to toggle between 2 distinct states (on & off). This property and the insight and knowledge of  binary arithmetic soon led to utilization of electronic devices for binary arithmetic. This soon led to the development of electronic circuits for simple addition, subtraction, and multiplication etc of binary numbers. Invention after invention and innovation after innovation led to development and metamorphosis of the now ubiquitous Personal Computer.

Conclusion

In conclusion innovations can be incremental. There are numerous examples of human cleverness and innovation all around us. One just needs to notice and pretty soon we begin to appreciate human ingenuity. We need to reflect on the thought processes behind each incremental innovation which have made our lives more pleasant and convenient. Hopefully, as we become more aware, someday, some of us may have a defining “eureka” moment.

Find me on Google+