DOC PREVIEW
MIT 6 001 - Good programming practices

This preview shows page 1-2-3-22-23-24-44-45-46 out of 46 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Good programming practices Code design Documentation Debugging Evaluation and verification 01 14 19 6 001 SICP 1 45 01 14 19 6 001 SICP 2 45 Code layout and design Design of Data structures Natural collections of information Suppression of detail from use of data Procedural modules Interfaces 01 14 19 6 001 SICP 3 45 Code layout and design Design of Data structures Natural collections of information What are appropriate selectors and constructors E g points in the plane x y line segments as pairs of points Can one naturally think about operations on constructs as a unit E g translation rotation scaling of points and segments Suppression of detail from use of data Do the operations on data constructs actually depend on individual pieces In attacking a problem try to lay out the collections of objects you will need the relationships between them and the operations on them 01 14 19 6 001 SICP 4 45 Code layout and design Design of Data structures Procedural modules Computation to be reused Suppression of detail from use of procedure Interfaces 01 14 19 6 001 SICP 5 45 Code layout and design Design of Procedural modules Computation to be reused What computations appear to be specific to this problem What computations are likely to be used elsewhere Suppression of detail from use of procedure Can you specify a contract between the input and output of a computation If so does this form a natural breakpoint i e does anything depend on how this is done or only on the contract 01 14 19 6 001 SICP 6 45 Code layout and design Design of Data structures Procedural modules Interfaces types of inputs and outputs 01 14 19 6 001 SICP 7 45 An example of code modules Finding the sqrt of X Make a guess G If it is good enough i e G 2 close to X stop Otherwise get a new guess by averaging G and X G G X G X G Average Good enuf Abs 01 14 19 6 001 SICP no G yes 8 45 Documenting code Supporting code maintenance Can you read your code a year after writing it and still understand why you made particular design decisions Can you read your code a year after writing it and even understand what it is supposed to do Identifying input output behaviors Specify expectations on input and the associated contract on output of a procedure 01 14 19 6 001 SICP 9 45 Documenting code Description of input output behavior Expected or required types of arguments Type of returned value List of constraints that must be satisfied by arguments or stages of computation Expected state of computation at key points in code 01 14 19 6 001 SICP 10 45 An example of code documentation define sqrt helper lambda X guess compute approximate square root by successive refinement guess is current approximation X is number whose square root we are seeking Type number number number constraint guess 2 X if good enuf X guess can we stop if yes then return guess sqrt helper X improve X guess if not then get better guess and repeat process 01 14 19 6 001 SICP 11 45 Debugging errors Common sources of errors Common tools to debug 01 14 19 6 001 SICP 12 45 Common errors Unbound variable Cause typo Solution search for instance Unbound variable Cause reference outside scope of binding Solution Search for instance Use debugging tools to isolate instance 01 14 19 6 001 SICP 13 45 The Debugger Places user inside state of computation at time of error Can step through Reductions computation reduced to a simpler expression Substitutions computation converted to a simpler version of itself Can examine bindings of variables and parameters 01 14 19 6 001 SICP 14 45 Debugger example Lines identify stack frames most recent first Sx means frame is in subproblem number x Ry means frame is reduction number y The buffer below describes the current subproblem or reduction define foo The ERROR that started the debugger is lambda n Unbound variable bar if n 0 S0 bar R0 bar bar R1 if n 0 bar n foo n 1 n foo n 1 R2 foo n 1 S1 foo n 1 R0 n foo n 1 R1 if n 0 bar n foo n 1 R2 foo n 1 S2 foo n 1 R0 n foo n 1 R1 if n 0 bar n foo n 1 R2 foo 2 more 01 14 19 6 001 SICP 15 45 Syntax errors Wrong number of arguments Source programming error Solution use debugger to isolate instance Type errors As procedure As arguments Source calling error Solution trace back through chain of calls 01 14 19 6 001 SICP 16 45 Structure errors Wrong initialization of parameters Wrong base case Wrong end test and so on 01 14 19 6 001 SICP 17 45 Evaluation and verification Choosing good test cases Pick values for input parameters at limits of legal range Base case of recursive procedure Pick values that span legal range of parameters Pick values that reflect different kinds of input Odd versus even integers Empty list versus single element list versus many element list Retest prior cases after making code changes 01 14 19 6 001 SICP 18 45 Debugging tools The ubiquitous print display expression Tracing Print out values of parameters on input to a procedure s Print out value return on exit of procedure s Stepping Show the state of computation at each stage of substitution model 01 14 19 6 001 SICP 19 45 A debugging example We want to compute sines using the mathematical approximation 3 5 7 x x x sin x x 3 5 7 01 14 19 6 001 SICP 20 45 Initial code example define sine x define aux x n current let next expt x n fact n compute next term if small enuf next if small current just return current guess aux x n 1 current next otherwise create new guess aux x 1 0 01 14 19 6 001 SICP 21 45 Test cases sine 0 Value 0 should be 0 sine 3 1415927 should be 0 Value 22 140666527138016 sine 3 1415927 2 0 should be 1 Value 3 8104481565660486 01 14 19 6 001 SICP 22 45 Chasing down the error define sine x define aux x n current newline display n is display n display current is display current let next expt x n fact n if small enuf next current aux x n 1 current next aux x 1 0 01 14 19 6 001 SICP 23 45 Test cases sine 3 1415927 sin n is 1 current is 0 n is 2 current is 3 1415927 n is 3 current is 8 076395046346645 n is 4 current is 13 244108055421808 n is 5 current is 17 3028204216732 n is 6 current is 19 85298464991622 n is 7 current is 21 188247537124454 n is 8 current is 21 78751212841507 n is 9 current is 22 022842786585954 n is 10 current is 22 104988684118826 n is 11 current is 22 130795579321248 n …


View Full Document

MIT 6 001 - Good programming practices

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 pages

Load more
Download Good programming practices
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Good programming practices and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Good programming practices 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?