# Computer programming



## Kopachris (May 31, 2010)

General computer programming discussion here. Also, a tutorial written by yours truly.

*How to computer programming*
Okay, grab your favorite IDE and type the following:

```
#include <iostream>

using namespace std;

int main() {
    cout<<"Hello world!";

    return 0;
}
```
Now hit "Run."

Oh, too much? Sorry. I guess we should go over some of the theory before getting down to business.

*What is computer programming?*
Okay, you probably already know that computers deal with something called binary, which is a bunch of 1s and 0s, but you might not understand exactly what that means. We'll skip the part about electrons and transistors--all you really need to know is that everything in a computer is represented with numbers. It doesn't matter whether you represent the numbers as binary, decimal, octal, or hexadecimal (or duodecimal--each one has their own use). Every command, every letter, every piece of data is a number. Everything you see on the screen is created by manipulating these numbers in different ways. Computer programming is the act of that manipulation.

*Abstraction*
The computer only understands one language: numbers. However, because you can store numbers and have the computer parse them later (i.e. store and run a program), you can use those numbers to manipulate other numbers in a format _you_ can understand (i.e. ASCII-encoded text). The first level of abstraction higher than pure numbers (called "machine code") is called assembly language. Assembly language assigns each command-number (each instruction) a mnemonic. For example, (and I'm just making this up), the instruction to add two numbers might be instruction number 15 for the computer, but will use the mnemonic "add". So you type "add 2 2" and the computer will understand it as "15 2 2". Other programming languages provide further abstraction, with simple, easy-to-remember commands representing entire groups of machine instructions. Because we're having the computer build its instructions for us, we can create languages that let us speak to the computer in different styles or paradigms, which brings us to...

*Classifying programming languages*
In order to pick the right tool for the job, it's good to know some of their basic features. We've already talked about abstraction level, but related to that is how the language is read. There are two main camps, here: compiled and interpreted. With a compiled language, the code is read by a program called a compiler, translated into machine code (and often linked with various libraries, which we'll get to later), and put into a file that can be run on its own anytime. These programs are generally faster than interpreted ones, with the disadvantage (or advantage, depending on point of view) that the final result is pretty much unreadable by human eyes. Additionally, compiled programming languages usually require the programmer to write more boilerplate code--code to set up the environment the rest of the code will run in. Because of the boilerplate code, compiled languages are often harder to read and write, as well.

Interpreted languages, on the other hand, are read and executed by an interpreter. The interpreter provides an environment for the code to run in, and all the code must refer to pieces of code provided by the interpreter. However, these aren't the only two options. Java, for example, is compiled into what's called bytecode, which is a portable form of almost machine code. The bytecode is then run by an interpreter. There are also interpreters for certain languages which include a "just-in-time" compiler. Rather than executing code in the interpreter's environment, a JIT compiler creates an environment for the code. This allows the machine code to be optimized in ways it couldn't be using the interpreter's environment, therefore allowing the code to run faster than purely interpreted code.

In addition to the compiled/interpreted distinction, you'll want to know what sort of writing styles, or programming paradigms, are supported by what languages. Deep down, all machine code is procedural--each instruction is executed one after the other. Higher levels of abstraction let us artificially create constructs that let us reuse code, therefore making it easier to read.

As I said, procedural languages execute code one bit at a time, one after another. Even if a language supports or prefers another paradigm, it will almost always allow procedural programming as well, since this style is most "natural" for computers. An example of a high-level language (or language family, rather) that's almost purely procedural is BASIC.

The other two programming paradigms you'll probably be interested in are functional programming and object-oriented programming. With functional programming, programs are defined in terms of functions such as those found in lambda calculus. Each function returns a value to its parent function, which then manipulates the value with other functions. Look up LISP for an example of a purely functional language. Object-oriented programming, on the other hand defines programs in terms of objects (usually called "classes"). Each object has a number of attributes, all of which are also objects, some of which are actually functions (in which case they're called "methods"). Object-oriented programs then rely on using methods to manipulate various attributes. Java and C# are both almost purely object-oriented, though C++ and Python also encourage object-oriented programming.

One last programming paradigm worth mentioning is declarative programming. Declarative programming languages allow you to define what should be done without defining how it should be done. Most query languages, such as SQL and regex fall into this category, and any markup language meant for formatting text, such as HTML, LaTeX, or even BBCode may arguably count as well.

As this post is already getting quite long, tune in next week (or whenever the mods approve this thread) for the next part of the tutorial, which will include some actual programming!


----------



## Taggart (Feb 14, 2013)

I don't _quite _see the relevance of this. However, as somebody who started out in CPM using 8080 assembler, it looks interesting.


----------



## Kopachris (May 31, 2010)

Taggart said:


> I don't _quite _see the relevance of this. However, as somebody who started out in CPM using 8080 assembler, it looks interesting.


Teaching people how to write in a programming language without teaching them how the computer works is how we get computer programmers who don't know how to program. It's like trying to teach a natural language without any cultural immersion, or teaching to read music without any understanding of meter. The most important part of all that should be the understanding that all a computer does is manipulate numbers, and computer programming is telling it how to manipulate those numbers. If the rest looks boring, it can probably just be skimmed through.


----------



## Vaneyes (May 11, 2010)

Re computer programming, it's 'Just say No' for me. My brain still has the occasional seizure from learning COBOL in the '60's.


----------



## Crudblud (Dec 29, 2011)

Shaping up to be a very nice thread, KC. I haven't done programming in many a year, but this is pretty interesting and well informed. Keep it up!


----------



## ahammel (Oct 10, 2012)

Good overview. However, I think it is missing a correction of the most common misconception about computer programs, and what must be the leading cause of bad code: the idea that a computer program is primarily a set of instructions for a machine to execute. It's not. A good program is first a message to another human being, and only incidentally something that a computer can run.


----------



## Taggart (Feb 14, 2013)

Kopachris said:


> Teaching people how to write in a programming language without teaching them how the computer works is how we get computer programmers who don't know how to program. It's like trying to teach a natural language without any cultural immersion, or teaching to read music without any understanding of meter. The most important part of all that should be the understanding that all a computer does is manipulate numbers, and computer programming is telling it how to manipulate those numbers. If the rest looks boring, it can probably just be skimmed through.


Don't forget that a computer program is also just numbers, one of the old tricks in 8080 code was to write self modifying code to minimise memory usage. Made debugging a total nightmare but great fun.

Thing is a functional and\or declarative language like SQL or prolog means that you can get right away from the hardware and concentrate on things like embodying business logic without having to disappear into massive nested if or case structures. Those of us who remember the original pentium bug know that understanding computers still doesn't guarantee that number crunching will give the "right" answer.

I agree with @crudblud - looks like a nice thread.


----------



## ProudSquire (Nov 30, 2011)

This seems like a very good idea. I'll be following along.


----------



## Huilunsoittaja (Apr 6, 2010)

I learned some Java in high school, wasn't too bad at it. The Copy-Paste ability of computers is basically the greatest invention after computers itself. Saves soooo much time and effort.


----------



## Couchie (Dec 9, 2010)

I do some VBA work in Excel, does that count?


----------



## ahammel (Oct 10, 2012)

Couchie said:


> I do some VBA work in Excel, does that count?


Absolutely!

Programming is like plumbing: anybody can use a plugger/VBA in excel; it takes a bit more study to install a sink/write a shell script; with lot of experience you can plumb a house/design a non-trivial system; and you need a team of engineers to design a sewer/operating system.


----------



## Couchie (Dec 9, 2010)

And you need project managers to ***** in the toilet until the whole thing is a clogged up, ******* mess.


----------



## opus55 (Nov 9, 2010)

The last thing I'd want to see on TC after spending hours programming/debugging at work


----------

