Hour 2
Process and Techniques

The words program and programmer are mentioned throughout each of these 24 lessons, and you undoubtedly heard them before you picked up this book. Before going back into specifics of Python and other programming languages, this hour explains what a program really is. A firm grasp of this hour’s material is a prerequisite for moving on to the more advanced programming concepts introduced throughout this book.

As you read this hour’s lesson, keep in mind that programming is rewarding—not only financially but also mentally and emotionally. Programmers often feel the same sense of creative rush that artists and skilled craftspeople feel while honing their projects. Writing a program, however, can be tedious. It is often a detail-oriented task, and ample frustration comes with the territory. The computer’s quick feedback on your mistakes and results, however, often provides a sense of accomplishment that keeps you programming until you get it right.

The highlights of this hour include the following:

Image Directing a computer with programs

Image Putting enough details in a program that a computer can follow the instructions

Image Executing programs in memory rather than from the hard drive

Image Looking at whether programming is an art or a science

Image Learning why computers cannot yet understand a human language

Understanding the Need for Programs

When individuals and companies need a program or an app, they can obtain it in one of three ways:

Image Buy a program that’s already written

Image Buy a program and modify it so that the customized version does exactly what they need

Image Write their own program

There are advantages and disadvantages to each option (see Table 2.1). The first two options are much quicker than the third and are much less expensive.

TABLE 2.1 Advantages and disadvantages of obtaining programs

Option Advantages Disadvantages
Buy The program can be obtained quickly and relatively inexpensively. The program may not be exactly what is needed.
Buy and customize A usable program that does what is needed can be obtained fairly quickly. Also, the program is relatively inexpensive, depending on the changes needed. It isn’t always possible to modify the program.
Write The program (after proper design and testing) does exactly what you want it to do. This option can be very expensive and can take a lot longer than the other options.

Most computer users (of PCs, Macs, tablets, and smartphones) choose the first option because the programs are fairly inexpensive, given their power. Because companies such as Apple, Microsoft, and Adobe sell so many of the same programs or apps, they can do so at reasonable prices. Individual computer users simply don’t have the resources that companies have to write the many programs they need.

Companies, on the other hand, do not always choose the first option, and you might wonder why. The reason is that companies spend many years developing products and services to distinguish themselves from other companies, and they learn to keep records in specific ways so that they can manage those services effectively. When a company computerizes any of its record keeping, it is vital that new programs reflect exactly what the company already does. The company should not have to change the way it does business just so it can use programs found in app stores or online. On the other hand, programs that can be purchased off the shelf have to be generic so that the producers of the programs can sell them to more than one customer.

The second option—buy a program and customize it—might seem like the smartest option, but it is chosen least often. If companies could buy a program that is already written, they would have a framework in which to quickly adapt the program to their specific needs. The problem is that software is rarely sold, as you learned in the previous hour; software is licensed. When you buy a program, you do not own the program; you only own the right to use it. You often cannot legally change it, sell it, or copy it (except for backup purposes). Some software does come with a license to revise the code, but most software that you purchase off the shelf does not allow the modification of code.

Note

There is a class of software and operating systems that not only allows modification of code but encourages it. Open-source software makes the code available with the program so that improvements to the program can be made by the public in a collaborative manner. The Linux operating system is a famous example of open-source software.

Not only are there legalities involved, but also sometimes you cannot physically change the software. Programs come in two formats:

Image Source code, such as the Python program you created in the previous hour

Image Compiled, executable code, such as apps you purchase for your phone or tablet

Once a program is written and compiled, programmers can no longer modify the program. Programs that are sold in stores do not usually include the source code. Therefore, companies cannot modify the programs they buy to make the programs conform to their specific businesses. (Of course, you can modify and re-compile programs that you write and compile because you have access to your own source code.)

So, although it is expensive and time-consuming to write programs from scratch, many businesses prefer to do so, and they keep large programming departments on hand to handle the programming load. A company might have several members of its data-processing staff spend a full year writing a program that is close, but not identical, to one that the company could buy. Companies also might utilize contract programmers for specific projects.

Despite the cost and effort involved, it is worth it to some companies not to have to conform to a program they buy from someone else. The program, once written, conforms to the company’s way of doing business.

Note

Some companies have found that they can sell programs they develop to other firms doing similar business, thereby recapturing some of their development costs. As you write programs for your company or for individuals, keep in mind the possible subsequent sale of your program to others.

Companies often measure the amount of time it takes to write programs in people-years. If it takes 2 people-years to write a single program, it is estimated that 2 people could write it in a single year, or 1 person would take 2 years. A 20-people-year project would take 20 people 1 year, or 1 person 20 years, or 10 people 2 years, and so forth. This measurement is only an estimate, but it gives management an idea of how it should allocate people and time for programming projects.

