-
- When we write a program,
we are figuring out a way
-
to turn the brilliant ideas
in our head into actual code.
-
Here I want to talk about a technique
-
that many programmers use to do that
-
and it's what we call "pseudocode".
-
Now pseudocode is probably a
word you've never heard before
-
but basically, it's code
that looks a lot like English
-
or really, whatever language
you like to talk in.
-
Well, okay, that may not make sense
-
so let's talk through an actual example.
-
So let's say that I want to
draw a nice, symmetrical face.
-
So I might start by looking
at myself in the mirror
-
and maybe sketching it out
-
and seeing, okay well
I have this oval face,
-
I have two eyes and
they're about at this level
-
and this is where the
center of the face is
-
and now I have an idea for what
I want my face to look like.
-
So, I'll start writing it in pseudocode.
-
So let's see, the first
thing we want to do is
-
draw the face which is
an oval in the center.
-
Then we'd want to draw the two eyes,
-
which are two ovals, about
two thirds up the face
-
and maybe one fifth the size of the face.
-
Not exact math there, just
looking at my own face.
-
And then we draw the mouth which is a line
-
going halfway across the face
-
and maybe one third of the way up.
-
So, notice how I write
my pseudocode as comments
-
by starting each line
with the two slashes here.
-
That way I can write my
pseudocode in the program itself
-
and not have to worry about
getting any syntax errors
-
because the program will ignore comments.
-
Now that I've written this in pseudocode,
-
I can spend the time to turn each
-
of these lines of pseudocode
into actual bits of code.
-
Alright, so let's see.
-
For the face, an oval in the center.
-
Well that I'll use the
ellipse function for
-
and I'll figure out the
center of the screen here
-
and figure out a nice size.
-
Okay, that looks good.
-
For the eyes, once again
those are ellipses.
-
Everything on my face is
an ellipse, I'm very round.
-
It's going to be, let's
see, we'll do some math here
-
to get the eyes at a nice place
-
and make them about fit the size.
-
Okay, that looks good for the first eye,
-
I'll just copy and paste
to make the next eye.
-
Great.
-
Now, I can even leave my pseudocode
-
for a friend to implement
and they probably could
-
because I've given this
really nice description.
-
So I'm going to do that here
because we're friends, right?
-
So you might think this is silly,
-
why are we going through the effort
-
to write our program twice,
first in human language
-
and then in program language?
-
Well, this example was pretty simple
-
but pretty soon you'll be
building more complex programs
-
and it may be hard for you
to keep the whole program
-
in your head before coding it,
-
so what I usually do is
write the general idea
-
in pseudocode first and
then I'll spend more time
-
on the details of each part of that idea,
-
converting each line of
pseudocode into real code.
-
I think you'll find once
you start using pseudocode,
-
you'll do it more and more.
-
Try it in your next program and see.