Freeform Style

C++'s freeform style lets you insert spacing and blank lines throughout your code to help make the program more readable. 


Whitespace consists of the blank lines and indentations you add to code.

Most of the time, you can put lots of spacing in a Visual C++ program and C++ will not complain. You can put whitespace between symbols and the words that make up the C++ language (but you can't split up words with spaces). C++ programmers often put extra spaces and blank lines in programs to make the programs more readable. With whitespace, C++ programmers make C++ programs more readable to people, not to the Visual C++ compiler. 

To your Visual C++ compiler, the following program is exactly the same program as the previous one you saw: 

//Filename:CFIRST.CPP//Program displays a message on-screen  #include   void main(){cout<<"I will be a C++ expert!";}


Freeform means that C++ lets you insert as many spaces and lines as you want.

Which is easier for you to read, the first or the second version of the program? Obviously, the first version is. Visual C++ is called a freeform compiler. You can indent lines of the program, or leave all the lines flush left. 

Because your computer is a machine, it does not require extra whitespace to understand a program. As long as you follow all the coding rules of Visual C++, the compiler will be happy with the code you supply. In spite of the Visual C++ compiler's lack of concern for how nice a program looks, you should be concerned about the program's look. Add extra whitespace to group similar lines of code together and make the program easier to understand for people who read the program. 

As you see other programs throughout this book, you will begin to pick up some C++ whitespace conventions and develop some of your own. 

Programs Always Change 

While you write Visual C++ programs, consider that someday you might have to change those programs or somebody you work with will have to. You could squeeze as much space out of a program as possible, but you will gain nothing from doing so. (You might save a few characters of computer memory, but not enough to make up for a messy program.) 

If you add extra whitespace to make the program more readable to people, the program will be easy to modify in the future. In this ever-changing world, programs have to be modified to reflect those changes, and the person who writes more readable code gets hired for programming jobs faster than one who does not care about program readability. Updating and changing programs is called maintaining programs. A maintainable program is a readable program.

If you are confused now, you are right on track! You still do not have to understand any specifics about the two program listings seen so far. This unit is getting you used to the look and feel of Visual C++ programs, not their particulars. If you understand that Visual C++ is picky about the characters you type, and if you realize that a program should be readable to people, you deserve an A+ for the unit so far. 

The C++ freeform language allows for as much whitespace as you want to add. 

No comments: