Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.

Similar presentations


Presentation on theme: "Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007."— Presentation transcript:

1 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007

2 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 2 4.1 Design a Structured Program

3 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 3 Seldom do we have a program worked the first time it is written Need debugging  To find out and correct the errors in a program May need many times of iteration for a large program To reduce the number of iteration, need a structural method for program development.

4 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 4 Debugging a big program is exponentially more difficult than a small program. If a big program is to be developed, always try to break it down to many small programs and debug them independently.  Divide and Conquer Difficulty in debugging Program size

5 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 5 int main() { : } int main() { : } int main() { : } int function1() { : } int function2() { : } int main() { : } int function1() { : } int function2() { : } It is a very bad habit to write a program in main() only  since it makes the program very big. Always break it down by calling functions from main(). 

6 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 6 Usually main() is only used to indicate the program flow. The actual task is done by the functions called by main(). By looking at the sequence of functions called by main(), people basically understand what is going on in a program. It gives the skeleton of a program.

7 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 7 #include using namespace std; bool menu(); //function prototype int main() {bool exitflag = false; //flag is bool while (exitflag == false) {exitflag = menu(); } return 0; } In the example program below, people expect this program will repeatedly show a menu. If interested in the details of the menu, he can further read the function menu().

8 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 8 Improve the Readability of Programs In practice, the time a programmer spends in documentation is more than writing the program. A real program is never developed by a single person, co-operations are needed between programmers or even teams of programmers. Besides, a program developed by a team of programmers often needs to be maintained by another team. It is essential that a program is properly documented to allow team work.

9 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 9 Documenting a Program - readme After developing a program, a readme file is often prepared in practice to indicate the following: Background information of the program (e.g. objectives, version no., development date, developer name) How to use the program (e.g. command line options) Additional resource required (e.g. hardware, driver, etc.) Bug fixed as compared to previous version(s) Possible conflict with other programs.

10 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 10 Within a program - comments Commenting a program is the responsibility of every programmer. Need meaningful comments.  Not explaining something people know already, but something people will have difficulty to understand.

11 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 11 // The main function int main() { cout << "Hello World!\n"; // cout Hello World return 0; // return a number 0 } Totally meaningless comment! For example

12 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 12 // It is to demonstrate the basic structure of a C++ // program // Usage: HelloWorld int main() { cout << "Hello World!\n"; // print the string Hello World! on screen return 0; // Indicate to the system the execution is // successful } Tell something hidden in the program! See the lines for comments are more than the codes

13 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 13 How to comment? At the beginning of each program, the following comments are often required: Background information of the program (Objectives, version no., development date, developer name, etc.) Usage (e.g. command line options) Additional resource required (e.g. if any other projects/files are required for compiling this program, e.g. header files)

14 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 14 How to comment? At the beginning of each function, the following information is needed Objective of this function Other functions that it will call Passed parameters Return parameters Global variables that have been made use of.

15 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 15 How to comment? Inside a function, the following information is needed The use of every local variable Explanation of any tricky part that other people may have difficulty in understanding.

16 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 16 Case study – Developing a program step-by-step Write a program that repeatedly asks the user to select one of the following three options: 1. On choosing ‘ a ’, ask the user to input a series of positive numbers and show the mean of them; 2. On choosing ‘ b ’, ask the user to input a series of positive numbers and show the variance of them; 3.On choosing ‘ q ’, quit the program.

17 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 17 Step 0 – Prepare a flowchart Start Yes No A Ask user to choose a, b or q To better visualize the problem, we may develop a flowchart for it Yes No D Yes C B End User chooses a ? User chooses b ? User chooses q ? Skeleton Menu

18 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 18 A D C B Input integers and calculate the mean Input integers and calculate the variance Generate error message

19 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 19 // This program is to compute the mean or variance // of a series of positive numbers input by user // Usage: Mean_Var // Version: 1 // Date: Feb 14, 2006 // Author: Frank #include using namespace std; bool menu();// Show a menu to user and ask for input int main() {bool exitflag = false; // A flag to indicate if user wants to quit while (exitflag == false) {exitflag = menu(); // If user chooses 'q', menu() returns true } return 0; } Step 1 – construct the skeleton Flow Chart

