When starting a new path, it is often difficult to know which way to go. Some good advice I once received is that when people are starting something new, they need to be told exactly what and how to do it. It is only once they have a bit of experience that you can remove the “how” from the equation and just say what needs to be done.
For new programmers just getting started, here are the steps I recommend taking until you understand your working style better and can make adjustments.
1) Understand the problem
What you are trying to accomplish should be clear in your mind. You should know what will happen once the task is complete and what the user will see. If you are confused about anything, as a developer, it is your responsibility to ask for more clarification until your are clear about it. There is nothing about writing code at this stage. Often times, developers just want to jump right into coding but understanding the problem is paramount.
2) Plan your approach on paper – pseudo code
On a piece of paper, away from the computer, write out all the steps necessary you think it will take to accomplish the task. Don’t write any programming code. This should be pseudo code and written in plain English. Write one action per line. The more detailed, the better.
If you get stuck on part of it, then you do not understand the problem well enough. Go back to step 1 and get it clear what you are trying to do. Notice, there is still nothing about writing code at this stage.
3) Understand existing code
Once the pseudo code is written out and your plan makes sense, now you can find the files and the areas that you think you will need to modify in existing code. You should read the surrounding code and understand it first before making any changes. If you do not understand it fully, there is a greater chance that any changes you make will introduce more bugs. Notice, there is still nothing about writing code at this stage. So far, it has been all understanding and planning.
4) Write code, one line at a time
Ok, now you can start writing code because a) it is clear what you want to accomplish and b) you understand the existing code well.
NEVER, EVER copy code from somewhere else. If you do find code somewhere else that (once you fully understand it) you think applies to your task, then type it out line by line. You may think this will be slower but making sure you understand what you are doing ALWAYS saves you time down the road. When typing it line by line, you should be reading it and understanding it as you go along. If you are copying and pasting code, alarm bells should start going off in your head.
When writing code, only write out enough code for one of your pseudo-code lines. Do not write out everything. Start at the first line written on the paper and then convert that into actual code. Then go on to the next step.
NOTE: this stage should actually take the least amount of time in the process. Yes, it’s a little ironic that as a developer you’ll be spending the least amount of time writing actual code but you should be spending the majority of your time understanding the problem, planning it out, reading and understanding other code and testing your work. If you are spending the most of your time here then there is something you do not understand well enough. If it is the problem, then go back to step 1. If it is the other code, then you need to read and understand more. If it is the programming syntax then you need to read and understand more about the applicable programming language.
5) Test what you have done
For each line of your pseudo-code that you have written in actual code, test it to make sure it is doing what you expect. Do this immediately after writing the code! You do not want to write a whole bunch of code and then find out that it is not working somewhere. You should be testing after each step so you can fix it at that point while it is fresh in your mind instead of finding it hours or days later and you have to think again about what you were doing. If you test it and it works as you expect, go back to step 4 and write the code for the next line of your pseudo code.
6) Don’t give up
You are probably closer to the solution than you realise. Have you heard the story about the gold miner? During the gold rush he bought a mine and after weeks of exploration, found gold. Experts said that he had found one of the richest mines in the state. Returning with more equipment he spent time mining but no more gold could be found. Frustrated, he finally gave up and sold the mine. The new owner brought in an expert to assess the mine and he discovered that the main vein was just a few feet away from where they had stopped mining.
Sometimes it can be frustrating when you are programming and going in circles. Instill good habits at the start of your programming career and they will treat you well down the road. Be as orderly about your programming style as your code and you will find a successful career as a developer. I wish you the best.