If you become a contract programmer, the people-years measurement is a great tool to use when pricing your service. You might give a customer an estimate of the price per people-year (or, for smaller projects, perhaps you would estimate the job in people-months or people-weeks). If you hire programmers to help you finish the program, you might finish early, but you can still charge fairly for each person’s labor because you priced the programming job in people-years rather than in calendar time.

Note

Recently, a lot of attention has been given to the idea of people-years. Critics of this measurement state that computer firms do not generally estimate people-year measurements properly and that such measurements are rarely meaningful. That may be, but just because people misuse the measurement does not make the measurement a bad way to gauge a project’s completion when it is estimated correctly.

Programs, Programs Everywhere

Why aren’t all the programs ever needed already written? Search the Internet today, and you’ll see hundreds of programs for sale or free to download. There are programs for everything: word processing, accounting, drawing, playing games, editing and manipulating photos and videos, and so much more. In addition, Apple, Amazon, Android, and many other sites contain thousands of apps you can download and run on your tablet or other mobile device. Some of these programs are free, and some are for sale. It seems as if any program you need is within reach. Because computers have been around more than half a century, you would think that everybody would be about done with all the programming anyone would need for a long time.

If all the programs needed were already written, you would not see the large listings of “Programmer Wanted” ads on job-listing websites. The fact is that the world is changing every day, and businesses and people must change with it. Programs written five years ago are not up to date with today’s practices. Also, they were written on computers much slower and more limited than today’s machines. Finally, earlier programs were written just for personal computers, while companies must now interact with users who access resources via tablets and smartphones. As hardware advances are made, the software must advance with it.

There is a tremendous need for good programmers today more than ever before. As computers become easier to use, some people believe that programmers will become relics of the past. What they fail to realize is that it takes top-notch programmers to produce those easy-to-use programs. More importantly, it takes programmers to modify and improve upon the vast libraries of programs in use today. Even with economic slumps, the world is becoming more digitized daily, and the demand for programmers will increase as more people use computers more often.

Programs as Directions

If you have ever followed your GPS into unfamiliar territory, you know what it is like for your computer to follow a program’s instructions. With only the instructions being called out, you can feel blind as you move from place to place, turning left and right, until you reach your destination or find that you made a wrong turn somewhere. Your computer, as the previous hour explained, is a blind and dumb machine waiting for you to give it directions. When you do, the computer acts out the instructions you give it without second-guessing your desires. If you tell your PC to do something incorrectly, it does its best to do so. Recall this definition of a program (from Hour 1, “Hands-On Programming”):

A program is a list of detailed instructions that the computer carries out.

The term detailed in this definition is vital to making a machine follow your orders. Actually, the job of programming is not difficult; what is difficult is breaking the computer’s job into simple and detailed steps that assume nothing.

Practice with Detailed Instructions

To get an idea of the thinking involved in programming, consider how you would describe starting a car to someone from the past. Suppose a cowboy named Heath, from the Old West, appears at your doorstep, bewildered by the sights around him. After getting over the future shock, Heath wants to adapt to this new world. Before learning to drive your car, Heath must first learn to start it. When he is comfortable doing that, you will teach him to drive. Unlike a 16-year-old learning to drive, Heath has not grown up seeing adults starting cars, so he really needs to master this process before going any further. Being the busy programmer you are, you leave him the following set of instructions taped to the car key:

  1. Use this key.

  2. Start the car.

How far would Heath get? Not very far. You gave correct instructions for starting a car, but you assumed too much knowledge on his part. You must remember that he knows nothing about these contraptions called automobiles, and he is relying on you to give him instructions that he can understand. Instead of assuming so much, these might be better instructions:

  1. Attached is the key to the car. You need it to start the car.

  2. With the key in hand, go to the car door that is closest to the front door of our home.

  3. Under the door’s black handle, you will see a round silver-dollar-sized metal place into which you can insert the key (with its rough edge pointing down).

  4. After sticking the key into the hole as far as it will go, turn it to the right until you hear a click.

  5. Turn the key back to the left until it faces the same way as it did when you inserted it and remove the key.

  6. Pull up on the black handle to open the door and get into the car. Be sure to sit in front of the round wheel (called a steering wheel) on the left-hand side of the front seat.

  7. Close the door.

  8. On the right side of the column holding the steering wheel, you will see a slot in which you can put the key.

