Chapter I.4. Programming Tools

The two most important tools a programmer needs are an editor and a compiler. An editor lets you type and save language commands (called the source code) in a plain text file. (Unlike a word processor file, a text file doesn't contain any formatting, like italics or fonts.) A compiler converts your source code into machine code and stores those machine code commands in a separate file (often called an executable file). After you store your program in an executable file, you can sell and distribute that executable file to others.

An editor and a compiler are absolutely necessary to write and distribute programs. However, most programmers also use a variety of other tools to make programming easier. To help them track down problems (bugs) in a program, programmers use a special tool, a debugger. To help them identify which parts of a program may be making the entire program run too slow, programmers can use another tool, a profiler.

For distributing programs, programmers often use a help file creator and an installer program. The help file creator makes it easy for the programmer to create, organize, and display help that the user can read while using the program. The installer program makes it easy for users to copy all the necessary files on to their computer so the program runs correctly.

Finally, programmers may also use a special tool — a disassembler — which can pry open another program to reveal how it works. Disassemblers are often used by security professionals to analyze how viruses, worms, and spyware programs work. For less honorable uses, programmers can also use a disassembler to dissect a rival program and study how it works.

Warning

Programmers often get so attached to their favorite programming tools that they'll argue the merits of their favorite editor or compiler with all the passion of a religious fanatic. Just as there is no single programming language that's the best language to use at all times, so there is no single programming tool that's the best tool to use all the time.

Choosing a Compiler

No two compilers work exactly alike, even compilers designed for the same language, such as two competing C++ compilers. It's perfectly possible (and quite common) to write a program that works perfectly with one compiler but doesn't run at all under another compiler without minor (or massive) changes.

When Microsoft wrote the Macintosh version of their Microsoft Office suite, they used CodeWarrior, which is a C++ compiler. Unfortunately, the Code Warrior compiler ran only on the PowerPC processors, which were used in the older Macintosh computers. When Apple switched to Intel processors, Microsoft had to dump the CodeWarrior compiler and use a different compiler, Xcode.

Because CodeWarrior and Xcode are both C++ compilers, Microsoft could theoretically compile the same C++ program under both CodeWarrior and Xcode with no problems. Realistically, Microsoft had to rewrite major portions of their C++ programs just to get them to run under the Xcode compiler. So the moral of the story is that switching compilers is rarely an easy decision, so it's important to choose the "right" compiler from the start.

Warning

At one time, the CodeWarrior compiler was considered the "right" compiler to use for creating Macintosh programs. What made CodeWarrior suddenly turn into the "wrong" compiler was when Apple switched from PowerPC processors to Intel processors. Everyone who had used the CodeWarrior compiler had to switch to the Xcode compiler. So what may seem like the "right" compiler at the time could later turn out to be the "wrong" compiler later through no fault of your own or the compiler company.

When choosing a compiler, you have to consider your needs, the compiler company's reputation, and the compiler's technical features.

Defining your needs for a compiler

The most important choice for a compiler centers solely on what you need. Follow these steps:

  1. Decide which programming language you want to use.

    If you want to write C++ programs, you need a C++ compiler. If you want to write BASIC programs, you need a BASIC compiler.

  2. Decide which operating system you want to use.

    If you want to write C++ programs for the Mac OS X operating system, your choices immediately narrow down to the small list of C++ compilers that run under the Mac OS X operating system.

  3. Choose a compiler that has the best chance of being around years from now.

    • Most companies prefer using compilers from brand-name companies, like Intel or Microsoft.

      Warning

      Even compilers from big-name companies are no guarantee against obsolescence. Microsoft has stopped supporting their compilers over the years, such as Microsoft Pascal and Visual Basic 6. So if you used either of these compilers to write a program, you had to change compilers when Microsoft stopped developing them.

    • Many people are choosing "open source" compilers.

      Open source simply means that the source code to the compiler is available freely to anyone. Not only does this mean that open source compilers are free (compared to the hundreds of dollars you can pay for a brand-name compiler from Intel or Microsoft), but open source also guarantees that the compiler can't become obsolete due to lack of support.

Warning

If you use a compiler from a company that goes out of business, you're forced to transfer (or port) your program to another compiler, which means having to rewrite the program to run under a different compiler.

Because anyone can examine and modify the source code to an open source compiler, anyone can make changes to the compiler to improve it. One of the most popular open source compilers is GCC (http://gcc.gnu.org), which stands for GNU Compiler Collection.

Note

Xcode, the free compiler that Apple distributes with every Macintosh computer, is actually the GCC compiler.

Originally, GCC only compiled C source code, but later versions of GCC now compile several different languages, including C, C++, Java, Ada, and Objective-C, with more programming languages being supported every day. Even better, the GCC compiler also runs on a variety of operating systems, such as Windows and Linux, so if you write a program using the GCC compiler, you can recompile your program to run under another operating system with minimal (hopefully) changes.

Note

The GCC compiler actually consists of two parts:

  • The front-end of the compiler translates source code into an intermediate format.

    • To write C++ programs, you must use the C++ front-end of the GCC compiler.

    • To write Ada programs, use the Ada front-end of the GCC compiler.

    • By creating front-ends for different languages, programmers can make the GCC compiler compile more programming languages.

  • The back-end of the compiler finishes translating the intermediate code into actual machine code.

Evaluating the technical features of a compiler

After you choose a particular programming language and pick which operating systems you want your programs to run on, your list of compiler choices is likely narrowed down to one or two choices. Given two compilers that both meet your needs, you can pick the "best" compiler by examining their technical features.

Warning

The technical features of a compiler are meaningless if

  • The compiler stops being developed and supported.

  • The compiler can't run under the operating system or processor you need in the future.

  • A particular technical feature is something you don't need or care about.

Supported language standards

No two compilers are alike, even those that compile the same programming language, such as C++. The problem is that every programming language has an official "standard," but the standard for most programming languages is usually far behind what people in the real world are actually using. (By the time an official standards committee agrees on the features of a given programming language, programmers have already created new features that eventually become standards in future versions of that language.)

As a result, most compilers support a given language standard plus additional features that programmers have developed. Therefore, every compiler actually works with a different dialect of a programming language. So C++ programs that run under the Microsoft Visual C++ compiler may or may not run the same when compiled under the GCC compiler, even though both compilers claim to support the "standard" C++ programming language.

Warning

Language standards are nice but generally useless when comparing compilers. What's more important is whether a particular compiler offers the specific features you need or want, regardless of whatever particular standard it may follow.

Code generation and optimization

Every compiler converts source code into machine language, but some compilers can translate source code into more efficient machine language commands than other compilers. As a result, it's possible to compile the same C++ program under two different C++ compilers and create identically working programs that consist of different machine language instructions.

The goal of every compiler is to create a program that takes up as little memory and disk space as possible while running as fast as possible. Usually, compilers make a trade off. To make a program run faster, the executable file may take up a large amount of disk space or require a lot of memory. If the compiler can reduce the size of your program and the amount of memory it needs to run, it may create a slow program.

To help you tweak your program for the best balance of speed, size, and memory requirements, many compilers offer optimization settings. By fiddling with these optimization settings, you can tell the compiler how to speed up or shrink your program, as shown in Figure 4-1.

Compiler optimization settings let you make your program as small and as fast as possible.

Figure I.4-1. Compiler optimization settings let you make your program as small and as fast as possible.

One major feature of a compiler's code generation capabilities involves speed, which can measure two different features:

  • Speed can refer to how quickly the compiler works in translating your source code to machine code.

    In the old days, compilers could take hours or days to compile a simple program. Nowadays, compilers often work in minutes or even seconds. Shove in a program that consists of 800,000 lines of code, and in less than a minute, the compiler can create an executable file for you. The faster the compiler works, the less time you waste running and testing your program.

  • The speed of a compiler can refer to the performance of the machine language code that the compiler creates.

    Given the same program, one compiler may create a program that runs quickly whereas a second compiler may create that same program that runs much slower.

Warning

Ideally, you want a compiler that both compiles fast and creates programs that run fast.

Target platforms

Most compilers can compile programs only for a specific operating system, such as Windows or Linux. However, what happens if you need to write a program that runs on two or more operating systems?

You could write the program twice with two different compilers — one for each operating system. So if you wanted to write a C++ program that runs under Windows and Mac OS X, you could compile that program by using Microsoft Visual C++ (for Windows) and then write a similar program to compile by using Xcode (for Mac OS X).

Of course, writing the same program two times for two different compilers on separate operating systems is a tremendous waste of time. As an alternative, some compilers are known as cross-compilers — they can create programs that work on multiple operating systems, such as Windows, Linux, and Mac OS X. Figure 4-2 shows the REALbasic cross-compiler, which lets you choose whether to compile a program for Windows, Linux, or Mac OS X.

A cross-compiler lets you write a program and compile it for multiple operating systems at the click of a mouse.

Figure I.4-2. A cross-compiler lets you write a program and compile it for multiple operating systems at the click of a mouse.

With a cross-compiler, you can write a program once and compile it to run on multiple operating systems, effectively doubling or tripling your potential market. Without a cross-compiler, you need to write a program for each compiler, under a different operating system, essentially doubling or tripling your work.

Warning

Although the idea of writing a program once and having it run on multiple operating systems might seem appealing, cross-compilers aren't perfect. Chances are good you'll have to tweak your program to run under each operating system, but those minor tweaks can be much easier than rewriting huge chunks of your program if you had to use two separate compilers.

Cost

Paying more for a compiler doesn't necessarily mean you're getting a better compiler. The GCC compiler is free and is one of the best compilers available. Generally, if you want to use a popular programming language, such as C++, you can find a free compiler, but if you want to use a more obscure programming language, such as LISP or Modula-2, you'll probably have to pay for a compiler.

Because Windows is the most common operating system in the world, you can find plenty of free compilers. Some of these compilers are free only for personal use, but if you want to create programs to sell commercially, you have to pay for the compiler. Table 4-1 lists some popular free Windows compilers.

Table I.4-1. Free Windows Compilers

Compiler Name

Programming Language

Where to Find It

GCC

C, C++, Objective-C, Java, FORTRAN, and Ada

http://gcc.gnu.org

Visual Basic Express 2005

BASIC

http://msdn.microsoft.com/vstudio/express

Visual C# Express 2005

C#

http://msdn.microsoft.com/vstudio/express

Visual C++ Express 2005

C++

http://msdn.microsoft.com/vstudio/express

Turbo C++

C++

www.turboexplorer.com

Turbo C#

C#

www.turboexplorer.com

Turbo Delphi

Pascal

www.turboexplorer.com

Dev-C++

C++

www.bloodshed.net

Dev-Pascal

Pascal

www.bloodshed.net

Free Pascal

Pascal

www.freepascal.org

Visual Prolog

Prolog

www.visual-prolog.com

GNU Prolog

Prolog

http://gprolog.inria.fr

The Mac OS X operating system is much less common than Windows, so you can choose from far fewer free compilers. However, Table 4-2 lists some Mac OS X compilers that you can use for free.

Table I.4-2. Free Mac OS X Compilers

Compiler Name

Programming Language

Where to Find It

Xcode

C, C++, Objective-C

http://developer.apple.com/tools/xcode

GNU Pascal

Pascal

www.gnu-pascal.de

Free Pascal

Pascal

www.freepascal.org

Note

The original Macintosh operating system was written in Pascal, but the newer Mac OS X operating system is written in Objective-C.

Because Linux is free, most Linux compilers are free as well. Table 4-3 lists some popular free Linux compilers. The REALbasic Linux compiler is free, but if you want REALbasic's cross-compilation feature to develop programs for Windows and Mac OS X, you have to pay for the professional edition. Likewise, the Intel C++ and FORTRAN compilers are free only for non-commercial use. If you plan to develop commercial applications, you have to pay for the compiler.

Table I.4-3. Free Linux Compilers

Compiler Name

Programming Language

Where to Find It

GCC

C, C++, Objective-C, Java, FORTRAN, and Ada

http://gcc.gnu.org

GNU Prolog

Prolog

http://gprolog.inria.fr

GNU Pascal

Pascal

www.gnu-pascal.de

Free Pascal

Pascal

www.freepascal.org

REALbasic

BASIC

www.realbasic.com

Intel C++

C++

www.intel.com

Intel FORTRAN

FORTRAN

www.intel.com

Finding an Interpreter

Interpreters are commonly used for scripting languages, such as Perl or Python, but rarely used for systems programming languages, such as C++. That's because if you write a program and use an interpreter, you must distribute a copy of your source code with the interpreter. Giving away your source code essentially gives away your program, so most commercial programs use a compiler instead.

In the old days, many companies sold language interpreters (the most popular were interpreters for running BASIC programs), but nowadays, most interpreters are given away for free as part of an operating system (such as Mac OS X) or another program (such as Microsoft Word). Interpreters for open source programming languages, such as Perl, Python, and Ruby, are also given away for free.

Operating system interpreters

One of the most common uses for an interpreter is for creating short programs to automate another program, such as an operating system. The Mac OS X operating system includes an interpreter, dubbed Script Editor, for running programs with the AppleScript language. After you write a program (script) that's particularly useful, you can even compile it into a standalone program, too.

Warning

The Script Editor program is usually buried inside the Applications folder on your Macintosh's hard disk.

Although Windows doesn't include a similar macro language interpreter, you can visit Microsoft's web site (www.microsoft.com/downloads) and download Microsoft PowerShell, a free interpreter.

PowerShell is designed for system administrators (people who need to control multiple computers, such as those connected to a network) to control and automate Windows. By writing simple programs in PowerShell, administrators can automate their routine tasks, such as starting or stopping a program, or moving files from one location to another.

PowerShell consists of simple commands (called command-lets or cmdlet) that perform a single task. By combining multiple cmdlets together where one cmdlet feeds data into another cmdlet, you can create more complicated programs.

Web page interpreters

Nearly every Web browser comes with a JavaScript interpreter. Web page designers use JavaScript for creating interactive Web pages, verifying information typed on a Web page (such as a username and password), or opening pop-up windows that display advertisements.

Note

JavaScript is the programming language used to create DashBoard widgets, which are mini-programs that can pop up on the screen of a Mac OS X computer.

Note

Most Web browsers include a free JavaScript interpreter, but to avoid trademark problems, Microsoft calls their JavaScript interpreter JScript, which they include with their Internet Explorer browser.

Although JavaScript interpreters can be found in any Web browser, you may have to download and install interpreters for other programming languages. Some popular programming languages for running programs on Web servers (those computers responsible for displaying and retrieving information from Web pages, such as shopping data) include

  • PHP (www.php.net)

  • Perl (www.perl.com)

  • Python (www.python.org)

  • Ruby (www.ruby-lang.org)

The preceding four languages not only have free interpreters that you can copy, but their interpreters also run on different operating systems. That means a Ruby or Perl program written on a Windows computer should run identically if copied and run on a Linux or Mac OS X computer.

Compiling to a Virtual Machine

The problem with compilers is that they're difficult to make for multiple operating systems and processors. The problem with interpreters is that they need the source code of a program to run, making interpreters unsuitable for distributing software. To solve both these problems, computer scientists created a third alternative — a virtual machine.

To protect the source code of a program, a virtual machine lets you compile your program into an intermediate file called bytecode or pseudocode (also known as p-code). To make a program run on multiple operating systems, you need a virtual machine that runs on each operating system, as shown in Figure 4-3.

A virtual machine acts like a combination of an interpreter and a compiler.

Figure I.4-3. A virtual machine acts like a combination of an interpreter and a compiler.

Note

When you compile a program into bytecode, it's still possible to reverse engineer, or disassemble, that bytecode file and view the original source code.

The most popular programming language that uses a virtual machine is Java (http://java.sun.com), which was created by Sun Microsystems. The idea behind Java is to let you write a single program in Java, compile it into a bytecode file, and then distribute that bytecode file to any computer that has a Java virtual machine installed.

Note

IBM has created and released its own Java compiler dubbed Jikes (http://jikes.sourceforge.net), which is supposedly faster than the official Java compiler from Sun Microsystems. Another Java compiler, GCJ (http://gcc.gnu.org/java), can compile Java programs into either bytecode or machine code.

Theoretically, you can write a program once and make it run on Windows, Linux, and Mac OS X with no modifications whatsoever. Realistically, you may still need to tweak the program a bit to get it to run flawlessly on different operating systems, but that's still much easier than writing a program from scratch for another operating system.

Although Java can run the same program on multiple operating systems, the virtual machine will never be as fast as a similar compiled program. As a result, Java programs can run sluggishly, especially on older computers.

Despite these drawbacks, Java has grown in popularity. Many companies write and sell programs entirely written in Java. As computers get faster and Sun Microsystems improves the performance of its virtual machine, programs written in Java probably will run fast enough for most uses.

Writing a Program with an Editor

To write programs, you need an editor, which acts like a special word processor just for helping you write commands in your favorite programming language. After you type your program commands in an editor, you can save this file (known as the source code). Then, you can feed this source code file into a compiler to turn it into a working program.

Editors

You can choose from two types of editors. Your best bet probably depends on whether you write programs in more than one programming language.

Standalone

A standalone editor is nothing more than a separate program that you run when you want to edit your program. You run the compiler separately.

Tip

If you regularly write programs in different programming languages, you might want to use a standalone editor. That way you can get familiar with all the features of a single editor.

You can buy standalone editors, but here are two popular free ones. Both of these editors run on multiple operating systems (such as Windows and Linux):

  • GNU Emacs (www.gnu.org/software/emacs/emacs.html)

  • VIM (www.vim.org)

Tip

The sidebar, "Common editor features," lists features you find in most editors, including standalone editors.

Integrated development environment (IDE)

An integrated development environment (IDE) combines an editor with a compiler in a single program so you can easily edit a program and compile it right away. An IDE gives you access to these features within a consistent user interface, as shown in Figure 4-4.

Tip

If you mostly write programs in a single programming language, using an IDE can be more convenient than a standalone editor.

An IDE provides access to multiple programming tools within a single user interface.

Figure I.4-4. An IDE provides access to multiple programming tools within a single user interface.

Features

In addition to a compiler and all the usual features of standalone editors (as listed in the sidebar, "Common editor features"), many IDEs include other features in a convenient user interface:

  • Debugger helps identify problems in your program.

  • File management helps organize the source code for your various projects.

  • Profiler helps identify which parts of your program may be slowing down the performance of your entire program.

  • GUI designer helps you design the appearance of your program's windows, pull-down menus, and buttons.

Free software

Many compilers come with their own IDE, but you can always use another IDE or a standalone editor instead. These IDEs are popular (and free):

  • NetBeans: www.netbeans.org

    Designed for writing Java programs but can also be used for writing C/C++ programs as well. Available for multiple operating systems.

  • Eclipse: www.eclipse.org

    Designed for writing Java programs, but can also be used for writing C/C++, PHP, and even COBOL programs as well. Available for multiple operating systems.

  • SharpDevelop: www.icsharpcode.net

    A Windows-only IDE designed for writing C# and Visual Basic.NET programs.

Fixing a Program with a Debugger

Eventually, everyone makes a mistake writing a program. That mistake could be as simple as typing a command wrong or forgetting a closing parenthesis, or it can be as complicated as an algorithm that works perfectly except when receiving certain data. Because writing error-free programs is nearly impossible, most programmers use a special tool — a debugger.

Warning

Program errors are dubbed bugs, so a debugger helps you find and eliminate bugs in your program.

Two common debugger features include

  • Tracing or stepping

  • Variable watching

Note

Not all bugs are equal:

  • Some bugs are just annoying, such as the wrong color on a pull-down menu.

  • Some bugs are critical, such as a bug that adds two numbers wrong in an accounting program.

  • Any bug that keeps a program from running correctly is a show stopper.

Stepping line by line

Stepping (or tracing) lets you run your program, line by line, so you can see exactly what the program is doing at any given time. The second you see your program doing something wrong, you also see the exact command in your program that caused that problem. Then you can fix the problem, as shown in Figure 4-5.

Warning

Sometimes when programmers find one error and fix it, their fix accidentally creates another error in the program.

Here are the two types of debuggers:

  • Source-level: Lets you examine your source code line by line. So if you write a program in BASIC, a source-level debugger shows you each line of your entire BASIC program.

  • Machine-language: Lets you examine the machine language code, line by line, that your compiler created from your source code. Programmers often use machine-language debuggers to examine programs when they don't have access to the source code, such as a computer virus or a rival's program.

Stepping line by line through a small program may be feasible, but in a large program that consists of a million lines of code, stepping line by line would take far too long. So to make stepping easier, most debuggers include breakpoints and stepping over/stepping out commands.

Stepping through a program, line by line, can help you find errors or bugs in your program.

Figure I.4-5. Stepping through a program, line by line, can help you find errors or bugs in your program.

Breakpoints

A breakpoint lets you skip over the parts of your program that you already know work. So if you have a program that's 10,000 lines long and you know the problem is somewhere in the last 1,000 lines of code, there's no point stepping through those first 9,000 lines of code.

A breakpoint lets you tell the computer, "Skip from the beginning of the program to the breakpoint and then step through the rest of the program line by line." Figure 4-6 shows how you can highlight a line with a breakpoint. That way your program runs from the beginning to the first breakpoint. After your program stops at a breakpoint, you can step through the rest of your program line by line.

Breakpoints let you skip over parts of your program that you don't want to examine line by line.

Figure I.4-6. Breakpoints let you skip over parts of your program that you don't want to examine line by line.

Over and out

The stepping over and stepping out commands are used to debug a large program that consists of multiple subprograms. Normally, stepping would force you to examine every subprogram, line by line. However, what if you know the problem isn't in a specific subprogram?

By using the step over and step out commands, you can avoid stepping through lines of code stored in a subprogram.

Step over

To avoid stepping through every subprogram, debuggers let you use the step over command. This tells the computer, "See that entire subprogram? Treat it as a single command and don't bother stepping through it line by line." Figure 4-7 shows how the step over command works.

The step over command lets you completely skip over any lines of code stored inside of a subprogram.

Step out

Suppose you start examining a subprogram line by line and suddenly want to stop. As an alternative to stepping through the rest of the subprogram, you can use the step out command, which tells the computer, "Stop stepping through the subprogram line by line right now!"

Watching variables

When you step or trace through a program, line by line, you can see how the program works. For more insight into your program's behavior, you can watch your variables.

Tip

You can read more about variables in Book II, Chapter 2. For now, just think of a variable as a temporary place to store data, such as a number or a word.

Watching a variable lets you see what data your program is storing and using at any given time. That way if your program is supposed to print a name but actually prints that person's phone number, you can step through your program line by line, and watch to see which line stores the wrong data for the program to print.

Not only can you "watch" how your program stores data, but a debugger also lets you change data while your program is running. By changing data, you can see how your program responds.

The step over command lets you skip or "step over" the lines stored in a subprogram.

Figure I.4-7. The step over command lets you skip or "step over" the lines stored in a subprogram.

For example, suppose a program asks the user to type in his age. If you run your program and type in a valid age, such as 15, you can see how your program handles the number 15. But what happens if the user types in an invalid number, such as 0 or -17?

A good programmer always makes sure the program doesn't accept invalid data, but suppose part of your program changes the age data by mistake. Depending on the age, sometimes the program changes the age to 0, sometimes it changes it to a negative number (-12), sometimes it changes it to an outrageously large number (486).

To find out how and why your program seems to randomly change the age, you can step through your program and watch the number stored as the age. By changing your variable while the program is running, you can type in different age values to see how your program responds.

By doing this, you can suddenly see that any time the age is less than 5, your program turns the age into 0; any time the age is greater than 5 but less than 45, the program turns the age into a negative number; and any time the age is greater than 45, the program turns the age into a huge number, like 486.

Without the ability to change the value of the age variable while the program is running, debugging a program is much slower and more tedious. By changing the value of the age variable while the program is running, you can test different values without having to trace through the program multiple times using different values. Just run the program once and change the value of the age variable as many times as you want, as shown in Figure 4-8.

Watching and changing variables can show you how a program reacts to different data.

Figure I.4-8. Watching and changing variables can show you how a program reacts to different data.

Saving Time with Third-Party Components

Programmers are naturally lazy and often look for the simplest way to solve any problem. So when faced with creating a program, programmers prefer to cheat by using third-party components, which are programs that somebody else has created already (and hopefully tested).

Third-party components usually offer commonly needed features, such as a word processor, a spreadsheet, or a database, that you can plug into your own program. So instead of having to write your own word processor, you can buy a word processor component and plug it into your own program; now your program has word processing capabilities with little additional programming on your part.

Warning

Third-party components can give your program instant features, but they can also give you instant problems, too. If the third-party component doesn't work right, your program won't work right either and you can't fix the problem until the company that sells the third-party component fixes the problem. Basically, third-party components put you at the mercy of another company. If that other company stops updating and improving their component, you're stuck with an outdated and possibly buggy component.

Depending on the features, third-party components can range in cost from a few hundred dollars to a few thousand dollars or more. Most third-party components aren't cheap, but because they can save you a lot of time, they may be worth the price.

Optimizing a Program with a Profiler

Not all parts of a program are equal. Some parts of a program may only run once or twice, whereas other parts of a program may run hundreds or even thousands of times. For example, suppose you have a program that stores names and addresses. To use this program, you must first type in your name and password before you can sort and search through the program's list of names and addresses.

In this example, the part of the program that asks for a username and password runs only once, but the part of the program that searches and sorts through your list of names and addresses may run multiple times. So which part of your program determines its speed? The part that runs once (asking for a username and password) or the part that runs multiple times (searching and sorting names and addresses)?

Obviously, if you want to speed up your program, you'd focus on the part that runs most often, and that's what a profiler does. A profiler examines your program and identifies the most frequently used parts of that program. After you know which parts of your program run most often, you can work on making that part of the program run faster, a process known as optimizing.

Profilers typically use two methods for examining a program:

  • Sampling: This examines your entire program at fixed time intervals. Through sampling, a profiler can get a rough estimate on which parts of your program (often referred to as hot spots) are being run most often.

  • Instrumentation mode: After you use sampling to identify hot spots, you can use this mode to examine the program in more detail to determine exactly what each program line does.

    By identifying the hot spots, you can speed up your entire program by rewriting or optimizing those frequently run parts of the program.

    Tip

    By optimizing a small part of your program, such as ten percent of that program, you can often improve the entire program's performance dramatically.

Creating a Help File

Hardly anyone reads software manuals, so when people need help, they typically turn to the program's help file for answers. This help file is essentially the software manual organized as miniature Web pages that you can view and click to see similar (linked) information, as shown in Figure 4-9.

Almost every program has a help file, but creating a help file can be tedious and time-consuming. So to simplify this process, many programmers use special help file creation programs.

Just as a word processor makes it easy to write, edit, and format text, help file creators make it easy to write, edit, organize, and link text together to create a help file.

Help files provide instructions for using a program.

Figure I.4-9. Help files provide instructions for using a program.

Installing a Program

Before anyone can use your program, they need to install it. Because programs can consist of dozens of separate files, most programs use a special installation program that makes sure all necessary files are copied on to a user's computer.

To use an installation program, identify all the files that make up your program and where to store them — either in a single location (folder) or scattered in multiple locations (folders).

Although the main purpose of an installer is to make sure all program files get copied to the user's hard disk, installers often give you the option of displaying graphics or playing music while your program installs on the user's computer. You can specify which graphic and audio files to display and play as well as how long to display and play them, as shown in Figure 4-10.

An installer lets you define which files to copy and where to store them on a hard disk.

Figure I.4-10. An installer lets you define which files to copy and where to store them on a hard disk.

Dissecting Programs with a Disassembler

A disassembler acts like a reverse compiler. A compiler converts your program (written in any programming language, such as C++ or Pascal) into machine language, a disassembler takes an executable file (which contains machine language code) and converts it into assembly language.

Note

Disassemblers can't convert machine language back into its original source code language (such as C++) because disassemblers can't tell which programming language was originally used. An executable file created from a C++ program looks no different than an executable file created from a Pascal or BASIC program. Therefore, disassemblers simply convert machine language into assembly language.

Disassemblers have both honorable and shady uses. On the honorable side, antivirus companies use disassemblers to dissect how the latest viruses, worms, and Trojan Horses work. After they know how these nasty programs work, they can figure out how to detect, stop, and remove these malicious programs.

On the shady side, many companies use disassemblers to tear apart their rival's products and see how they work. After you know how a competitor's program works, you can copy those features and use them in your own program.

Programming languages, such as Java, C#, and Visual Basic.NET, get compiled into bytecode format; therefore, a disassembler can reverse compile a byte-code file into its original source code. So, if you compile a Java program into bytecode format, a Java disassembler can convert the bytecode file into Java source code. Likewise, if you compile a C# or Visual Basic.NET program, you can disassemble that bytecode file into its original C# or Visual Basic.NET source code.

To protect their bytecode files from disassemblers, programmers use another program — an obfuscator. An obfuscator essentially scrambles a bytecode file. The bytecode file can still run, but if others try to disassemble an obfuscated bytecode file, they can't retrieve the original source code.

Tip

If you use a programming language that compiles into bytecode (such as Java, C#, or Visual Basic.NET), consider using an obfuscator to protect your source code from prying eyes.

At the bare minimum, all you need is an editor (to write programs) and a compiler (to convert your programs into executable files). However, most programmers use a debugger, a help file creation program, and an installer. Although most programmers are happy when they can get their program to work, some programmers use a profiler to help them speed up and optimize their program.

Finally, some programmers use disassemblers to peek inside the inner workings of other programs, such as viruses or rival software. Disassemblers are never necessary for creating a program, but they can prove useful for legal and not so legal purposes.

The tools of a programmer are highly subjective. Some programmers swear by certain tools, such as their favorite editor or compiler, whereas others are happily with whatever tool is available. Just remember that programmer tools can help you write faster and more reliable programs, but the best tool in the world can never substitute for decent programming skill in the first place.

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

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