General Programming

What is Programming?

I compare programming to writing a recipe. Both involve some potentially complex problems and both often require clear detailed instructions on how to achieve the desired result. Whether you are attempting to create eggs benedict or build a webpage, there are useful similarities. The analogy isn't perfect, but it is adequate to explain things at a simple level.

With a recipe

There are ingredients, some more complex than others

A recipe typically contains a list of ingredients needed to complete it. This list usually includes both the type of ingredient as well as quantity. Some examples include:

salt, butter, english muffins

There are instructions on how to combine them

What follows the ingredient list is a detailed set of instructions on how to combine these ingredients, and the proper procedure to follow in order to achieve the desired result. For example:

butter the english muffin

They have abbreviations to shorten the instructions

Recipes use abbreviations to identify commonly used terms without having to spell them out every time. This not only aids in writing recipes, but by standardizing on a common set, there less ambiguity. A few examples:

1 Tbsp, 3 C, 1/4 pt

They assume a certain level of prior knowledge of certain tasks

Recipies often assume you have basic cooking skills and don't attempt to explain common or simple tasks. Doing this simplifies the instructions but provides for a clear explanation. Using forks to split an english muffin need not be explained in every recipie. So the instructions can say:

separate the english muffin

When the complete instructions are too complex, they refer to other instructions to simplify or reuse common techniques

Specifying the complete process to make hollendaise sauce would make most recipies that include it to be overly long. Since the process doesn't change from recipie to recipie, the instructions can refer to the end result. Doing this provides for a more detailed means to explain the sauce without duplicating it in multiple places. Additionally, by putting the instructions for the sauce in a common place, we also eliminate the potential errors or inconsistencies that could occur if it appeared in several places. Thus, the recipie for Eggs Benedict can simply say:

make a hollendaise sauce (see page xx)

When all of this is done correctly, most people following the directions can produce the desired result. When the instructions are unclear, ambiguous, or confusing, the result is, at best, inconsistent.


Programming is much the same

There are ingredients, some more complex than others

Programs use data as ingredients. Some come from the user, some from the program itself, and some might come from a database. They have different types and different rules for manipulating them based on their type. Some common examples of such elements are:

numbers, text, arrays

There are instructions on how to combine them

At a simple level this is what the real code part of your program is. Say you have an array of names and you want to sort the array. In PHP you could simply say:

sort($array);

They have abbreviations to shorten the instructions

Programs all full of symbols that mean things and are specific to the language being used. These symbols shorten the code in a consistent and meaningful way. To add 1 to a number in PHP the syntax is:

$i++;

They assume a certain level of prior knowledge of certain tasks

Programming languages generally provide a library of functions/methods that a program can use that were built for you. Such libraries make the task of programming much simpler. Rather than writing code to take a calculation and round the result to an integer every time you need that, a library routine is provided. PHP contains a function called round which does just that. Thus, in PHP we can accomplish this by:

$a = round($b);

When the complete instructions are too complex, they refer to other instructions to simplify or reuse common techniques

Most languages not only provide a library of predefined routines to accomplish detailed tasks for you, but they also provide a means to define your own. This allows you to create functions not in the library so you can reuse them over and over without needing to recode the process. In PHP the basic syntax is:

function yourNameHere ($parameter1, ...) { /* code would appear here */ }

General Rules of Programming

I've spent many years writing (and rewriting) code and have learned a number of basic principles that I've used with success. I include them here so that when we delve deeper into the mechanics of programming, we are building on a solid conceptual basis for why we do things MY way.

Be clear about what you are trying to do.

Focus on the real objective. When you do that, many potential questions disappear since they are no longer important.

Write so that changes are easy.

Programs that never change are boring and likely not terribly useful. Since you will change it, make your life easier at the beginning. Build in the ability to change at the start.

Surprises are fine for birthdays, not for users.

Unless you are writing a game where surprises are expected, never perform an action in response to a user that is unexpected. This causes the user to lose trust in your code. When you do what the user expects, they feel they are in control. Imagine pressing the accelerator on a car and sometimes having it roll down the window as well.

Simple is better.

It constantly amazes me how complex people can make the simplest tasks. Setting the clock on a VCR comes to mind. Even the layout of a keyboard was originally designed to be hard in order to slow people down. Due to this fact alone, no standard layout keyboard should be called ergonomic. In spite of this truth, and the fact that keyboards are no longer mechanically limited, I use the same 'QWERTY' arrangement as the majority of folks.

Explain WHY, not HOW.

When you explain in comments how the code does some task, you are simply reiterating what the code says. I can read the code itself and figure out what it does. What is far more interesting however is WHY the code exists in the first place. That is often not clear at all just from reading the code.

Don’t try to do too much with one thing.

Ever seen one of those combination tools that supposedly works as a hammer, screwdriver, pliers, wrench, and so on? If you've ever used one you learn quickly that although it can be used for all those tasks, it seldom does any of them well. Software is much the same. Do one thing at a time, and do it well. This not only makes it clearer what you are doing, but if you need to change it, you've already made it easier.