Learning to program - how to jump the first hurdles
Archive - Originally posted on "The Horse's Mouth" - 2009-08-20 07:45:03 - Graham EllisFrom last Sunday ... here are links to the examples of code that I wrote in front of the "Learning to Program in Python" delegates - to show them not only how a program works, but also how I (and other code authors) go from a requirement through to the code that actually does the job. Or, paraphrasing, "a manual is all very well but it doesn't show the process of how the code is developed - you just see the final product, which is of use (but only limited use) if you need to develop code yourself".
Some Early Stuff
Showing operator precedence - how multiplication and division happen before addition and subtraction, but you can alter that using round brackets. [source code]
A first use of conditionals - how you can look at something in your program, and then perform certain tasks based on what you find. This is "if" and "else" (and - depending on the language - elif elsif or elseif). [source code]
And then how you push pieces of code that can be shared between a whole lot of programs out into a separate file that each of those programs can call in. Although you don't think about this naturally when you write your very first programs, you will realise very quickly in your 2nd, 3rd and 4th programs that use the same data that you don't want to recode ... and you don't want to copy and paste either, since you'll end up having to maintain / update multiple copies if you do so. See [source code - calling program] and [source code - shared logic].
The importance of structured or OO programming
I can't stress this enough, even from day 1. You need to write code that will be re-usable and maintainable - a Rembrant and not the dog's dinner.
[here] is a tiny piece of code ... or so it appears at first. In fact, it's a tiny main program that pulls in other bits of code that MIGHT be short or MIGHT be substantial ... but in itself, a very short piece of code to do a job.
That same piece of code is expanded [here] into an example that only runs some of the structured blocks in certain conditions, and is expanded further [here] into code that loops, repeating the operation of one of the structured blocks of code time and time again.
For reference (at this stage), the blocks of code that have the logic hidden within the ("encapsulated") may be found [here].
I have posted further source showing the importance of good variable naming here and of commenting your code here
Looking ahead and motivating the class
Let's face it - you can't learn to program in a day. And a programming language is made up of a large number of different components and concepts which when used together make it powerful. So at the end of the first day's teaching, delegates are seeing something but not the whole picture, and need a beacon to shine ahead and say "this is where we'll be heading by the end of the course in a couple of days".
[here] is an example I wrote at the tail end of Sunday - it's a file parser that went through a log file that's about 30 Mbytes in size and produced me a simple report telling me how many accesses there were in total, and how many to a particular web page. It runs like this:
Dorothy-2:py grahamellis$ python file_first
Whole file was 149307 lines long
Intersting stuff was 11 lines
Dorothy-2:py grahamellis$
You'll spot that a very short piece of code - 21 lines - written at the end of that first day is sufficient to give me a useful analysis of a substantial data flow which wouldn't otherwise have been straightforward. As soon as you have program loops involved, the power of your program increases by an order of magnitude.
It increases by a further order of magnitude when you add in collections - variables that can hold a whole series of values based on a key of some sort; using those, I have been able to expand that previous example to count access to all past blog articles in that log file, rather than just one specific article - so it's a really useful analysis. The sample program, written in front of delegates at the end of the first day to shine a beacon ahead, is [here], and when you run it, it produces a long report ending as follows:
1502 136 Java-sorting-ArrayList-example-generics
255 157 STDIN-STDOUT-STDERR-and-DATA-Perl-file-handles
969 241 Perl-and-
158 937 MySQL-LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN
Dorothy-2:py grahamellis$
which tells me that the four most popular blog articles (by number of times read) that day were no.s 158, 969, 255 and 1502, that they were read 937, 241, 157 and 136 times ... and that they were about (well - you can see the report for yourselves!).
All of which ain't a bad outcome for new programmers on a Sunday!