1 1 Programming and!Program Style"The material for this lecture is drawn, in part, from!The Practice of Programming (Kernighan & Pike) Chapter 1!2 Goals of this Lecture"• Help you learn about:!• Good programming (verb) style!• Good program (noun) style!• Why?!• A well-styled program is easier to maintain and more likely to be correct than a poorly-styled program!• A power programmer knows the qualities of a well-styled program, and how to develop one!2 3 Lecture Overview"• Programming style: how to create a good program!• Top-down design!• Successive refinement!• Program style: qualities of a good program!• Well structured!• Uses common idioms!• Uses descriptive names!• Contains proper comments!• Modular!4 Part 1: Programming Style"3 5 Bottom-Up Design is Bad"• Bottom-up design !• Design one part in detail!• Design another part in detail!• Repeat until finished!• Bottom-up design in painting"• Paint upper left part of painting in complete detail!• Paint next part of painting in complete detail!• Repeat until finished!• Unlikely to produce a good painting!• Bottom-up design in programming"• Write first part of program in complete detail!• Write next part of program in complete detail!• Repeat until finished!• Unlikely to produce a good program!1! 2!… 1!2!3!4!… 6 Top-Down Design is Good"• Top-down design!• Design entire product with minimal detail!• Successively refine until finished!• Top-down design in painting"• Sketch the entire painting with minimal detail!• Successively refine the entire painting!• Top-down design in programming"• Define main() function in pseudocode with minimal detail!• Refine each pseudocode statement!• Small job => replace with real code!• Large job => replace with function call!• Recurse in (mostly) breadth-first order!• Bonus: Product is naturally modular!1!2! 3!4! 5!…4 7 Top-Down Design in Reality"• Top-down design in programming in reality"• Define main() function in pseudocode!• Refine each pseudocode statement!• Oops! Details reveal design error, so…!• Backtrack to refine existing (pseudo)code, and proceed!• Recurse in (mostly) breadth-first order, until all functions are defined!1!2! Oops!1ʼ!2ʼ! 3!1ʼ!2ʼ! 3!4! Oops!1ʼʼ!2ʼʼ! 3ʼ!4ʼ! 5!… 8 Example: Text Formatting"• Goals of the example!• Illustrate good programming style!• Especially function-level modularity and top-down design!• Illustrate how to go from problem statement to code!• Review and illustrate C constructs!• Text formatting (derived from King Section 15.3)!• Input: ASCII text, with arbitrary spaces and newlines!• Output: the same text, left and right justified!• Fit as many words as possible on each 50-character line!• Add even spacing between words to right justify the text!• No need to right justify the very last line!• Simplifying assumptions!• Word ends at white space or end-of-file!• No word is longer than 20 characters!5 9 Tune every heart and every voice. Bid every bank withdrawal. Let's all with our accounts rejoice. In funding Old Nassau. In funding Old Nassau we spend more money every year. Our banks shall give, while we shall live. We're funding Old Nassau. Tune every heart and every voice. Bid every bank withdrawal. Let's all with our accounts rejoice. In funding Old Nassau. In funding Old Nassau we spend more money every year. Our banks shall give, while we shall live. We're funding Old Nassau. I N P U T O U T P U T Example Input and Output"10 Thinking About the Problem"• I need a concept of “word”!• Sequence of characters with no white space!• All characters in a word must be printed on the same line!• I need to be able to read and print words!• Read characters from stdin till white space or EOF • Print characters to stdout followed by space(s) or newline!• I need to deal with poorly-formatted input!• I need to remove extra white space in input!• Unfortunately, I canʼt print the words as they are read!• I donʼt know # of spaces needed till I read the future words!• Need to buffer the words until I can safely print an entire line!• But, how much space should I add between words?!• Need at least one space between adjacent words on a line!• Can add extra spaces evenly to fill up an entire line!6 11 Writing the Program"• Key constructs!• Word!• Line!• Next steps!• Write pseudocode for main() • Successively refine!• Caveats concerning the following presentation!• Function comments and some blank lines are omitted because of space constraints!• Donʼt do that!!!!• Design sequence is idealized!• In reality, much backtracking would occur!12 The Top Level"int main(void) { <Clear line> for (;;) { <Read a word> if (<No more words>) { <Print line with no justification> return 0; } if (<Word doesn’t fit on this line>) { <Print line with justification> <Clear line> } <Add word to line> } return 0; } • First, letʼs sketch main()…!7 13 Reading a Word"• Now letʼs successively refine. What does <Read a word> mean? The job seems complicated enough that it should be delegated to a distinct function…!… enum {MAX_WORD_LEN = 20}; int main(void) { char word[MAX_WORD_LEN + 1]; int wordLen; <Clear line> for (;;) { wordLen = ReadWord(word); if (<No more words>) { <Print line with no justification> return 0; } if (<Word doesn’t fit on this line>) { <Print line with justification> <Clear line> } <Add word to line> } return 0; } int ReadWord(char *word) { <Skip over whitespace> <Store chars up to MAX_WORD_LEN in word> <Return length of word> } 14 Reading a Word (cont.)"• ReadWord() seems easy enough to design. So letʼs flesh it out…!int ReadWord(char *word) { int ch, pos = 0; /* Skip over white space. */ ch = getchar(); while ((ch != EOF) && isspace(ch)) ch = getchar(); /* Store chars up to MAX_WORD_LEN in word. */ while ((ch != EOF) && (!
View Full Document