Intro to Computer Programming

There are many introductory classes out there.  Often, they will pick a language or two and dive in.  This is a great way to learn.  You should definitely learn a language to start.

But here, I won’t get too involved into teaching one language, and won’t go into computer code just yet.  I’m applying the same approach here as with the other topics – avoiding the math and actually solving the problem, and just focusing on the higher level concepts first – like having a conversation about computer programming with your friend.

There are so many languages.  Let’s just write out a few – the most popular ones of 2015, according to IEEE (Java is most popular, followed by C):

  • Java
  • C
  • C++
  • Python
  • C# (C Sharp)
  • R
  • PHP
  • JavaScript
  • Ruby
  • Matlab

The one that’ll hopefully learn first in school is C.  C is a widely used general purpose programming language, developed at AT&T Bell Labs.  Many, many languages have borrowed from C in some way or another, including pretty much all of the programming languages on that list.

If you pick a language – let’s pick C – and go to wikipedia, you see in the first sentence that C is:

  • general purpose
  • imperative (procedural – a type of programming paradigm)
  • supporting structured programming
  • lexical variable scope and recursion
  • statically typed

What does any of that mean, and how does it compare to other languages?  In fact, why are there even so many languages?  Well, languages are written by people for slightly different purposes.  Ultimately, a computer language is a formally constructed language (or set of rules and procedures) that communicates instructions to a computer.  We can make computers do things, but we need to tell the computer what to do.  Since computers don’t understand english, we needed to teach them a language that they understood, and that we understand.  And so these computer languages are the bridge between us and them – it gives us the ability to communicate with the machine.  And they may seem dry compared to actual language, but a good language is easy to understand by another programmer.  It needs to be clear. So the language used to communicate the instructions needs to be formally constructed so that other people can learn it.

Different people can write different languages .  And over time, languages improve and new features are added to make communicating with the machine easier, or faster, or in a whole new different way.  Sometimes, outdated features are removed, or added, or improved.  This is pretty different than something like English.  English or Spanish doesn’t change much.  Trying to constantly change English and improve upon it would be too difficult to implement and not necessary – but with a programming language, people can make advances and easily spread this new information around.  Whether people decide to implement it or not is up to them.  But usually, newer languages that have been heavily improved are adopted and become widely used.  Some languages are simply better for certain problems, and others better for other problems.  Some are designed for performance.  Some are designed for generality, and some are designed for productivity.  It is generally thought that you can have two of these three, but no language exists that is super fast, super general, and super quick to write.

If you are still confused about why there are so many programming languages, look around you and identify almost anything.  Take your computer, for instance.  Some are designed to be quick, some are designed to be easy to use, some are designed to have great battery life, some are designed to be cheap.  The list goes on and on.  But fundamentally, they all do the same thing.  They all let you go on Facebook.  And this diversity is a good thing.  It allows a consumer to identify their specific needs, and then go out and purchase that computer.  Just like a programmer can think, what language will best suit my needs?  And then they can go and pick that language and use it.

So I want to cover some of the bigger differences between different languages.  And then I will dive into some of the details of Objective C.  My motivation for Objective C: it has been used to program at least 1 million iPad/iPhone applications, and that is pretty cool.  If you want to learn to make your own app, Objective C is really useful.  Keep in mind that Apple just released a new language called Swift. While Swift is advanced and modern, Objective C holds many advantages, and if you want to make a simple iPhone app (which I highly recommend, even if it is just something as simple as a calculator), you won’t be able to get around knowing at least a bit of Objective C. As well, Objective C is built to be layered on top of C.

Remember that list of things that described what C was in the very first sentence of that wikipedia entry?  Let’s go through those.

General Purpose

