ch03 (1)
ch03 (1)
Style
Style is the most important part of
programming.
Style is what separates the gems from the
junk.
It is what separates the programming artist
from the butcher.
The Mona Lisa and a paint-by-numbers
picture are both paintings.
What separates the two is Style.
Last year my grade 12 student’s final project broke 1000 lines of
code.
/*
* The second version can be used
* for multi-line comments
*/
#include <iostream>
int main()
{
std::cout << "Hello World\n";
return 0;
}
Heading
Author
Purpose
Date
Anything else that’s useful
/********************************************************
********************************************************
******** WARNING: This is an example of a *******
******** warning message that grabs the *******
******** attention of the program. *******
********************************************************
********************************************************/
/*------------> Another, less important warning<--------*/
/*>>>>>>>>>>>> Major section header <<<<<<<<<<<<<<<< */
/********************************************************
* We use boxed comments in this book to denote the *
* beginning of a section or program *
********************************************************/
/*------------------------------------------------------*\
* This is another way of drawing boxes *
\*------------------------------------------------------*/
/*
* This is the beginning of a section
* ^^^^ ^^ ^^^ ^^^^^^^^^ ^^ ^ ^^^^^^^
*
* In the paragraph that follows we explain what
* the section does and how it works.
*/
/*
* A medium level comment explaining the next
* dozen or so lines of code. Even though we don’t
have
* the bold typeface we can **emphasize** words.
*/
++destination_ptr;
++source_ptr;
if (*(destination_ptr-1) == '\n')
break; // exit the loop if done
}
Indentationis a religious issue. Many religious wars are waged
over where to put the curly braces ({}).
while (! done) {
std::cout << "Processing\n";
next_entry();
}
if (total <= 0) {
std::cout << "You owe nothing\n";
total = 0;
} else {
std::cout << "You owe " <<
total << " dollars\n";
all_totals = all_totals + total;
}
// another style for brackets
while (! done)
{
std::cout << "Processing\n";
next_entry();
}
if (total <= 0)
{
std::cout << "You owe nothing\n";
total = 0;
}
else
{
std::cout << "You owe $" << total << "\n";
all_totals = all_totals + total;
}
Programs should read like a technical paper. Break your code
into sentences, paragraphs, sections, chapters and books.
// poor programming practice
temp = box_x1;
box_x1 = box_x2;
box_x2 = temp;
temp = box_y1;
box_y1 = box_y2;
box_y2 = temp;
Same program with paragraphs added in.
/*
* Swap the two corners
*/
/* Swap X coordinate */
temp = box_x1;
box_x1 = box_x2;
box_x2 = temp;
/* Swap Y coordinate */
temp = box_y1;
box_y1 = box_y2;
box_y2 = temp;
• A single function should not be more than one or two pages long.
• About the time your code starts to run into the right margin, you probably
should consider splitting up into smaller, simpler units.
• Did you ever read a sentence, like this one, where the author went on and
on, stringing together sentence after sentence with the word “and” and didn’t
seem to understand the fact that several shorter sentences would do the job
much better and didn’t it bother you?
C++ statements should not go on forever. Split long statements into smaller,
simpler ones.
• Split large single code files into multiple smaller ones. Any file with more
than about 1,500 lines of code is hard to edit and even harder to understand.
Makeyour program as clear
and as simple as possible.
/* This program demonstrates the \n code,
which generates a new line.
*/
#include <iostream>
using namespace std;
int main()
{
cout << "one\n";
cout << "two\n";
cout << "three";
cout << "four";
return 0;
}
The newline character can be placed
anywhere in the string, not just at the end.
It must be placed within the quotation marks.
your output.
Lesson 2 - C++ Tutorials Comments, indentation
and spacing
QUESTIONS
using ascii