ITM © Port, Kazman 1 ITM 352 How to Think Like a Programmer
ITM © Port, Kazman 2 Coding Tips and Tricks
ITM © Port, Kazman 3 Coding Advice r Know exactly what you want before starting to code r Have some idea of how to implement what you want before writing code r Start small and evolve gradually m Break down into basic parts, simplify, do one part, test, expand until satisfied r Write comment first explaining what is needed then write code to implement it r Do not write too much code before testing r Make a backup of your program after any substantial amount of effort r Backup only “working” versions, if program gets broken, revert to that last known working version
ITM © Port, Kazman 4 Defensive Programming r Always use meaningful variable and file names r Keep code neat, clean, and easy to read AS YOU GO r Follow code formatting conventions (e.g. indent under if and loop statements) r Anticipate errors! Always expect data may be bad or invalid – test to ensure data is valid when first used. r Add “debugging” statements (e.g. print out important variable values) – comment out when not needed r Think of small validation tests as you go and perform tests – make sure you get what you expect at every step
ITM © Port, Kazman 5 Follow Along Exercise Participation is MANDATORY – you are expected to ask questions and make suggestions!
ITM © Port, Kazman 6 Programming from Concept to Testing r Concept: write a program that prints a 2D “star field” *…*.*…*…* …*…**..*….*.*…*….*.* Basic S teps F or starting a program from scratch: 1. Establish objectives, constraints, and priorities 2. Build simple prototype (esp. UI) 3. Negotiate requirements 4. Analyze information and data needs 5. Design program structure (modules, algorithms, …) 6. Implement designs (code) 7. Test and debug
ITM © Port, Kazman 7 1. Establish Objectives, Constraints, and Priorities Objective 1: Working program that displays stars on screen Objective 2: Some form of user adjustable parameters (e.g. length and height, density, different characters) Constraint 1: Complete in less than 20 mins. Constraint 2: use only PHP and HTML Priorities: - Objective 1 is top, 2 is “if there is time”
ITM © Port, Kazman 8 2. Build a Simple Prototype
ITM © Port, Kazman 9 3. Negotiate Requirements - User interface? - Max / min number of stars? - Density definition?
ITM © Port, Kazman Analyze Information and Data Needs
ITM © Port, Kazman Design Program Structure (Modules, Algorithms, …)
ITM © Port, Kazman Implement Designs (Code)
ITM © Port, Kazman Test and Debug