Thursday, August 20, 2009

Some advice to computer programming

Computer is more and more important nowadays. Even if you're not doing work directly related to computers, it's still useful to have the tool ready. It speeds up a lot of things. Before you dig into programming, these are a few general things that I wish I had known when I started learning how to program, and I want to share with you.

The real challenge (and fun) of programming is to think about how to distribute resources between different parts, and how to "speak" an algorithm in a comprehensive way to both computers and humans.

Efficient programming is an interplay between development cost, execution cost (how long the program runs) and maintainance cost. It's not good for any of them to be too time-consuming, especially maintainance, which is usually overlooked in classes that teach you how to program. For new programmers, they will probably be struggling with making the code work. But once you go past this barrier, usually what you will find is a lot of little tricks that are either cool-looking or those that make execution cost a bit lower, while leaving the code hard to read. This is one big trap for inexperienced programmers. Unless you are writing programs that have a tight time budget or memory budget, it's usually a good idea to exchange a little bit of execution efficiency for readability and easiness for maintainace. Take C++ for example, there is this code fragment

int x = 10; // x and y are two integers
int y = 35;
x ^= y ^= x ^= y; // "^" is xor operator

The last line is nothing but swapping the content of the two variables. If somebody hasn't seen this trick, it will be hard to understand what is going on. And imagine several hundred lines of this kind of stuff. After a few months even the programmer himself will have a hard time understanding the code.

Adopt a coding style and stick to it. This includes the format/spacing of the code, choice of variable names, remember to release memory, etc. Avoid incomprehensive variable names. Make the variable names speak for itself. The size of the source code might differ a bit, but it's already 21st century. A few kb is nothing, and it doesn't make any difference once the code compiles. But it makes your code a lot easier to read. Sticking to a coding style can also decrease your debugging time.

Use comments to document what you wrote. It might take some time to write the comments, but it will ultimately save a lot of time in code maintainance. One good strategy is, before actual coding, write down the flow of algorithm in comments, for example a list of things to do in the order of execution. Then fill the code in between comments. It's usually more efficient this way as you don't have to keep all the details of the algorithm in your memory.

Spend most of the time thinking about what to do rather than going into coding directly. Write a short summary of programming plan if needed. Most of the time, the inefficiency of coding comes from not knowing exactly what to do. It's also more error-prone if you're not completely sure what you are doing. For example, you can start by thinking about what the big parts in the algorithm are. How should one present the idea in terms of code? Then you can try to think how to break the process down to simple pieces. There are usually some parts that are used again and again, and it's good to have them as a separated function. A pretty nice way I found is to write a function for each major part of the code. Then in the main function it might look like the following. (Don't care too much about the syntax if you don't know C++, but just get a feel of how one could approach programming.)

int main()   // program starts here
{
   ReadData(....);   // Calls a function that reads in data

   Initialize(....);   // Calls a function that does initialization
   while(CheckIfDone(....) == false)   // if not done....
      DoOneIteration(....);   // do one more iteration

   PrintResults(....);

   CleanUp(....);   // clean-up the memory
   return 0;   // end the program
}

This way you won't get a "spaghetti" code which has everything in it in a long stream of codes. It's pretty self-explanatory too. And note the empty lines between codes of different functionality. It's an example of the coding style that makes things easier to maintain. Of course you still need to implement the functions, but it's much better to focus on one thing at a time.

Learn some basic algorithms, and familiarize yourself with some basic graph theory. And think a lot. There is a good website (http://train.usaco.org/usacogate) that helps you develop basic algorithmic knowledge. It will be some solid hands-on experience. The reality is cruel. A good algorithm can run a lot faster than mediocre algorithms. It could easily be thousands times or more. The more you know, the more efficient you can command the computers to work.

Be familiar with the tools that are available. This includes the standard libraries that comes with the compiler for example. It's not only a problem with rewriting codes that are already there, the point is that the tool you use determines how you will think about a problem.

I hope this helps, even if you don't get everything rightaway. I won't say that these are the exact guidelines, but they are certainly things for you to think about. There is some chance that you will work on a larger-scale project, and these kind of things will start to really pay off. It's good to have the habit of better-quality coding. And before you know it, you'll like programming instead of cursing all the time why your code is not working.

--FHead, 2009 Aug. 20 at Geneva, Switzerland


Tuesday, August 18, 2009

Rather depressed lately....

Data-taking phase of the experiment has ended, and now it's all analysis. Which means that I have to sit in front of the computer the whole time. It's fine by itself, but over time it can be annoying if one continues to do so continually.

The progress....is not so good in my opinion. A lot of work has been put in coordination in software (nothing to do with physics in this part), and it took way longer than it really should. Now finally the real analysis starts rolling, and I got some preliminary result. Only preliminary. And it's been more than 3 weeks.

A bigger problem is probably that I don't know how much work is okay. I keep advancing, but I don't know whether my progress is considered, by other, slacking off or not. And since most of the work are groundwork to setup for analysis, I don't really have anything big yet. It makes me nervous.

Another thing is that we don't have a clear analysis plan. I was told that the plan was forming, but then, now, after three weeks, all I got is some suggestions of the things that might be interesting to look at. I feel like I'm walking in the dark. I can't really see where to go, yet I try to find something useful. And lately I've been feeling burned down, so exhausted that after work the only thing I want to do is get some rest and do literally nothing.

So, the things I wanted to do over the summer in my free time have been stalled for a while. I can't just waste time here, but really, I feel like I'm kidnapped and don't really have a choice. And I got an email just now, "FHead can give a short update on very interesting analysis". Sigh. I want a break. And please let me accumulate more stuff before making me give a presentation.

On top of all that, the relatively simple environment gives me a good amount of time facing myself. There is a big portion of solitude in everyday life. Working, eating, sleeping, and repeat. The busy work doesn't make it better. I hope it did, but instead it just makes me feel more lonely. Just like the old days when I just started undergraduate study. There are things that you think are gone already, but in these moments when you are all by yourself, they will come back and haunt you.

Maybe I should just take a couple of days off and go travel somewhere. There are tons of places I wanted to visit. Really, I can't just waste time burning myself with work and work alone.