10/20/20091DESIGNING, CODING, AND DOCUMENTINGLecture 15CS2110 – Fall 2009Designing and Writing a Program2Don't sit down at the terminal immediately and start hackingDesign stage –THINK firstabout the data you are working withabout the operations you will perform on itabout data structures you will use to represent itabout how to structure all the parts of your program so as to achieve abstraction and encapsulationCoding stage – code in small bitstest as you gounderstand preconditions and postconditionsinsert sanity checks (assert statements in Java are good)worry about corner casesUse Java API to advantageThe Design-Code-Debug Cycle3Design is faster than debugging (and more fun)extra time spent designing reduces coding and debuggingWhich is better?Actually, should be more like this:designcodedebugdesigncodedebugDivide and Conquer!4Break program into manageable parts that can be implemented, tested in isolationDefine interfaces for parts to talk to each other – develop contracts(preconditions, tditi)postconditions)Make sure contracts are obeyedClients use interfaces correctlyImplementers implement interfaces correctly (test!)Key: good interface documentationPair Programming5Work in pairsPilot/copilotpilot codes, copilot watches and makes suggestionspilot must convince copilot that code workstake turnsOr: work independently on different parts after deciding on an interfacefrequent design revieweach programmer must convince the otherreduces debugging timeTest everythingDocumentation is Code6Comments (esp. specifications) are as important as the code itselfdetermine successful use of codedetermine whether code can be maintainedcreation/maintenance = 1/10Documentation belongs in code or as close as possibleCode evolves, documentation drifts awayPut specs in comments next to code when possibleSeparate documentation? Code should link to it.Avoid useless commentsx = x + 1; //add one to x-- Yuck!Need to document algorithm? Write a paragraph at the top. Or break method into smaller, clearer pieces.
has intentionally blurred sections.
Sign up to view the full version.