Lecture 7 program development issues (supplementary)


Published on

Published in: Technology, Education
  • Be the first to comment

Lecture 7 program development issues (supplementary)

  1. 1. 1 of 31Module 7 : Algorithms and Program DevelopmentIntroduction to       Computational ThinkingModule 7 :Program Development Issues(supplementary material)Asst Prof Chi‐Wing FU, PhilipOffice: N4‐02c‐104email: cwfu[at]ntu.edu.sg
  2. 2. 2 of 31Module 7 : Algorithms and Program DevelopmentTopics• Basic Rules in writing programs• Program Readability• More on Problem Solving• Planning for testing, verification, anddocumentation
  3. 3. 3 of 31Module 7 : Algorithms and Program DevelopmentA Good ProgramWhat makes a good program?• A program is a reflection of the writer andtheir thoughts• First, you must have some thoughts(computational thinking)!• The difficulty for most people is to figure outwhat has to be done, the problem solving,before writing a program
  4. 4. 4 of 31Module 7 : Algorithms and Program DevelopmentThree RulesRule 1: Think before you program* Understand & Analyze the problemRule 2: A program is a human-readable essayon problem solving that also happens to beexecutable on a computer* A program -- Not just for computer to run,but also for human to readRule 3: Practice! Practice! Practice!The best way to improve your programmingand problem solving skills is to practice
  5. 5. 5 of 31Module 7 : Algorithms and Program DevelopmentTopics• Basic Rules in writing programs• Program Readability• More on Problem Solving• Planning for testing, verification, anddocumentation
  6. 6. 6 of 31Module 7 : Algorithms and Program DevelopmentProgram Readability• We will emphasize, over and over, that aprogram is not just for computers to read, butalso intended to be read by other people,even if “other people” is you in the future!• Write a program so that you can read it,because sometime in the future you mayhave to read it again…• So… Any guideline?
  7. 7. 7 of 31Module 7 : Algorithms and Program DevelopmentReadability(1): Naming• The easiest thing to do that affectsreadability is good naming• Use meaningful names for the itemsyou create that reflect their purpose• To help keep straight the types used,include that as part of the name.Python does not care about the typestored, but you do!
  8. 8. 8 of 31Module 7 : Algorithms and Program DevelopmentWhat does this do?• What is a? What is b?
  9. 9. 9 of 31Module 7 : Algorithms and Program DevelopmentHow about this?• More specific names on variable• Hungarian notation: append types
  10. 10. 10 of 31Module 7 : Algorithms and Program DevelopmentReadability(2): SpaceMore space in the program helps reading also(my personal habit: align assignment op.)
  11. 11. 11 of 31Module 7 : Algorithms and Program DevelopmentReadability(3): Comments• info at the top, the goal of the code• purpose of variables (if not obvious bythe name)• purpose of other functions being used• anything “tricky”? If a piece of code tookyou some time to write, it is probably hardto read and demands for a comment
  12. 12. 12 of 31Module 7 : Algorithms and Program DevelopmentReadability(3): CommentsVisual example in module 6.2Helps understanding when programis not short or not straightforward
  13. 13. 13 of 31Module 7 : Algorithms and Program DevelopmentReadability(4): Indentation• A visual cue to say what code is“part of” other code• This is not always required in manyprogramming languages (more freedom),but Python forces you to indent properly• This aids readability greatly
  14. 14. 14 of 31Module 7 : Algorithms and Program DevelopmentTopics• Basic Rules in writing programs• Program Readability• More on Problem Solving• Planning for testing, verification, anddocumentation
  15. 15. 15 of 31Module 7 : Algorithms and Program DevelopmentStrategies: Problem Solving• Engage/Commit• Visualize/See• Try it/Experiment• Simplify• Analyze/Think• Relax
  16. 16. 16 of 31Module 7 : Algorithms and Program DevelopmentEngage• You need to commit yourself toaddressing the problem.• Don’t give up easily• Try different approaches• Set the “mood”• Just putting in time does not mean youput in a real effort!!!
  17. 17. 17 of 31Module 7 : Algorithms and Program DevelopmentVisualize/See the Problem• Find a way that works for you,some way to make the problem tangible.• Draw pictures• Layout tables• Literally “see” the problem somehow• Everyone has a different way, find yours!
  18. 18. 18 of 31Module 7 : Algorithms and Program DevelopmentTry It/Experiment• For some reason, people are afraid to just“try” some solutions. Perhaps they fearfailure, but experiments, done just for you,are the best way to figure out problems.• Be willing to try, and fail, to solve aproblem. Get started, don’t wait forenlightenment!
  19. 19. 19 of 31Module 7 : Algorithms and Program DevelopmentSimplify• Simplifying the problem so you can get ahandle on it is one of the most powerfulproblem solving tools.• Given a hard problem, make is simplier(smaller, clearer, easier), figure that out,then ramp up to the harder problem.
  20. 20. 20 of 31Module 7 : Algorithms and Program DevelopmentThink it Over/Analyze• If your solution isn’t working:• Stop• Evaluate how you are doing• Analyze and keep going, or start over.• People can be amazingly “stiff”, bangingtheir heads against the same wall overand over again. Loosen up, find anotherway!
  21. 21. 21 of 31Module 7 : Algorithms and Program DevelopmentOne More Thing: Relax• Take your time. Not getting an answerright away is not the end of the world. Putit away and come back to it.• You’d be surprised how easy it is to solveif you let it go for awhile. That’s whystarting early is a luxury you should affordyourself.
  22. 22. 22 of 31Module 7 : Algorithms and Program DevelopmentExample: Cryparithmetic ProblemE L F+ E L FF O O LE= ?F = ?L = ?O = ?Remember:• Engage• Visualize• Try it• Simplify• Analyze• Relax
  23. 23. 23 of 31Module 7 : Algorithms and Program DevelopmentTopics• Basic Rules in writing programs• Program Readability• More on Problem Solving• Planning for testing, verification, anddocumentation
  24. 24. 24 of 31Module 7 : Algorithms and Program DevelopmentProgram Development1. Problem Specificatione.g., identify requirements & goals2. Problem Analysis (how to solve it)e.g., identify input & output, formulae3. Program Design- write solution stepse.g., pseudo code and flowcharts4. Implementation- translate your solution into program- build first program skeleton/outlinee.g., comments on major steps5. Program Testing- use test samples to try your programProblemSpecificationProblem AnalysisProgram DesignImplementationProgram TestingIt can be aniterative process!!!
  25. 25. 25 of 31Module 7 : Algorithms and Program DevelopmentProgram Testing• A program is correct if it returns the correct result forevery possible combination of input values.Exhaustive testing: use all possible combinations ofinput values and check the output is correct. This willtake a whole year, or forever, to show the program iscorrect.-> Impractical!!!• What we can do: 1) use test data that causes everyprogram path (e.g., in branching and looping) to beexecuted at least once; and 2) think and be creative!!!
  26. 26. 26 of 31Module 7 : Algorithms and Program DevelopmentProgramming Errors• Syntax Errors• “grammatical” errors• detected by compiler and found automatically• need to be fixed before the compiler can understand thecode• E.g., missing colon before a block in while or for loops• Runtime Errors• execution error (e.g., divide by zero, program crash, etc.)• detected during the execution of code• error messages may be useful to help identify thereasons and locations• sometimes not easy to fix
  27. 27. 27 of 31Module 7 : Algorithms and Program DevelopmentProgramming Errors• Logical Errors• due to error in designing the algorithm orimplementation• no compilation errors; no run-time error message• most difficult to detect• e.g., in a role-playing game, you killed a monsterbut experience point is not given to you (but notsupposed to!!!)… note: you can still play thegame… no crash! But no level up!
  28. 28. 28 of 31Module 7 : Algorithms and Program DevelopmentDebugging• The process of finding and correctingerrors, especially logic errors (BUG!!!)• Strategies:• Hand Tracing or Simulation• Program Tracing• Use print() function at appropriate programlocations to check:• Program control flow• Values of the variables• Try different user input• Be Patient! Don’t give up!!!
  29. 29. 29 of 31Module 7 : Algorithms and Program DevelopmentDocumentation• Documentation is needed for further modificationand maintenance• Proper documentation includes:• problem definition and specification;• program inputs, outputs, constraints and mathematicalequations;• algorithms and logic, e.g., flowchart, pseudo code, etc.• source code with appropriate comments;• sample test run of the program; and• user manual for end users (how to use it)• It should be done alongside with the programdevelopment but not the very end!!!
  30. 30. 30 of 31Module 7 : Algorithms and Program DevelopmentTake Home MessagesBasic Rules:- Think before you program- A program is a human-readable essay on problem solving- Practice! Practice!! Practice!!!Program Readability: Naming; Space; Comments; IndentationProblem Solving Strategies:- Engage/Commit; Visualize/See; Try it/Experiment; Simplify;Analyze/Think; RelaxProgram Testing- need to design test data to try every program path- unit test: test your code piece by piece on their correctness!Programming Errors- Syntax Errors; Runtime Errors; Logical ErrorsOther issues: Debugging and Documentation
  31. 31. 31 of 31Module 7 : Algorithms and Program DevelopmentReading Assignment• TextbookChapter 3: Algorithms and Program Development3.1 to 3.5Note: Though some material in textbook is notdirectly related to the lecture material, you canlearn more from them.