This is a pretty simple one to explain.  If a language is general-purpose, it means that it is easily applied in many applications – a language that is capable of creating all sorts of programs.  By contrast, a special-purpose language would be used for a very specific set of problems.  Like the language COGO, used for civil engineering problems.  Another term is Domain-specific language (DSL).  A domain is just a field of study – like designing web pages.  A few examples of domains could be creating software for simulating a race car.  Or for pretty much anything you could think of.  So if you have a domain specific language for calculating taxes, if would probably be difficult or impossible to take this language and use it to create some computer game.  With a general purpose language, you could potentially use the same language for both.  Of course, the line between GPL and DSL isn’t clearly defined.  A DSL could have some specific features for some domain, but may still be applicable outside of that domain.  Or it may evolve over time.  A good Example is the language Perl – which was originally designed as a text processing language, which is very specific.  It has since become widely used and so is now a general purpose language.

Keep in mind that although a programming language may be classified as general, it may only be used in specific applications.  You can see why the line between DSL and GPL is so blurry.

C is a general purpose language.  You could potentially use it almost anywhere, although DSL may be better for specific applications.

Programming Paradigm (Procedural/Imperative)

This is an important one, so let’s take some time on it.

Procedural is a known as a programming paradigm.  A paradigm, in science, is a set of concepts, patterns, rules, standards.  A programming paradigm is the fundamental style of the programming language.  It is the fundamental approach on how to program a computer.  And there are many programming paradigms.  Each different paradigm is composed of a set of concepts, intended to solve a specific problem.  Think of it as the overall basis of what a program does – how it is put together, what it aims to solve.

There a lot less paradigms than programming languages.  Several languages use the same paradigm, which means that they use the same overall structure and concepts to solve a certain task.  If you look at a few popular languages, like Java, Javascript, C#, Ruby and Python (all extremely popular languages), they use the same paradigms.  From this viewpoint, they are pretty much identical.  Which is great, because if you learn one, you’ll understand how the other ones work.  Which makes it easier to switch between languages – you just need to learn the specifics of each language, but you already know how they work.

That doesn’t really give you a good idea of what a paradigm actually is, however.  So let’s dive into a couple of different types of paradigms.

The procedural programming paradigm means that the computer program simple carries out a set of instructions, and runs from top to bottom.  Each step in the program is executed before moving onto the next step.  A procedure could be a routine, a subroutine, a method, or a function.  A procedural language will operate something like this: perform task X, and then use X to perform task Y, and then perform task Z.  And that is how the program will be written too.  It is like reading a book.  You read it from top to bottom – you don’t jump around reading different sentences all over the page, in different orders.  Procedural languages are easier to learn in this way, because you can always follow along at each step and know where the program will be.   You write step by step instructions for the computer, and in this way it is fairly easy to read code and figure out what the program does.  C is a procedural language.

The object-oriented programming (OOP) paradigm is quite a bit different.  This paradigm is based on the concept of objects.  Objects are just data structures, and they contain, well, data.  Like a box containing books.  And you can do things with these objects.  Using an example from everyday life – let’s say that your apartment or home is the object.  This object was created out of a class of objects called houses, and your particular house is an instance of that class.  Because your neighbour also has a house, and that is their instance of that class.  But the houses are slightly different.  And with your house, you can do things.  You can eat inside of it.  You can sleep.  You can clean it.  These actions you can perform are called methods.  Anything you do inside of your house is really an action.  And you can add methods.  let’s say you want to add the ability to take a shower.  You can add that method.  And you can use these methods on other objects too.  Another object could be a car.  You could do many of the same things in your car, although some may work better than other.  You could clean it.  You could sleep in it.  And so with these objects and methods, you can do a variety of things, and the objects can interact with each other.  So the whole paradigm is that the language is organized around objects, and how they interact, rather than just performing one action after another.  An objected-oriented program is really concerned with the data and how to manipulate it so that it does what we want.  There are a whole bunch of advantages of OOP.  Programs using the object-oriented paradigm includes an impressive list:

  • Python
  • C++
  • Objective-C
  • Smalltalk
  • Java
  • Swift
  • C#
  • Perl
  • Ruby
  • Python

