ch-02 programming style
TRANSCRIPT
Programming style
Good Programming• Good programming style
Simple, easy-to-read program• Style is the most important part of
programmingLearn good programming style before typing first line of code will create the highest quality of code
Writing program vs Maintenance
• 74% of the managers surveyed at the 1990 Annual Meeting and Conference of Software Maintenance Association reported that :“they have system in their department that have to be maintained by specific individuals because no one else understand them”
Compact instruction = Problem
• Programming believe that the purpose of a program is only to present the computer with a compact set of instructionTHIS IS WRONG!
• Problems emerge :• Program difficult to correct, sometimes author
does not understand them• Modification and upgrades are difficult to make,
maintenance programmer must spend a considerable amount of time figuring out what the program does from its code
Style• Comments• Naming Style• Indentation• Clarity• Simplicity• Consistency
COMMENT
Comments• Two purposes of a program :
– Present the computer with a set of instruction– Provide the programmer with a clear, easy-to-read
description of what the program does• Program contains a glaring error :
#include <iostream.h>int main(){
Printf(“Hello World\n”);return 0;}
• The program contains no comments
Uncommented Program• It is an error that many programmers still
make and one that causes more trouble than any other problem
• A working but uncommented program is a time bomb waiting to explode
• Sooner or later someone will have to modify or upgrade the program, and the lack of comment will make the job ten times more difficult
A well-commented, simple program is a work of art
Flavors of comments• Start with /* and ends with */
/* This is a single-line comment. *//* * This is a multiline comment*/
• Begin with // and goes to the end of the line– // This is another form of comment.– // The // must begin each line that is to be comment.
Comment your program!/********************************************************************* hello –program to print out “Hello World”. ** Not an especially earth-shattering program. ** ** Author : John Smith ** ** Purpose : Demonstration of a simple program ** ** Usage : Run the program and the message appears **********************************************************************#include <iostream.h>int main(){
//tell the world helloprintf(“Hello World\n”);return 0;
}
NAMING STYLE
Naming Style• Names can contain both uppercase and
lowercase letters• Classic convention :
– Use all lowercase names for variables• e.g. source_ptr, current_index
– All uppercase is reserved for constants• e.g. MAX_ITEMS, SCREEN_WIDTH
• Newer program : mix-case names– e.g. RecordsInFile
• Choose any naming style as long as you are consistent
INDENTATION
Indentation• Indent program to make program
easier to understand• General rule for a C/C++ program :
– Indent one level for each new block or conditional
• The amount of indentation is left to the programmer– Two, four, and eight spaces are common– Choose any indent size as long as you
are consistent
Style of Indentation (1)while(!done) {
cout << “Processing\n”;next_entry();}if (total <= 0) {
cout<< “You owe nothing\n”;total = 0;
} else {cout<< “You owe “<< total << “dollars\n”;all_totals = all_totals + total;
}Most of the curly braces are put on the
same line as the statements
Style of Indentation (2)while(!done) {cout << “Processing\n”;next_entry();}if (total <= 0) {cout << “You owe nothing\n”;total = 0;} else {cout << “You owe “<< total << “dollars\n”;all_totals = all_totals + total;}
Curly braces are put on lines by themselves
CLARITY
Clarity• Program should read like a technical
paper– Should be organized into sections and
paragraphs– Procedure form a natural section
boundary• Organized your code into
paragraph– Good idea : begin a paragraph with a
topic sentence comment and separate it from other paragraphs by a blank line
Clarity, Example ://poor programmingtemp = box_x1;box_x1 = box_x2;box_x2 = temp;temp = box_y1;box_y1 = box_y2;box_y2 = temp;
better version/** Swap the two corner*//* 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;
SIMPLICITY
General rule of thumb (1)
• A single function should not be longer than one or two pages– If it gest longer, it can probably be split into two
simpler functions– Human mind can hold only so much in short-
term memory• Avoid complex logic
– The more complex your code, the more indentation level you will need
– Splitting your code into multiple procedures and thus decreasing the level of complexity
General rule of thumb (2)
• Avoid long statements– If an equation formula looks like it is
going to be longer than one or two lines, split it into two shorter equations
• Split large single code files into multiple smaller ones(modular programming)– Keep files smaller than 1500 lines– Easy to edit and print
General rule of thumb (3)
• Make program as simple and easy to understand as possible, even if it means breaking some of the rule– The goal is clarity– It is possible to create program with a
single statement that spanned more than 20 pages, because of specialized nature of the program, this statement more simple and easy to understand
CONSISTENCY
Consistency• Consistency is also factor in creating high-
quality program
• Books usually is organized with the table of contents at the front and the index at the back– Almost every book printed has a similar
organization– This consistency makes it easy to look up word
in the index or find a chapter title in the table of contents
• Good style is nice, but consistency is better
Contoh:
Summary• A program should be concise and easy to
read• A program must serve as a set of computer
instructions, but also as a reference work describing the algorithms and data used inside it
• Everything should be documented with comments
• Comments serve two purpose :– Describe your program to any maintenance
programmer who has to fix it– Comment help you remember what you did