StyleGuide - Appendix A C+ Programming Style Guide Good...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
StyleGuide 107 Appendix A C++ Programming Style Guide Good style is like touch typing: it may seem counter-productive at first, but the initial effort will pay enormous dividends. After a little while, the elements of good style will be second nature. Good style will help you to debug your programs more easily, and also make them more easily readable by your instructor and the grader. A.1 Readability There are a number of stylistic elements which can make a program more readable, including the use of horizontal and vertical spacing, the conventions used in declarations, etc. Each of these elements will be discussed in turn. Keep in mind that once a program is written, it is seldom read from top to bottom. While debugging or modifying a program, programmers often skip large blocks of text in order to find what they are looking for. A good analogy can be made to a dictionary. Imagine if the words in a dictionary were written in normal English style, as is this article. What if the dictionary were not alphabetized? What if the words being defined did not appear in boldface? As a good programmer you should strive to enhance the visual appearance of the code you write. The effort you put in will begin to pay dividends as you debug your code. A.1.1 Indentation Indentation is used to enable a reader to determine the nesting level of a statement at a glance. In order to be useful, indentation must be consistent - the number of spaces used per indentation level should be between 3 and 5 - and the same style of indentation should be used throughout the program. Proper indentation makes your program much easier to debug. A.1.2 Spaces Normally in programming the standard for the use of spaces is that you follow normal English rules. This means that:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
StyleGuide 108 1. Most basic symbols in C++ (e.g., “=”, “+”, etc.) should have at least one space before and one space after them, with the following notable exceptions: No space appears before a comma or a semicolon. No space appears before or after a period. No space appears between unary operators and their operands (e.g. "++"). for (counter = 0; counter < length; counter++) { friendFile « addressBook[counter].firstName « ' ' « addressBook[counter].lastName « endl; friendFile « '(' « addressBook[counter].areaCode « “)” « addressBook[counter].phoneNumber « endl; friendFile « setw(2) « addressBook[counter].month « '/' « setw(2) « addressBook[counter].day « '/' « setw(4) « addressBook[counter].year « endl; friendFile « endl; } // for Figure A.1: An example of good spacing practices. 2. More than one space may be used if you are aligning things on adjacent lines, as is done with the "«"in Figure A.1. A.1.3 Blank Lines
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 03/09/2012.

Page1 / 16

StyleGuide - Appendix A C+ Programming Style Guide Good...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online