Writing Programs in an Editor

January 30th, 2008 yudi Posted in Programming Tools No Comments »

An editor acts like a simple word processor where you can type the commands you want your computer to follow. When you save your commands, you save them in a text (or ASCII) file. Although you can use a word processor to create a text file, a word processor offers fancy formatting features (such as changing fonts or underlining text), which you don’t need to write a program.

Technical Stuff An ASCII file consists of nothing but characters that you can type from a keyboard. ASCII stands for American Standard Code for Information Interchange, which is simply a universal file format that any computer can use.

An editor looks like a word processor but may offer special features to make programming easier, such as automatically formatting your source code, offering shortcuts for editing your source code, or providing pop-up help as you’re typing program commands

AddThis Social Bookmark Button

Compiler and Interpreter

January 30th, 2008 yudi Posted in Programming Tools No Comments »

After you type your instructions in an editor by using a programming language such as C++ or Java, guess what? The computer doesn’t have the slightest idea what you just created. Computers understand only machine language, so you need to use another special program to convert your source code (the instructions that you write in C++ or Java) into machine language.

You can use either of the following two types of programs to convert source code into machine language:

  •  A compiler

  •  An interpreter

  •  A combination compiler/interpreter (p-code)

Compilers

A compiler takes your source code, converts the entire thing into machine language, and then stores these equivalent machine-language instructions in a separate file, often known as an executable file. The process is like having a translator study an entire novel written in Spanish and then translate and rewrite it into Arabic.

Remember Whenever a compiler converts source code into machine language, it’s compiling a program.

After you compile a program, you can just give away copies of the executable (machine-language) version of your program without giving away your source code. Most commercial programs (such as Microsoft PowerPoint and Quicken) are compiled, but you never see the source code.

After you use a compiler to convert source code into machine language, you never need to use the compiler again (unless you make changes to your source code).

Remember A compiler creates machine language for a specific microprocessor. If you write a program in BASIC and want to run it on a Macintosh and a Windows computer, you need to compile your program twice: once for the Macintosh and once for the Windows environment. However, many compilers, such as REALbasic, let you compile for multiple platforms at the same time. So if you wrote a program in REALbasic and compiled it for Windows and Macintosh, you’d wind up with a compiled executable file for Windows and a second compiled executable file for Macintosh.

Technical Stuff Not all compilers are equal. Given identical C++ source code, one C++ compiler may create a program that runs quickly, whereas a second C++ compiler may create a smaller file that runs much slower, yet both programs may look and work exactly alike.

Image from book

“Bootstrapping” a compiler

A compiler is nothing more than a program. So if you want to create a compiler, guess what? To write any type of a program, you need a compiler (or an assembler, which is just a special compiler for converting assembly language into machine language). So here’s the dilemma. How did computer scientists create the first compiler?

To create the first compiler, computer scientists used a technique called bootstrapping, derived from the phrase “pulling yourself up by the bootstraps.” First, they wrote the bare bones of the compiler in machine language, which the computer can understand without any translation whatsoever. Then they used this bare bones compiler to create an assembler so they could write more of the compiler in assembly language. Finally, when enough of the compiler had been built from a small base of machine-language code and a larger base of assembly-language code, they used the compiler itself to write additional instructions in a higher-level programming language (such as C) to build the rest of the compiler.

Nearly every language compiler is written partially or entirely in another programming language. Microsoft wrote the original Visual Basic compiler in assembly language but wrote later versions in C++. Shoptalk Systems wrote its Liberty BASIC compiler in a language called SmallTalk. So if you want to create a compiler for a brand-new programming language, you have to start creating that compiler by using an existing programming language.

Image from book
 

Warning! Your program is at the mercy of the compiler you use. Many Macintosh programs were created by using a compiler called CodeWarrior, but when Apple switched from PowerPC to Intel processors, guess what? CodeWarrior wouldn’t compile C++ source code for the new Intel Macs. So everyone (including Microsoft and Adobe) who had written C++ programs with CodeWarrior had to rewrite their C++ programs and compile them with Apple’s C++ compiler instead. Switching compilers and rewriting programs to run under a different compiler is rarely easy, so not only is it important to choose the right programming language, but also the right compiler.

Interpreters

A second, but less popular, way to convert source code into machine language is to use an interpreter. An interpreter converts each line of your source code into machine language, one line at a time. The process is like giving a speech in English and having someone translate your sentences, one at a time, into another language (such as French).

Whereas a compiler stores machine code in a separate file, an interpreter converts source code into machine language but stores the machine code in the computer’s memory. Every time that you turn off the computer, you lose the machine-language version of your program. Each time you want to run the program, you must feed the source code into the interpreter again.

If anyone wants to run your program, that person needs both an interpreter and the source code for your program. Because your source code enables everyone to see how you wrote your program (and gives others a chance to copy or modify your program without your permission), few commercial programs use an interpreter.

Interpreters are often used for Web page programming languages, such as JavaScript. Because different computers can view Web pages, you can’t compile programs that you write in JavaScript into machine language because people with different computers may visit your Web site. Instead, your computer’s browser uses an interpreter to run a JavaScript program.

Technical Stuff In the old days, when computers were slow and lacking in sufficient memory and storage space, interpreters were popular because they gave you instant feedback. The moment you typed an instruction into the computer, the interpreter told you whether that instruction would work and even showed you the results. With an interpreter, you could write and test your program at the same time. Now, computers are so fast that most programmers use compilers rather than interpreters, although a handful of languages like LISP still rely on interpreters.

P-code: A combination compiler and interpreter

Getting a program to run on different types of computers is a big pain in the neck. Even though Macintosh and Windows programs use pull-down menus and dialog boxes, programs often need to write one set of commands to create Macintosh pull-down menus and a second set of commands to create the identical menus in Windows.

Because one program almost never runs on multiple computers without extensive modification, programmers combined the features of a compiler with an interpreter to create something called p-code.

Instead of compiling source code directly into machine language, you compile the source code into a special intermediate file format (called p-code or byte code).

To run a program compiled into p-code, you need a special p-code or byte code interpreter, often called a run-time file. To run a p-code file on a Macintosh, your computer needs a Macintosh p-code run-time file. To run that same p-code file on Windows, your computer needs a Windows p-code runtime file. Instead of creating compilers for multiple computers, it’s often easier just to create different run-time files for multiple computers.

Java is the most popular programming language that uses p-code. After you compile a Java program into p-code, you can copy that p-code to a Macintosh, a Windows computer, or a Linux computer. As long as that computer uses a Java p-code interpreter, you can run the Java program on that computer without modification.

Best of all, programs that you compile into p-code can run without the original source code, which means that you can protect your source code and still give your program away to others.

Remember Naturally, p-code has its own disadvantages. Programs stored as p-code tend to run much slower than programs that compiled directly into machine language. Although p-code programs can run without a copy of the original source code that you use to create them, you can also decompile p-code programs.

Decompiling a p-code program can reveal the original source code that the programmer used to create the program. So if you write a program in Java and compile it into p-code, a rival can decompile your p-code program and see your original Java source code. Your rival then ends up with a nearly identical copy of your source code, which gives him the opportunity to steal your program.

Technical Stuff You can actually decompile any program, including programs that you compile into machine language. But unlike with decompiling p-code programs, decompiling a machine-language version of a program never gets you the original high-level language source code that the programmer used to write the program. If you compile a program into machine language, the original source code can be written in C++, COBOL, FORTRAN, BASIC, Ada, LISP, Pascal, or any other programming language in the world. Because the decompiler has no idea what language the original source code was written in, it can decompile a machine-language version of a program only into equivalent assembly language. After you decompile a program into assembly-language source code, you can rewrite or modify that source code. Decompiling effectively allows you to steal the ideas of others, but it’s often used to dissect computer viruses and worms to understand how they work (and how to defend against them).

So what do I use?

If you want to write programs to sell, use a compiler, which protects your original source code. If you want to write a program to run on your Web page, you can use either an interpreter or p-code. If you want to write a program that can run on different types of computers, p-code may prove your only choice. As a safer but more cumbersome alternative, you can also use multiple compilers and modify your program to run on each different computer.

Remember The language that you choose can determine whether you can use a compiler, an interpreter, or p-code. You often convert Java programs into p-code, for example, although you can also compile them directly into machine language. On the other hand, you usually compile C++ and rarely interpret or convert C++ programs into p-code.

AddThis Social Bookmark Button

Eliminating Bugs

January 30th, 2008 yudi Posted in Programming Tools No Comments »

No computer program works 100 percent correctly, which explains why computers crash, lose airline reservations, or just act erratically at times. Mathematically, writing a program that works 100 percent correctly every time is impossible because testing a program for all possible types of computers, hardware, and additional software that may interfere with the way your program runs is impossible.

Image from book

The .NET framework

If you try running a program written in Visual Basic, it won’t run unless it finds a copy of its run-time file on your computer, which usually has an odd name like VBRUN300.DLL or MSVBVM60.DLL. With the latest version of Visual Basic, Microsoft has replaced the Visual Basic run-time file with a whole bunch of files that make up something called the .NET framework.

Unlike the old Visual Basic run-time files that could run only Visual Basic programs, the .NET framework is designed to run any programs that have been compiled to p-code for the .NET framework. So instead of requiring a separate run-time file for a Visual Basic program and second run-time file for a Visual C# program, computers now just need a single copy of the .NET framework.

By porting the .NET framework to other operating systems, such as Linux or the Macintosh, it’s (theoretically) possible to run Visual Basic and Visual C# programs on other types of computers. Until this happens though, Visual Basic and Visual C# programs run only on computers that can install the .NET framework, which currently limits Visual Basic and Visual C# programs to any computer that uses Windows.

Image from book
 

A problem that keeps a program from working correctly is known as a bug.

Technical Stuff In the early days, computers used mechanical relays and vacuum tubes rather than circuit boards and microprocessors. One day, the computer failed to work correctly. The scientists examined their program; it should have worked. So they next examined the computer itself and found that a moth had gotten smashed in a mechanical relay, preventing it from closing and thus keeping the computer from working correctly. From that point on, problems in computers have been called bugs. So it’s a good thing that a dog never got trapped in a mechanical relay because then computer problems might be called Chihuahuas.

Because writing a program that works 100 percent correctly all the time is impossible, operating systems (such as Windows XP) unavoidably contain bugs that keep them from working correctly. When you convert your source code into machine language, you must use a compiler or interpreter, which also contain bugs. Finally, your program may contain bugs of its own. With so many places for bugs to creep in, you shouldn’t be surprised that bugs infest computer programs the way cockroaches infest cheap apartment complexes.

Although you can do little about bugs in other people’s programs (except not buy their programs), you can reduce (but not completely eliminate) bugs in your own program by using a debugger. A debugger is a special program (which may also contain bugs) that can help you track down and wipe out bugs in programs that you write.

A debugger provides several ways to track down bugs in your program:

  •  Stepping: The debugger runs your program, line by line, so that you can see exactly which line may contain the bug. This process is like rereading written instructions to get to another person’s house if you’re lost. By going over these instructions, one by one, you can find out where you made a wrong turn.

  •  Breakpoints: Rather than force you to step through an entire program, line by line, breakpoints enable you to specify where you want to start examining your program line by line. So if you were lost, instead of rereading the instructions to get to another person’s house from start to finish, you skip those instructions that you know you followed correctly and examine only the remaining instructions that you aren’t sure that you followed correctly. Similarly, by using breakpoints, you can selectively examine only parts of your program, line by line, and skip over the parts that you know already work.

  •  Watching: Watching enables you to see your program storing data in memory and to determine what that data may be. If your program stores incorrect data (such as saving a name instead of a telephone number), you know exactly where in your program the bug is occurring.
    Each time you examine a line in your program, the debugger shows you how that particular line affects the value you’re watching. As soon as you see the value change, the debugger shows you exactly which line in your program caused that change. This process is like having someone tell you to drive ten miles south down a certain road after turning right. The moment that you exceed ten miles, a watch-point alerts you so that you know exactly where you almost made a mistake and got lost.