This is part of the reason why later on I have chosen to introduce Objective C.  You’ll hopefully get a good idea of how object-oriented programming works, and then learning any of those other languages will not be difficult.  You’ll also be able to start working on your own iPhone app, if you chose to do so.

An object-oriented programming language typically shares some features with procedural programming.  The procedural paradigm contains some fundamental tools that are really need to construct programs.  These include tools such as:

  • Variables to store data like integers, or characters, strings
  • Procedures, like functions, and routines, to take data in, process it in some way, and then send it back out.  A procedural function could be to input the number 5, multiple it by 10, and then output 500.

We’re going to dive completely in OOP later on, so lets move on from this for now.

Now you may be getting a better idea of how paradigms work.  This isn’t to say that you couldn’t write two different programs, one in a procedural language, and another in an object oriented language, to do the exact same task (in certain cases).  But depending on the application, you really need certain paradigms in order for them to work.  Many iPhone applications, for instance, really wouldn’t be possible to write with procedural languages.  If the procedural program just runs from top to bottom, how would you handle different events that are possible, like a user pressing button A instead of button B?  It just wouldn’t work.

Another paradigm is machine code, or assembly language.  When you write a computer program, whether it be procedural or OOP, at a certain point the computer has to interpret this.  Machine code and assembly language are very low level languages which the computer is able to read and understand.  Higher level languages, like C and Java, the computer is not able to understand.  It must be converted to this low-level code, which is what a compiler does.  It takes the source language written that the programmer has chosen, and turns it into code that the computer can understand (assembly, machine code).  You can write a program in this code, but it is very tedious.  Anyway, this a paradigm that is intended for the computer, not the user – it is direct instructions as a sequence of numbers.

Just a bit of history here – the first programming languages were machine code – low level languages with direct instructions to the computer.  These are called first and second generation languages.  Assembly and machine code is still used in time-critical systems and embedded systems, because you have direct control over what the computer is interpreted.  A compiler, although it will make your code work, may not always convert it into the most efficient code possible (although compilers get better at this all the time).

The next advance was procedural programming, which is considered a higher level, third generation language – a language that is more friendly to the computer programmer, and quicker and easier to write programs with, making it more powerful.  It abstracts away many of the low level details that the computer needs, but that the programmer does not need to be concerned with.  After procedural, the OOP paradigm was created.

It may be hard to distinguish between procedural and imperative (the broader paradigm).  Indeed, they seem to be almost identical, and the terms are often used interchangeably.  Specifically, procedural programming occurs when the program relies on subroutines and functions – which are both procedures.  A subroutine/function is simply just a unit of code that performs some task.  You can take this chunk of code which performs a particular task and insert it into another program, where it will then perform the same task.  Say that you have a variable x, and you want to multiple it by some integer, 5.  You can write a function that does this – and you may need to perform this task several times – but you only need to write the code once.  Then throughout the program, you can call this function as needed.  The advantage of this with more complex, larger programs, is that you can decompose a difficult task into simple steps – essentially, taking a large problem and breaking it into simple pieces.  You can also reduce duplicate code within the program (like adding 5 multiple times throughout the program – instead you just need to call the function, and it will do that work for you), and of course you can reuse code across different programs.  You could also break up a programming task to multiple different programmers – each person could write a function, to be easily added to the code later, since it is its own separate little block of code.  The main disadvantage is one of speed – every time you call a function, there is the overhead involved with calling it.  You can visualize it kind of like having to travel to that function, and then having the function perform whatever task you need it to, and then travelling back to where you originally were.  If you just do the task yourself, you don’t speed any time travelling to that function.  Time-critical systems may instead choose not to use functions in order to speed up the execution time of the code.

So basically, procedural is imperative programming.  Imperative is the broad paradigm.  With procedural, you rely on procedures – known as functions/subroutines – to construct your code.  Of course, this affects how your code looks and works.

 

Leave a comment