Thursday, August 9, 2012

Brains, computers, and problem solutions

Programmers tell the computer what to do.

The computer has a processor, it has some memory, and it follows a set of instructions. The instructions say what to load into memory, what to do with the values in memory, how to change that memory, and when to store it off somewhere else. There is an instruction pointer that shows the next instruction to execute. There are tests that can change where the instruction pointer goes next.
(approximation of a Harvard architecture)
Assembly language is the closest to pure computer programming, to telling the computer exactly what to do. Imperative languages like C are an abstraction above that. We still tell the computer what to do, in what order, and how to change the values in memory. Code is in one place, data in another. Object oriented languages like Java offer a few more abstractions, organizing relevant code with data, hiding memory management. These languages are still imperative: we tell the processor what to do and in what order.

Now that we don't program business software in assembler, do programmers still tell the computer what to do? We tell the compiler what to do, and that tells the runtime what to do, and that tells the computer what to do. What do we really do?

Programmers solve problems.

When we learn to code we learn to think like the computer. But what if that is not the optimal way to think? what if we think instead of how the problem looks, what the solution looks like?
(it's abstract, OK? It's supposed to represent a data flow.)
Programming in a declarative style means stating solutions, not steps. State what is true, or state what we're doing - don't describe how the computer goes about it. That's an implementation detail for the compiler to optimize. (I'm talking business software here, not embedded systems.) Code in a language suited to the problem. This gives us more ways to solve problems, and cleaner ways to solve problems.

We're not limited to what comes naturally to a computer, or what comes naturally to our brains.

Abstraction is key. The right abstractions make our code easy to read and faster to write and maintain. Don't limit abstractions to the ones that fit the computer. Don't limit them to objects that model business entities. This is why learning math is useful, and it's why functional programming is useful: the more abstractions we know how to hold in our head -- abstractions with no analog in either the real world or the computer world -- the more tools we have for solving problems.

1 comment: