Monday, December 1, 2014

Three Advice to Newbie Programmers

It has been almost 4 years since I've started programming. In this period I've learned how to read code and how to write (more or less) readable code; worked on various small projects, and now regularly managing the codebase of one of these projects for 1.5 years. So now I can claim that I am not a complete noob anymore, probably a 'Junior Programmer', or a Level 1 programmer according to Programmer Competency Matrix who is slowly making his transition to level 2.

Being a student, sometimes I get help requests from classmates and juniors - especially in the last weeks of the semester when the deadline of the assignments and the term projects are approaching. It gives me a way to analyze what problems other starters are facing. Generally I help someone generously when he asks for guidance (I want to develop a chatting system for my Java term project. Where to start?) or he can express clearly what problem he is facing and what he has done so far to solve the problem. Unfortunately, most of the requests are "my code doesn't work, help me!" kind. I get really irritated when someone throws me some messy, semi-indented code which contains all the a-z alphabets as variable names and then tells me he doesn't know what error he is getting. Most of the cases, the only thing they tried to 'fix' the code is to do Voodoo Programming - changing this and that randomly and hoping that something magical would make the code work.

So if you are a newbie programmer, and you regularly spend hours in Voodoo programming, and then ask someone to fix your mess (or ask a question in Stackoverflow and get downvoted), here are three advice you might find very useful:

1. Learn how to identify the source of the error

In my opinion, finding the source of an error in a faulty portion of code is much more difficult and important than solving the error itself. Often it takes only a minute to solve a problem when the source is known. Finding the source is much more tricky. So, how do you find the source of the error? Debug it!

I often get astonished observing the lack of debugging knowledge in newbie programmers. Probably it is the fault of the learning system. Programming courses or books spares thousand words to teach you how to code - but often they won't teach you anything about how to debug. This is unfortunate, given the fact that you would spend a large portion of your time in debugging while developing something. Learning how to use a debugger is probably the single most important thing about programming that you can learn in 10 minutes. Of course, learning how to effectively use a debugger takes time and practice, but the basic operations of (e.g. setting a breakpoint and stepping through your code) shouldn't take more than 10 minutes. Search Google, pick a tutorial on how to use a debugger on your favorite IDE, learn it and use it regularly.
Of course, if you use the debugger for every single error, it will cost you a lot of time. Generally I try 'log based debugging' aka 'printf debugging' before starting my debugger. It is always a good idea to log important events and expected results in your code to catch the error early. Frameworks like Log4j makes it much easier to deal with a lot of logs.

Besides, never forget the importance of statically analyzing your code before running it to catch common errors. Unit tests also make finding errors quick and easy, but probably it would be a bit advanced for a beginner (I myself haven't used unit tests in any non-trivial projects so far, but I expect to do it in near future).

2. Learn how to solve the error

Once you have identified the source of the error, the solution would be obvious in most cases. But what if you face some NeverSeenThisKindOfException? Just Google it! It is surprising how many newbie programmers doesn't know that Googling using the compiler error/exception message will lead them to dozens of results from Stackoverflow and other forums. If it is not the error message, but some seemingly strange behavior of code that is bothering you, then try to formulate your search query using a few keywords. As a newbie programmer, it will be highly unlikely that you will face a completely new problem. So if you don't get any relevant search result, maybe you are searching using the wrong query. Try a new query which expresses your problem in a simpler way.

3. Learn how to ask for help

Certainly there will be cases when wouldn't be able to solve the problem on your own. Then you have to seek help from other people, either in person or through a forum. This Stackoverflow article covers almost everything about asking a good question, I'll emphasize on a few among those. Never ask someone to debug your code for yourself. Ask specific questions, like "I'm trying to do X in this part of the code, I'm expecting Y but this is giving Z output/giving this error". Briefly mention what you've done so far to solve the problem. This will make people more friendly towards you.

Another very important thing is to write readable code. Nobody expects modular and manageable code from a newbie programmer, but they would certainly expect you to write codes that won't hurt their eyes. You don't need to spend a day learning how to write readable code, just follow the style of the code from your favorite book/blog. Examine the how indentation and spacing are used. Always give variables meaningful names and follow the naming convention of the language. It may take some time to come up with good variable names, which will slow you down a little. But it will pay off when other people will read the code, or you would come to that code one month later and trying to figure out what the heck you've written earlier. Try to group logical pieces into functions. You will fall in love with your code soon.