0% found this document useful (0 votes)
35 views

ch03 (1)

The document discusses the importance of style, formatting, and comments in programming. It provides examples of good commenting practices, such as including a header with author, date, and purpose on all programs. It emphasizes writing code that is well-structured, indented, and spaced with comments to explain sections and purpose. Programs should be written like technical papers with paragraphs and shorter code blocks for readability.

Uploaded by

Samantha Morin
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

ch03 (1)

The document discusses the importance of style, formatting, and comments in programming. It provides examples of good commenting practices, such as including a header with author, date, and purpose on all programs. It emphasizes writing code that is well-structured, indented, and spaced with comments to explain sections and purpose. Programs should be written like technical papers with paragraphs and shorter code blocks for readability.

Uploaded by

Samantha Morin
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter -3

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 average number of lines of code in a typical application


has skyrocketed from 23,000 in 1980 to 1.2 million in 1990,
according to a recent survey of managers attending the 1990
Annual Meeting and Conference of the Software Maintenance
Association.

What’s worse, 74% of the managers surveyed at the 1990


Annual Meeting and Conference of the Software Maintenance
Association reported that they “have systems in their
department that have to be maintained by specific individuals
because no one else understands them.”

— Software Maintenance News, February 1991


A program serves two masters.
• Code tells the computer what to do.
• Comments describe what the program does to the poor
programmer who has to maintain it.

There are two types of comments in C++.

// Comments that begin with double-slash


// and go to the end of line

/* Comments that start with slash/star */


/*and go to star/slash */

/*
 * The second version can be used
 * for multi-line comments
 */
#include <iostream>
int main()
{
    std::cout << "Hello World\n";
    return 0;
}

What’s missing from this program?


/*
 hello -- program to print out "Hello World".         
  Not an especially earth-shattering program.
 Author: Mrs. Morin   
 Date: Oct 22, 2011                           
 Purpose: Demonstration of a simple program         
*/

#include <iostream> // include library

int main(){ //starts the program

    std::cout << "Hello World\n"; // outputs the message hello


    return 0; //ends the program
}

The following must be included in your comment
header EVERY time your write a program


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.
 */

/* A simple comment explaining the next line */


Cleverand very compact.
while ('\n' != *p++ = *q++);
What does this do? It takes up very little space, but don’t
save space, save the sanity of the people that follow you.

Simple and somewhat easier to understand.


while (1) {
    *destination_ptr = *source_ptr;

    ++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 ({}).

Some common styles are: The short form

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.

Example of a program with no “paragraphs.”

// 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.

• Avoid complex logic like multiple-nested if’s.

• 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.

 They are many other similar characters and


as a whole they are called: Escape Sequences.
 Escape Sequences are necessary normally
because the original character already has a
specific use or function.
 For example “ already means something but

what if we need to display quotation marks in


our message.
 The most basic command in C++ is to
output messages to the user. You must be
clear and communicate effectively anyone
who knows NOTHING about programming
should be able to understand your
messages/questions.

 cout << “Hello friend”;


 If you want to add to the same cout line use

another set of <<


 cout << “Hello friend” << “I’m happy”;
 Watch your spacing this line is not effective

because the d and I will be touching in your


output.
 cout << “Hello friend ” << “I’m happy”;
 Notice the spaces
 cout << “Hello friend” << “ I’m happy”;
 Notice the newline
 cout << “Hello friend\n” << “I’m happy”;
 Always add a \n to your last output message
so it does not show up next to the please any
key message.
 \t can also be an effective way to organize

your output.
 Lesson 2 - C++ Tutorials  Comments, indentation
and spacing
 QUESTIONS

  

 1. Write a program that outputs your name and

the date.  Include comments for each line of code.


  

 2. Make a problem to draw a simple animal face

using ascii
  

You might also like