Computational science approach to programming

Sometimes, I wondered why people write code in a certain way, usually inefficient, lengthy or just plain weird. There’s a classic

int i;
char c[3];
i = 0;
c[i++] = 'a';
c[i++] = 'b';
c[i++] = 'c';

when

char c[3];
c[0] = 'a';
c[1] = 'b';
c[2] = 'c';

works just as well, and more readable. Perhaps they thought, “Hey there’s an array! Must iterate something!”. They didn’t give any thought to why the array was used in the first place.

And so I had a theory. What if they were so engrossed in the intricacies of the programming language, that they’ve lost sight of the problem they were trying to solve?

Which brings me to computational science. I gave a short explanation at the start of another article, but it didn’t do justice to it. You might want to check out the differences between computational science and computer science as described by Wikipedia.

I majored in computational science and applied mathematics. What happened was that I’d be given a problem (usually scientific or math based), and I’d have to solve it. Calculations by hand on paper were done. After the theory of the solution was understood, and practical use (with physically calculations) were done, it’s time to scale up by coding the solution method.

For example, Gaussian elimination on matrices. It’s used to solve a system of linear equations, where the coefficients in the equations were used to form the matrices. Coefficients are just the number in front of the (math) variable. So in the equation
2x + 5y = 7
the coefficient of x is 2.

The theory was taught and calculations were done by hand on small matrices. I worked on 2 by 3, 3 by 4 and even 4 by 5 matrices. One small slip in entering figures into the calculator, and I’d have to start all over again.

Then I had to implement the method in code. To do that, I needed to understand the theory and the solution behind the method. I did calculations by hand, so I knew the pain. And then I wrote the code. The problem given was to solve a 100 by 101 matrix. It’d probably take a whole night to do by hand. Assuming I don’t punch the wrong numbers on the calculator or read the wrong figures. The computer probably took only a few seconds.

And I believe that is what’s really behind computational science. My studies taught me to solve problems first. If the solution benefit from the calculation speed of computers and ease of repetitiveness with computers (which is usually the case), then it’s go ahead and write the code to implement the solution.

A programmer could agonise over the implementation of some feature, without thinking if that feature could be solved in another way, an easier-to-implement way.

For example, the programmer could be frustrated by the tedium of retrieving values from the database, adding them up with intermediate variables in code, and then storing them back into the database. It’s very painful to do that in C (I know, I’ve seen it). Did it ever occur to the programmer that he could just do the summation and store the sum entirely within the database environment?

All he needed was to write code to execute a SQL statement or stored procedure. Which is much simpler than keeping track of temporary variables, worrying about floating point errors and hold database records in memory for looping.

And that’s how I write code, the computational science approach to programming. Think about the problem, solve the actual problem, then use the computer’s capabilities to scale the solution.

  1. Christopher Tay

    I agree with you that a fatal mistake often made is to plunge straight in to coding without stepping back to reflect upon the proper algorithms or data structures to use. Having proper algos and data structures from the macro point of view have a much higher impact than the tiny optimizations to be made at the micro level. As to the character array example, I think most existing mature compilers should be able to reasonably optimize the code. But of course, the degree and forms of optimization varies amongst the different compilers.

    If you’re more interested in coding to solve problems instead of the technical details associated with writing a correctly executing program, you might want to take a look at functional languages. I guess you’re doing lots of .NET? There’s the programming language F# which runs on .NET and is similar to ML.

  2. Aaron Falloon

    Jeepers, we must think very alike because I posted a similar article a few days ago. I think it’s always helpful to define the problem and write that definition down. That way, you’re going to solve the actual problem.

  3. Vincent Tan

    Christopher – I’m sure the character array example can be optimised by compilers. The thing is, I’m not that good. I assume the programmer had a good reason for writing it in that particular way. After I figured what’s going on, and discover that there wasn’t really any good reason, I get a little frustrated because of the lost time.

    I’ve heard of F#. Just haven’t gone to explore it much. And yes, I’m a .NET programmer, dealing mostly with ASP.NET, though I do Windows and console applications too. That array example was paraphrased from a C program on Unix.

    Aaron – Hey! I just read that article on your blog. Yeah it’s freaky about the coincidence…

  4. Ben Barden

    I might use the c[i++] method for a bigger array, as adding items in the middle could get annoying as I’d have to keep changing the indices… Then again, I could sort the array… or do something more like this: array(‘a’, ‘b’, ‘c’). 🙂

  5. Vincent Tan

    For bigger arrays, by all means go ahead with iterating variables. It’d be a more effective method than changing numerical indices.

    I just feel the example and it’s associated context (smallish array) works fine with hard-coded numerical indices.

Comments are closed.