20 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 20 Step 2 – Add stubs (i.e. just for testing) The above program cannot be compiled and executed since the required function menu() has not be implemented We need to ensure the skeleton is correct before we proceed to implement the functions – add stubs // A stub // Input parameter: Nil // Return parameter: Just a constant bool menu() { return true; //Loop 1 time } Just enough for the program skeleton to compile and execute

21 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 21 // This program is to compute the mean and variance // of a series of positive numbers input by user // : #include using namespace std; bool menu();// Show a menu to user and ask for input int main() {: : return 0; } // A stub // Input parameter: Nil // Return parameter: Just a constant bool menu() {return true; } So the whole program at this stage is just like that

22 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 22 Step 3 – Implement Functions If the skeleton has been proved to be correct, start to implement functions. The original comments may need to be updated when any change is made to the original codes as a result of the implementation of functions. Similar to developing main(), it is desirable to further break a big function into smaller functions. Add stubs again when needed.

23 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 23 // Show a menu to user and ask for input // Input: Nil // Return: true if user chooses 'q', otherwise false void average(char); // Ask for a series of numbers and // compute their mean or variance bool menu() { char choice; // To store the command of user cout<<"[a] Mean\n"<<"[b] Variance\n"<<"[q] Quit\n"; cout << "Please enter your choice: "; cin >> choice; switch (choice) { case 'a': average('a'); break; case 'b': average('b'); break; case 'q': return true; // Shall quit menu() here default : cout << "Wrong input!\n"; break; } return false; } Flow Chart

24 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 24 // A stub // Input parameter: char command – // if 'a', do mean; if 'b', do variance // Return parameter: Nil void average(char command) { } menu() cannot be compiled or executed since average() has not been implemented To ensure menu() is correct, we need to compile and execute it. To solve the problem, a stub is added for average():

25 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 25 Step 4 – Implement the sub-functions If the major functions have been proved to be correct, start to implement sub-functions The original comments may need to be updated when any change is made to the original codes as a result of the implementation of functions. Similar to developing menu(), it is desirable to further break a big function into smaller functions. Add stubs again when needed. It is desirable to have each function contained at most 20 lines of codes. Divide and conquer average().

26 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 26 // Get positive nos., find mean or variance // Input parameter: char command - // 'a'-> mean; 'b'-> variance // Return parameter: Nil void average(char command) { double input = 0;// Store data input by user double sum = 0; // Store temporary sum int count = 0; // Count no. of data entered while (input >= 0) // If negative no., quit {cout << "Please enter your data: "; cin >> input; if (input < 0) continue; // Leave average() on -ve input count+=1; if (command == 'a') // calculate and show the current mean { sum = sum + input; cout<<"\n\nThe current mean is "<<sum/count<<endl; } else // calculate and show the current variance { sum = sum + input*input; cout<<"\n\nThe current variance is "<<sum/count<<endl; } // it is in fact not the real variance } }

27 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 27 Exercise 4.1 By following the steps as described above, you are requested to develop the skeleton of a personnel database program. The program should be a console application that repeatedly asks users to do one of the following in the main menu: a. Enter/modify record b. Show all records q. Quit

28 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 28 For every user command input, call a function. You don’t need to implement the details of the functions, just do the following: If user chooses ‘ a ’, show a message “ Please enter record: ” call a function and get back to the main menu. If user chooses ‘ b ’, show a message “ All records are shown below ” call a function and get back to the main menu. If user chooses ‘ q ’, show a message “ Thank you. Goodbye!!! ” and then the program will quit. Make sure your program follows the style as shown in the case study (Do NOT use main() to do everything) Make sure your program is commented appropriately using the method as described above.

29 Computer Programming and Basic Software Engineering 4. Basic Software Engineering 29 Acknowledgment The slides are based on the set developed by Dr. Frank Leung (EIE).


Download ppt "Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007."

Similar presentations


Ads by Google