Are you beginning to get the idea? This list of eight items is very detailed, and Heath hasn’t even started the car yet. You still have to describe the brake pedal that he might have to press while he turns the key, in the correct direction of course. You also don’t want to assume that Heath will turn off the car when he is done practicing, so you have to give him directions about that as well. (Perhaps you should also warn your neighbors to stay off the streets for a while!)

If you are beginning to think this car-starting analogy is going a little too far, consider what you must do to tell a nonthinking piece of electronic equipment—a computer—to perform your company’s payroll. A payroll program cannot consist of only the following steps:

  1. Get the payroll data.

  2. Calculate the payroll and taxes.

  3. Print the checks.

To the computer, these instructions lack thousands of details that you might take for granted. It is the detailing of the program’s instructions that provides for the tedium and occasional frustration of programming. Programming computers isn’t difficult, but breaking down real-world problems into lots of detailed steps that a computer can understand is hard.

Tip

A typical payroll program might contain 20,000 or more lines of instructions. Don’t let this deter you from learning to program, however. Large programming projects are written by teams of programmers; you will have plenty of help if you ever write such programs for a living. Also, new programming techniques and programming environments for today’s computer languages make programming, even for an individual programmer working alone, much easier than ever before.

Closer to Home: A Python Example

Consider the program output window shown in Figure 2.1. A Python program produced this output.

images

FIGURE 2.1
It takes detailed instructions to produce simple output.

Tools That Help

Many design tools exist to help you break down large problems into detailed components that translate into programming elements. Hour 3, “Designing a Program,” explains many of the methods that programmers use to get to a program’s needed details. You’ll find that programming is simple as long as you approach it systematically, as this text attempts to do. By learning theory and getting as much early, hands-on examples as possible, you’ll understand what you’re doing and learn how to program faster. If programming were truly difficult, there is no way so many computer advances could have been made over the past 70 years.

Programs Are Saved Instructions

The nice thing about programs you write is that you save them to disk after you write them. As with word-processed text, you store the programs you write in disk files, as you did in the previous hour’s lesson with the TYcode2 program. A program is to a computer as a recipe is to a cook. When a cook wants to make a certain dish, he or she finds the correct recipe and follows the instructions. When someone wants to execute a program, she or he instructs the computer to load the program from disk into memory and then run the program’s instructions.

The computer’s internal memory (random access memory, or RAM) is vital for holding program execution. Your computer’s CPU cannot execute a program’s instructions directly from the disk. Just as you cannot know what is in a book lying on a table until you read the book’s contents into your memory (using your own CPU—your mind), your CPU cannot process a program’s instructions until it loads the program from disk into main memory. Figure 2.2 shows the process of loading a program from the computer’s disk (or disk-like storage, such as a flash drive) into memory. As the figure shows, the CPU has direct access to memory but has no access to the disk drive. The disk is the long-term storage, and the memory is the short-term storage, where programs temporarily reside while the CPU executes them.

images

FIGURE 2.2
A program must be in memory before the CPU can execute the program’s instructions.

As you already know, a program differs greatly from the output. The program is a set of instructions, and the output is the result of those instructions. A recipe’s output is the finished dish, and the program’s output is the printed output that results when the instructions run.

Perhaps a specific example will further clarify what it means to a programmer for a user to use a program. If you use a word processor, then you probably follow steps similar to these:

  1. You load the word-processing program from your hard drive into the computer’s main memory. When you select the word-processing program’s name from the menu or select its icon, you are instructing the computer to search the disk drive for the program and load it into main memory.

  2. What you see onscreen is output from the program. You can produce more output by typing text on the screen. Everything that appears onscreen throughout the program’s execution is program output.

  3. After you type text, you may interact with other devices. You will probably issue a command to print the document (more than likely using the standard Windows File, Print menu option) to the printer and save the document in a data file on the disk.

  4. When you exit the word processor, your operating system regains control. The word-processing program is no longer in memory, but it is still safely tucked away on disk.

As you can see, the results of a program’s execution make up the output. The instructions produce those results. Figure 2.3 gives an overview of the program-to-output process. Modern-day programs produce output in many different ways. Programs play music, talk to other computers over Wi-Fi, and control external devices. Output sent to the screen and printer still makes up the majority of today’s program output.

images

FIGURE 2.3
The program comes from disk, executes, and then sends its results to any of the many output devices, such as the disk, screen, or printer (or more than one of them from within the same program).

When a program is in memory, it is not there alone. Your operating system always resides in memory. If it did not, you could neither load a program from disk nor run it because the operating system is what actually loads programs to and from memory when you issue the correct command. Limited memory often poses a problem for larger programs. Recall that a program processes data, and the data and the program must be in memory before the program can easily process the data.

