## 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.