This document provides information on how to write computer programs. We present suggestions you should follow while developing a computer program. The recommendations apply to any programming language.
Whether you use pseudocode, UML, and/or any other design tool, what matter is that you need to first think about what you need to implement before you start implementing it. Often a person can spent a significant amount of time writing code that does not solve the assigned problem. It is important to first understand the problem you need to solve, otherwise there you may waste a lot of time and effort. For example, if you need to implement a particular algorithm (e.g., a graph algorithm that computes shortest path), make sure you understand the algorithm and have a couple of examples that you have solved yourself before starting to implement any code. These examples will help you in the process of developing your design and verifying that your code is working as expected.
This is the most important rule of computer programming! The number one mistake you can make is to try to do a lot at once. Incremental code development means adding a little bit of code, checking that the program is generating the expected results, and then moving forward. It is that simple. If you follow this approach you will have an easier time at locating where problems are in your code. For example, if your program is working, and you add two lines of code and the program stops working, you can bet those two lines are causing the problem. Incremental code development is a simple idea that unfortunately escapes people (I guess there is a human gene that gets in the way :)).
Keep in mind that as your programming skills develop, what we consider a little bit will vary. For example, beginner programmers can define a little bit as just adding three or four lines of code, but seasoned programmers could write a larger number. When in doubt less is better.
Two common types of assumptions:
Testing is a process that can be tedious, but it is extremely important. You should test your system periodically and as often as possible, rather than waiting to the end once coding is completed (or almost complete). Writing tests may prevent you from moving faster, but in the long run it will save you time because:
Whenever possible, simplify the data sets you use to test your code. For example, if you need to implement a sorted list, verify the correctness of the list with the most simple data type (e.g., numbers) you can think of. If the problem originally deals with a list of sorted activities, during testing use a list of integers. It is easier to see what could be wrong by using a simpler data type. Of course eventually you want to use complex data sets.
You need to organize your tests in a way you can convince yourself (and others) you are covering all the possible cases. Before you start writing tests analyze the possible set of cases you can have.
Unlike writing a paper or other tasks you may be familiar with, computer programming can be very unpredictable in terms of time requirements. The time it can take to finish a computer program can be estimated, but you have to plan and allocate time to deal with unexpected circumstances. If you wait until the last minute to start on a programming task, you will not have the opportunity to ask for help or to clarify any doubts.
Keep in mind that computer programming requires significant mental effort; time is needed to rest and being able to focus. Take breaks; don't keep working on code hour after hour. A 10-minute break can help a lot.
As you write code it will help you significantly if you stick with a set of conventions and style (style that you may have defined yourself). For example, rules for naming variables/methods/functions if consistently used can help in the process of writing code. Remembering which variable you need to use is easier if you follow a naming convention.
There are two approaches you can use while writing code:
Preventive programming is the preferred choice. It could feel you are not moving as fast as you wish, but in the long run you will not waste time and effort. Small problems easy to identify in isolation may become extremely hard problems to identify later on once the code is part of a larger system.
Often you may add a semicolon, use a method/function, etc., and get the expected results without actually understanding why. You need to stop and ask why. Do not move forward without understanding why your program is working. This also applies to scenarios where you are receiving assistance from a colleague, a teaching assistant, etc. Ask them to explain to you any changes they have suggested that makes your program works.
Nowadays languages offer rich libraries that provide a lot of support. If you are using a library to solve a problem and you are not sure how to use the library, experiment with it in isolation before you use it to solve your problem.
Many beginner programmers think that writing code is about writing the code and then running to someone to find the problems with the code; this is not correct. Writing code means writing the code and finding (debugging) the problems with the code too. After all there is no such thing as the "company debugging person". There are several reasons for doing your own debugging:
A debugger is a great tool, but make sure you understand how to use it before using it. If you are planning to use a debugger, spend some time practicing the debugger with a piece of code that has no errors. This will allow you to practice features of the debugger with code you are familiar with. Your focus will be to understand the debugger. Keep in mind that if you are careful in the code development process the number of times you will need the debugger is minimal. Also, keep in mind that sometimes a simple output statement could help you in the process of finding problems in your code. If you are planning to be a professional programmer you need to know how to use a debugger.
A computer program is a complex piece of engineering. Organization helps keep this complexity under control. Good variable names, good indentation, appropriate comments all are part of keeping your code well organized.
People forget about the code they write. If you look at the code you wrote a couple of months ago, you will probably not recognize a large percentage of the code. As you write code, keep in mind you will forget a lot of details and decisions you made. Use appropriate comments and specify any assumptions you have made; it will help others and it will help you.
You need to have some sort of backup system. Something as simple as copies of your files or a more sophisticated system (e.g., git). Create backups often and if possible in different computers. Backups allow you to have a copy of your work if you computer fails and they allow you to go back to a previous version of your system.
Starting a programming assignment can be a difficult task. Make sure you understand the problem you need to solve and have a design/plan. Don't be afraid to start even if you don't have every single detailed thought out; you will figure out things as you move along in the development process.
Programming takes time and effort, but it is very rewarding experience. With the passing of time you will become faster at code development. Just be patient.