Matters of Style Topic 10 Chapter 5 Why have style Why have style Readability Reusability Modifiability Easier to debug You need to really read this chapter Follow up with me if you have questions Chapter 5 Matters of Style 2 Some style guidelines Name identifiers properly Variables lowercase Constants UPPERCASE Indent blocks of code int main indent here Chapter 5 Matters of Style 3 1 Commenting your code For all programs in this class Before int Main Use comments to describe your program Data Table The declaration section must contain a data table The data table states the use of the variable or named constant how its value is obtained used Other comments should be used throughout your code to Describe what each section is doing think in terms of input processing output Complicated parts of the code be descriptive 4 Data Tables Should state use of the identifier how it is used Comments should be lined up All identifiers should have their own line and datatype Which of these are correct int firstNum IN CALC first value to average int secondNum IN CALC second value to average float average CALC OUT average of two values CORRECT int firstNum INPUT first value to average int secondNum INPUT second value to average float average CALC OUT average of two values INCORRECT int firstNum input value Int secondNum input value float average calculated average INCORRECT 5 Chapter 5 Matters of Style AUTHOR Michele Rousseau ASSIGNMENT 1 Template CLASS CS1B SECTION MW 10 30a 12p DUE DATE 1 5 12 include iostream Pre processor using namespace std directives ADD TWO INTS This program accepts two integers in from a user sums them and then outputs the result to the monitor INPUTS inp1 First integer to be summed from user inp2 Second integer to be summed from user OUTPUT sum The sum of the two inputs to the screem int main constants include data table above for constants see the eclipse lab int inp1 IN CALC First integer to sum int inp2 IN CALC Second integer to sum int sum CALC OUT contains the result of the sum of two inputs Class Heading Create a Template Create a project Put all this in there Call it 0 template Cut paste the project General Program description Data Table OUTPUT class heading to the screen cout cout cout cout cout cout cout cout left n PROGRAMMED BY Michele Rousseau n STUDENT ID 750125 n CS1B MW 6p 7 30 n ASSIGNMENT 1 Template n n right INPUT A description of what is being input PROCESSING Detail what is being processed OUTPUT Details of what is being output return 0 Output Class Heading Doc throughout code 6 2 Class heading information First lines in your source file AUTHOR Michele Rousseau LAB 1 Template CLASS CS1A SECTION MW 10 30a 12p DUE DATE 1 5 12 Note the alignment Replace the data in purple with the appropriate data 7 Chapter 5 Matters of Style Next Preprocessor Directives then doc for the main program Including a list of inputs and outputs include iostream include iomanip using namespace std Program Title ADD MULTIPLY TWO INTS This program does whatever this program does General save this template and fill in the info appropriate Description for your program INPUTS int1 First integer to be summed from user int2 Second integer to be summed from user Describe the OUTPUTS Inputs Notice sum the sum of the two ages the product The product of the two integers Outputs here indentation 8 Chapter 5 Matters of Style Next int main int main Declare your constants here document constants above the declarations Double Declare variables here include your data table space Initialize variables OUTPUT your header and class information here see next slide INPUT A description of what is being input PROCESSING OUTPUT Detail what is being processed Details of what is being output return 0 Chapter 5 Matters of Style 9 3 Header Class Information OUTPUT class heading to screen cout left cout n cout PROGRAMMED BY Michele Rousseau n cout STUDENT ID 750125 n cout CS1A MW 6p 7 30 n put lab or Assignment as appropriate cout Lab 7 Lab Name n cout n cout right Change everything in purple to the appropriate information for the project For assignments put Assignment instead of Lab Or just you re the code from your eclipse lab 10 Chapter 5 Matters of Style It is easier if you show print margins Right click on the scroll bar to Expand these the left of the Editor window to get this menu Left click to check 1 Check show line numbers 2 Check Show print margins 3 Change Print margin column to 75 Change this to 75 Show Print Margin Left click to check Show line numbers 11 Chapter 5 Matters of Style Documenting executable code int main All programs have a data table Declare your constants here document constants above the declarations int num1 int doubledValue Space Between operators IN CALC first value to average CALC OUT integer to store the doubled value Document INPUT get numbers to average from user above each cout Enter first value to double code segment cin num1 Double space between code segments PROCESSING calculate the average double float num1 num2 2 Block of code is indented OUTPUT output the average cout n nThis integer doubled is double return 0 CORRECT 12 4 Initializing Variables DO NOT INITIALIZE VARIABLES IN THE DECLARATION SECTION Initialize variables just before their use in the program int count count 0 CORRECT int count 0 INCORRECT 13 Chapter 5 Matters of Style Flowcharting REVIEW If Then Statement T IF condition F If condition True instructions CORRECT X F T True instructions INCORRECT 14 Chapter 5 Matters of Style Flowchart for If Then Else Stmt F IF Condition False Instructions If Condition T True Instructions F X T True Instructions False Instructions X CORRECT Chapter 5 Matters of Style INCORRECT 15 5 Nesting If Then Else Stmt IF Condition F False Instructions T F T IF Condition False Instructions True Instructions CORRECT 16 Chapter 5 Matters of Style Nesting If Then Else Stmt IF Condition F T False Instructions F T If Condition False Instructions True Instructions X X INCORRECT 17 Chapter 5 Matters of Style For Loops FOR count 1 to 3 T Loop statements F FOR i 1 to x F X T Loop statements statement statement CORRECT Chapter 5 Matters of Style INCORRECT 18 6 While loops X While condition F T WHILE condition X F loop body statement T loop body While condition XF T statement CORRECT loop body statement INCORRECT 19 7
View Full Document
Unlocking...