Remember A debugger essentially shows you exactly how a computer is going to interpret the instructions in your program. That way you can see where you need to modify your instructions so that the bug doesn’t occur anymore. Of course, if you fix one bug, you may introduce several new ones. That’s why writing bug-free programs is impossible.

AddThis Social Bookmark Button

Writing a Help File

January 30th, 2008 yudi Posted in Programming Tools No Comments »

Few people have trouble using a doorknob, a toaster, or a microwave oven, but many more people still complain that computers and video recorders are too hard to use.

The problem with video recorders lies in the cryptic controls that aren’t easy to figure out just by looking at them. Similarly, the problem with computer programs is that programs are too complex to use at first glance. If you can make a program that’s easy to use, people can actually use it.

Because computer programs are still being designed for programmers by other programmers, computers still mystify the average user. To help the poor befuddled user, most programs now offer Help files.

A Help file provides instructions and explanations on-screen. Theoretically, if the user experiences trouble with the program, he or she can browse through the Help file , find an explanation or step-by-step instructions, and continue using the program.

Although Help files still can’t substitute for designing a program that’s easy to use in the first place, most programs offer Help files anyway. To keep your program modern and up-to-date, include a Help file with it.

To create a Help file, you can use a special Help-file-authoring program, which simplifies the process of creating and organizing topics for your Help file.

AddThis Social Bookmark Button

Creating an Installation Program

January 30th, 2008 yudi Posted in Programming Tools No Comments »

After you write your program, test it, debug it, and write a Help file for it, the final step is to give or sell copies of your program to others. Although you can copy your program onto a CD or DVD and force buyers to manually copy your program to their hard drive, doing so can cause problems. Users may not copy all the files correctly. Even worse, forcing users to manually copy your program to their hard drives may prove such a nuisance that most people won’t bother even using your program.

To make copying a program to a user’s hard drive as easy as possible, most commercial programs include a special installation program. Users run this installation program, which automatically copies a program and all necessary files to the appropriate location on the user’s hard drive. By making the installation of a program practically foolproof, software publishers make sure that users install the programs correctly.

So the final step to distributing your program to others is to use a special installation-creation program, which can smash all your program files into a single file that can automatically install itself on another computer.

Installation programs offer the following features for distributing programs to others:

  •  File compression: Most programs are fairly large, which means that they can’t always fit on a single CD. Rather than store your program on multiple CDs, an installation program can smash your files so that they can fit on a single disc.

  • Display graphics and play sounds: Installing a program usually takes a few minutes while the computer copies files from the CD to its hard drive. Rather than force the user to stare into space, an installation program can display advertisements or messages to make the installation process mildly interesting. Other uses for graphics include displaying an hourglass icon or a status bar that shows how far the installation is complete, such as 54 percent complete. That way, users know how much longer they need to wait.

  •  Simplify the copying process: Most important, an installation program simplifies copying your program to the user’s hard drive, making the entire process accurate, fast, and foolproof.

Remember The first impression that people get from your program is through its installation process, so an installation program helps give your program a professional appearance.

Of course, you need to make sure that your installation program installs your program correctly, or the first impression that users get from your program will be a highly negative one.

Armed with all these different tools (editor, compiler, debugger, Help file creator, and installation program), you’re ready to write, test, and distribute your programs. Who knows? You just might wind up writing a new category of software that nobody ever thought of before, like 19-year-old, college dropout Shawn Fanning did when he created Napster, which revolutionized the way the world distributes music and video files.

If a kid who didn’t even finish college can change the world by writing a single computer program, just think what you can do with your imagination.

AddThis Social Bookmark Button




eXTReMe Tracker