Vol. 1, No. 29
Getting Started With Programming
As we plunge deeper into the complex realm of Web building, the
tasks become more and more like programming. HTML jockeys may call
themselves "programmers" or "coders," but on the basic level of
marking up text, positioning images, and making forms and frames,
there's not a lot of genuine programming going on. Mouseover effects
and the like are a bit more involved, but programming for real doesn't
start until it comes to JavaScript and Perl code that actually
does things based on conditions that the code evaluates, using
variables, conditionals, loops, and all the rest of that marvelous
business.
To start, let's go over a few of the fundamental concepts of
programming, ideas that apply to every programming language. Be
patient. If you get the basic ideas in place first, you'll develop
good habits (one hopes) and a broad understanding that can be applied
to any project, be it the Perl scripts you write for your site's CGI
functionality, or obscure, assembly language drivers for obsolete
modems.
Unlike, say, writing an e-mail newsletter (about Web-site building),
the process of programming should always begin with a clear idea of
where you want to end up. Just like you don't just sit down at your
favorite editor and start typing HTML, hoping that something will
shape up, the more planning you do in advance, the better your
code will work out. For example, you know that you want some Perl
code that will sort form input based on its content. Start by
plotting out in abstract terms just how the code is going to work.
Divide up the tasks into blocks. For a program like this, there are
at least four discrete steps: accept the input, test the input to
see how it should be sorted, evaluate the results, and file the
output appropriately.
Once you've got the procedure sketched out, you can get down to work
on the various phases. It's a lot easier once you know where you're
headed.
Variables
Variables are one of the fundamental elements of almost all programming.
It's safe to say that without variables there'd be no computers. Variables
are basically little storage units with numbers or strings of characters
assigned to them, that are then manipulated in dozens of clever ways. So
when you say something like "cans = 6," you are setting the variable "cans"
equal to 6. By doing this, you can write a program that tracks soda usage
using that variable: Each time one is drunk, the program executes the
instruction "cans = cans - 1." That is, the number of cans is reduced by one.
It's a good idea, as in the above example, to give your variables
nice, clear, self-explanatory names, so that when you or someone
else looks over the code at a later date, you know what is going
on. It's no fun to read over your own code a year later and try
to remember what the difference was between variables that you
named "strghty" and "strgthy."
Conditionals and Functions
Some of the most common techniques you'll find yourself using in
your programs are conditional statements: doing one thing if a
certain condition is true, and another if it's not. In many
programming languages this is done with an "if" statement. It
can be something like "if cans = 0 then refill," where "refill"
is a function that restocks your fridge with soda.
Functions are isolated, self-contained little bits of code, each
one performing a small predefined task when directed to do so.
Using functions has the potential to make a programmer's life much
easier. Instead of writing the same code a hundred times to do
the same simple thing in different places in a program, you can
just write it once as a function and then call it whenever you
need it. This is called "code reuse" and is smiled upon by the
gods of programming.
Typically when you invoke a function, you give it "arguments."
No, not in the sense of picking a fight these arguments are
parameters that change each time the function is used. It's not
clear why they're called arguments, as they have nothing to do
with arguing. Each function is set up to expect a certain type
of argument, and that argument is usually what the function works
on. So for example, if you have a function that reverses text and
the function is called "reverse," the line of code that invokes
the function to reverse the text "Medusa" would look like this:
"reverse('Medusa')" and the output of the function would be
"asudeM."
These are a few of the fundamentals common to all programming.
From here, you should pick a nice language to start learning and
experimenting with. JavaScript, Java, and Perl are easy enough
and useful for Web-site development, so you might want to start
with one of them. Happy coding!
HINTS, POINTERS, AND TIPS 'O THE TRADE:
Like HTML, most programming languages allow you to interpolate
comments in your code that have no effect on how the program runs.
It is very helpful, for you or anyone else who may be looking at
your code, to put in a lot of comments clearly explaining what
each section of the program does and how. The more complex the
program, the more comments you should include.
Indent for clarity. If you have a function or loop, indent it one
tab stop. If there's another loop embedded within the first,
indent that another tab stop. That way it's easy to see exactly
what happens and when.
Don't expect your programs to work right away, even a little bit.
There is a lot of debugging, testing, and reworking involved in
even the simplest of projects. Coders must be patient.
In the hot weather, if you only have one fan, it's more effective
in the long run to use it to push the hot air out than to blow
cool air in.
RESOURCES:
Webmonkey's Intro to Programming
Webmonkey's JavaScript Tutorial
Webmonkey's Intro to Perl for CGI
A rundown of different programming languages