Figure 2.4 shows what a typical computer installation’s memory looks like when a program is running. The operating system takes a big chunk, the program must be there, too, and finally there must be room for data.

images

FIGURE 2.4
A typical memory layout shows that the operating system shares memory with executing programs.

The more memory your PC has, the faster your programs run. The extra memory means that the operating system will have to swap less to and from disk as the program operates. A program may even be contained in memory in its entirety the whole time that the program runs. As a programmer, you should run with ample memory, as much as 256MB or 512MB, so that you can properly test your running programs while still keeping your programming environment loaded.

Art or Science?

A debate that you often see in computer literature is whether programming is an art or a science. Through the years, there have been advances made in programming that, if followed, improve a program’s accuracy, readability, and maintainability (the ability to change the program later to perform a different or additional set of tasks). Most of these advances are nothing more than suggestions; that is, programmers don’t have to use them to write programs that work.

Two of the most important advances in programming have been more philosophically based than engineered. They are structured programming and object-oriented programming. This book explores these two programming advances thoroughly in the hours that follow. They both offer ways that a programmer can write a program to make it better. Again, though, these are just suggested approaches to programming; programmers can (and many do) ignore them.

There are many ways to write even the smallest and simplest programs. Just as authors write differently and musicians play differently, programmers each have a unique style. Therefore, you would think that programming is more of an art than a science. On the continuum of science to art, you would be closer to being correct than those few who argue that programming is more of a science.

Nevertheless, as more advances are made in developing programming approaches, as has already been done with structured programming and object-oriented programming, you should see a shift in thinking. With the proliferation of computers in today’s world, there is a massive educational effort in process to train tomorrow’s programmers. Because programming is still a young industry, many advances are still left to be made.

Some of the biggest proponents of moving away from the artful approach to a more scientific approach, using structured and object-oriented programming, are the companies paying the programmers. Companies need to react quickly to changing business conditions, and they need programs written as quickly and as accurately as possible. As advances in computer programming are discovered, more companies are going to adopt policies that require their programmers to use more scientific and time-proven methods of writing better programs.

Speak the Language

The instructions you give in your programs must be in a language the computer understands, which, as you learned in Hour 1, is machine language. At its lowest level, a computer is nothing more than thousands of switches flipping on and off—lightning fast. A switch can have only one of two states; it can be on or off. Because either of these two states of electricity can be controlled easily with switches, many thousands of them control what your computer does from one microsecond to another.

If it were up to your computer, you would have to give it instructions using switches that represent on and off states of electricity. Actually, that is exactly the way that programmers programmed early computers. A panel of switches, such as the one shown in Figure 2.5, had to be used to enter all programs and data. The next time you find yourself cursing errors that appear in programs you write, think of what it would have been like programming 60 years ago.

images

FIGURE 2.5
Programmers used a panel of switches to program early computers.

The on and off states of electricity are represented as 1s and 0s at the computer’s lowest level. This representation has a name: binary. You can control what your computer does if you know the correct pattern of 1s and 0s required to give it commands. You can program a computer simply by issuing 1s and 0s if you have the correct system tools. Of course, programming in the binary digits of 1s and 0s is not much better than flipping switches up and down on the switch panel, so there has to be a better way.

Caution

Computers are not going to learn a human’s spoken language any time soon, despite what you might see in science fiction movies. You have to learn a programming language if you want a computer to do what you want, and you must interpret or compile that language into machine language.

Computers Cannot Handle Ambiguity

Spoken languages are too ambiguous to computers. People’s brains can decipher sentences intuitively—but a nonthinking machine can’t. Some inroads are being made into artificial intelligence, which is the science of programming computers so that they can learn on their own. It also includes programming them to understand a spoken language such as English. Despite recent advancements, artificial intelligence is many years away (if it is even possible for computers to understand simple English commands).

Consider the following sentence:

Time flies like an arrow.

Your mind has no trouble understanding the parts of this sentence. You know that it is an analogy, and the parts of speech make total sense to you. Time is a noun, and the verb describes the action that time performs: It flies, and it does so like an arrow. If you teach the computer to accept these descriptions of this sentence, it will work fine until it runs into something like this:

Fruit flies like an orange.

Think about this for a moment. Again, you have no problem understanding this sentence, even though it is completely different from the other one. The computer taught to decipher the first sentence, however, is going to throw its cables up in frustration at the second sentence because none of the parts of the sentence are the same. The word flies is now a noun and not an action verb. The phrase like an orange is no longer a description of the action but rather both the verb (like) and the object receiving the action (an orange). As you can see from these two sentences alone, understanding simple sentences that most people take for granted poses a tremendous problem for programmers trying to “teach” a computer to understand a language such as English.

