Programming for the Non-Programmer Examples of programming concepts and terms for the rest of us Laurie Weaver
Programming for the Non-Programmer Programming and programmers can seem very mysterious to those outside of the profession. To take a peek behind this virtual curtain, we ll break down some common programming terms and practices into real-life examples At the highest level, programming is about telling a computer to perform a set of tasks and providing the computer with very precise written instructions on how to accomplish them. This entire set of instructions is the program. Programs can be written in different languages, such as Java or C++, and each language has different rules and syntax, just as English and German have different rules and syntax. Even so, all computer programming languages have some concepts in common, just as all human languages have some concepts in common. Here are a few of the concepts we ll be exploring: 1. Objects 2. Initialization 3. Code (Scripting) 4. Variables 5. Literals 6. Value 7. Syntax 8. Properties 9. Methods 10. Function Getting a computer to perform a task is somewhat like getting one s teenager to correctly set the table for company. It s not enough to just leave a note saying Set the table, please. You must be very specific. Which Objects are required? Objects are things in programming, just as they are in life. In programming, objects have two important attributes: state and behavior. State describes how they are, and behavior describes what they can do. More on these later. For table setting, the objects required might be: Dishes: State = clean, color; Behavior = break, lay Napkins: State = folded, design; Behavior = blot, lay Silverware: State = clean, metal; Behavior = cut, stab, scoop, lay Table: State = size, position; Behavior = support Chairs: State = size, position; Behavior = support Next up, to get started we need to tell the computer/teenager exactly how the state of the objects should first be when the table setting program begins. This is called initialization. Laurie Weaver 2008 all rights reserved LaurieWeaver.com 2
Here might be some of our written instructions (code) for initial table setting. Get four clean, blue dishes and place each one six inches from each corresponding chair. Smash! Inadvertent plate-break behavior! We didn t tell the program to place them on the table object six inches from the chair. Computers are quite literal. They, unlike most teenagers, will do exactly what you tell them to do. It s up to the programmer to be precise in order to avoid any and all misunderstandings. Even though computer behavior is literal, sometimes as a programmer, you use a special container in your code to stand in for a literal piece of information. These special containers are called variables. Computers know the difference between literals and variables because, in the code, you write literal information within quotation marks. Let s do an exercise to explore the difference between these two. Please write your name on something. What did you write? I would have written Laurie. Unless you are also named Laurie, you would have written a different word. In this case, name acts as the special container called variable. Remember, variables are placeholders for varying values. Each of us has a different value for our name variable. Now, please write down Name. If you wrote the letters N a m e, then you behaved as a computer would have, and literally wrote down what I asked instead of writing your name value. Notice that in the previous exercise, having quotations or not around the word name greatly changed the resulting behavior. This is an example of syntax. We have syntax in English as well. Consider the following: His dad called George. (Two guys here, Dad and George) His dad, called George (One guy here, George) Here s an example of programming syntax that is very important. Often in programming logic, we want something to happen only if both sides of a statement are equal. To check for that, the programmer uses two equal signs (==) in a row. Laurie Weaver 2008 all rights reserved LaurieWeaver.com 3
Let s examine a piece of pseudo-code to explore the vital difference in programming syntax between two equal signs (==) and one (=). If I wish the computer to display a welcome message only to those of us named Laurie, then my code might look something like this. If (name == Laurie ) then say Hi, Laurie. The first part of this code, because it contains two equal signs (==), is checking if the value of the variable name is equal to the exact literal Laurie. If so, then the code is telling the computer to display a message saying exactly Hi Laurie, because that is the literal phrase within the quotation marks. If I make a syntax mistake and only use one equal sign, I would be setting the value of name to Laurie instead of checking whether both sides of the statement are equal. Here s another example. Let s take the variable paycheck. What if we want to know as soon as our pay reaches the level of $2,500 so we can afford a vacation. We write a piece of logic to check the value of paycheck. If (paycheck == $2,500 ) then say Time to plan a vacation. If our paycheck was $1,400, we would not get the Time to plan a vacation message. The variable paycheck would NOT be equal to $2,500. But, if we made the mistake of using only one equal sign (=) in our code, we would have just said paycheck IS now $2,500 even if it was really only $1,400. Oops! To recap the importance of syntax, one equal sign (=) will set a variable to the value following it. Two equal signs (==) will check to see if both sides of the statement containing them are equal. Last but not least, we shall explore the two very important concepts mentioned previously having to do with objects: state and behavior. In programming, we can either find out about the current state of an object, or change the state of an object by getting or setting the object s properties. We tell objects to do something by calling on a set of their behaviors called methods. In the table setting example, I defined the object dishes as having states of clean and color. This means these are the properties of dishes. I can set the properties: This dish.clean = true; (I have a clean dish) Laurie Weaver 2008 all rights reserved LaurieWeaver.com 4
This dish.color = blue (I have a blue dish) I can also get the state of these properties. If (dish.clean = = false) say You need to wash the dishes! (dish.clean == false is the same as not clean) If (dish.color == yellow ) say Please use the blue dishes instead. (we are checking if the yellow dishes were used). Dish objects also were defined to have two possible behaviors, break and lay. These would be sets of special instructions for the dishes object called its methods. We write methods by defining functionality (function). function break (){ If (this dish.position == floor) then shatter } Basically, this pseudo-code is saying, if I tell the dish to break, check to see if it s on the floor. If it is, then shatter. We would call this function (tell the dish what to do) by using the syntax dish.break(). Notice the parentheses following the word break. These were also used when we defined the break function. This tells the program that it is to perform an action. Obviously, there are many, many more details to learn about programming, and some of the specifics will vary depending on the programming language you write code in. But, the main concepts we ve touched on are what programmers use every day. Most are concepts you also use every day, such as objects, syntax and properties. So, the next time a programmer is talking about programming, listen in, and you might be surprised at how much you can now understand. Laurie Weaver 2008 all rights reserved LaurieWeaver.com 5