Here’s another example.

The pop can has a lid.

Computers have extreme problems with statements such as this one because the sentence appears to have a subject/verb agreement problem. Does the sentence state that the can of pop has a lid or that the pop can have a lid? If it’s the latter, then has should be have. The human mind can easily distinguish these problems and knows that the can of pop has a lid, but given today’s technology, machines simply cannot easily do so.

Therefore, computers and people are at opposite ends of the spectrum. People want to speak their own language, but computers want to stick to their language comprised of 1s and 0s. There has to be some kind of go-between. Programming languages were created to try to appease both the computer and the person programming the computer. You’ve seen from Python that programming languages use words similar to those that people use, but programming languages have a highly specific syntax that allows little room for the ambiguity that is so prevalent in spoken languages. The computer can take a programming language and translate it to its machine language of 1s and 0s; the human programmer can learn, remember, and use the programming languages more effectively than 1s and 0s because programming languages appear similar to spoken languages, although they are more precise and simple.

Computers Speak Many Languages

You are going to learn a lot about several programming languages in this 24-hour course. You may have heard of some of the languages before. Over the years since the first computer language was invented, hundreds of programming languages have been written, but only a handful of them have prevailed to dominate the rest. Table 2.2 lists several programming languages that have gained more than obscure notoriety through the years.

TABLE 2.2 Many programming languages have appeared through the years

Algol PL/I
PROLOG LISP
COBOL Forth
RPG RPG II
Pascal Object Pascal
Go Ada
C C++
C# FORTRAN
SmallTalk Eiffel
BASIC Visual Basic
APL Java
Python PHP
JavaScript Ruby

The computer is like a modern-day tower of Babel, indirectly responsible for more programming languages than were ever needed. Most people considered to be computer experts might know only a handful of programming languages—from three to five—and they probably know only one or two very well. Don’t let the large number of languages deter you from learning to program.

There are lots of reasons there are so many programming languages. Different people have different preferences, some languages are better than others for different tasks, and some people have access to only one or two of the languages. For example, HTML is a language that programmers use for developing websites. FORTRAN was used in the past for highly mathematical applications, although other languages have supplanted FORTRAN in many programming departments. Java, JavaScript, and PHP, combined with HTML, are great languages for putting interaction on web pages. The emergence of agile development methodology has resulted in an increase in the use of Ruby programming.

Summary

There are several ways to obtain programs for computers, but to really make computers do what you want, you have to write programs yourself. Most programs that you purchase are merely licenses to use but not customize the programs. Companies need programs that enable them to do business the way they prefer and that do not force them to change their practices to conform to the programs.

Before you can program, you must learn a programming language. You may want to learn several languages, but your work and interest goals will determine which languages you learn. Perhaps you want to specialize in Internet languages, or perhaps you have a long-term Python contract. You’ll find that the more languages you learn, the easier it will be for you to pick up additional languages.

The next hour describes some of the background needed for proper program design.

Q&A

Q. Why do programs have to be so detailed?

A. Programs are detailed because computers are machines. Machines do not have intelligence. A computer blindly follows your instructions, step by step. If you do not give detailed instructions, the computer can do nothing.

Workshop

The quiz questions are provided for your further understanding.

Quiz

1. What are the three ways to acquire a new program?

2. Why do businesses often write their own programs, despite the extra expense required?

3. Why must programmers know a programming language?

4. Why do computers not understand human language?

5. Which is more ambiguous: human language or computer language?

6. True or false: To be useful, a programmer should know at least five programming languages.

7. Why is RAM-based internal memory so important to a running program?

8. Which languages from Table 2.2 do you think are derived from the C programming language?

9. Why might a programmer specialize in only one programming language?

10. What is open-source software?

Answers

1. You can buy a program that’s already written, buy a program with source code and modify the program, or write your own program from scratch.

2. Companies want their programs to conform to the way they do business.

3. Programmers must use programming languages to create programs.

4. Computers only understand a very low-level machine language and are not advanced enough to recognize higher languages.

5. Human language has more ambiguity than computer language.

6. False. A programmer might only know one or two languages and still be extremely useful and productive.

7. The more RAM your computer contains, the more of your program and data can fit into memory at the same time and the faster your program executes.

8. C++ and C# are derived from the C language.

9. A programmer might specialize in only one programming language because the programmer’s company may use only one language. Also, the more specialized the programmer is, the better the programmer will be in that skill. Nevertheless, most programmers are familiar with several languages and prosper more because of that knowledge.

10. Open-source programs are distributed with their source code along with the executable files. Users are encouraged to alter the code and improve the program, as long as they share their code changes with the community.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.113