DOC PREVIEW
Duke CPS 108 - Toward a Design/Programming Methodology

This preview shows page 1 out of 4 pages.

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

Unformatted text preview:

Duke CPS 1085. 1Toward a Design/Programming Methodology● Although you must write code, you shouldn’t be thinking aboutcode when you first start thinking about solving a problem➤ Analysis, Design, Implementation, but spiral through them,don’t use the “waterfall” model● Analysis: investigation of problem, not a solution --- for 108problems there is little/no analysis, but for open-endedproblems in business there are lots of people to talk to, issues toinvestigate➤ Deliver a set of requirements as the outcome of the analysis● Design: Logical OO objects that help realize a solution to theproblem➤ concentrate on classes and behavior, not on state➤ eventually will need to get to state, but not at firstDuke CPS 1085. 2Use Cases: From Analysis to Design● How will someone use the program? What are typicalscenarios?➤ Users have different roles, outside entities interact withthe program, but also objects within the program interactwith other objects➤ Give scenarios a description and provide a paragraph thatdescribes the user/system interactions● Use cases/scenarios can be done formally or informally, butyou need to think about how the system works➤ From use case proceed to design of classes➤ Nouns are classes, verbs are methods/member functions➤ Brainstorm, throw things out, revisit analysis, get to stateonly at the endDuke CPS 1085. 3Example: WOOFII in action● How does user interact with system? Are there any issues interms of user expectations of the system? Why?➤ Fast or small?➤ Possible additions to the system?● Is it worth thinking about this from an OO viewpoint or isthis main, two functions, and that’s it➤ Suppose you have a class WordsInaFile, can this be usedif specs change to do words in all files in a directory?• WordCollection vs. WordsInaFile• Operations on WordCollection objects?● Should the readFile method sort objects? Why?Alternatives?Duke CPS 1085. 4WOOFII classes (and maybe KWIC classes)● Similarities/Differences➤ Is a word a string?➤ Time vs. space➤ What about repeated words on a line in both programs● Keep in mind➤ Open/Closed principle, extend but don’t modify➤ Coupling and Cohesion, each function/class captures oneabstraction, minimal coupling between functions/classes➤ Test each class outside of the main application➤ Document design decisions as they happen rather thanafter the factDuke CPS 1085. 5C++ idioms● Consider Date, DirStream, DirEntry, what happens with➤ x = y;➤ assignment is memberwise unless operator = overloaded➤ copy constructor used in passing parameters by value● If you need one of: destructor, assignment operator, copyconstructor, you need all of them➤ heuristic only: managing resources other than memory➤ preventing objects from being copied➤ what about non-copyable state, e.g., stream● In assignment operator, watch for self-assignmentDuke CPS 1085. 6Assignment and C++● Copy constructor used to construct a variable that has neverbefore existed➤ no need to check self-assignment➤ use initializer lists➤ parameter is const-reference object, sometimes non-constneeded as well● Assignment operator gives new value to existing object➤ check for self-assignment➤ clean up old resources when allocating new ones➤ Not used in Foo x = y;● Both can be disabled by making methods privateDuke CPS 1085. 7Destructors in C++● Destructors clean up resources allocated by an object➤ Objects/variables on the stack are destructed automaticallywhen they go out of scope➤ Objects allocated on the heap are destructed when thedelete operator is called● It’s nice not to worry about reclaiming storage, easy to getwrong delete p; //if p is NULL? Points to stack object?➤ Java has automatic garbage collection, your C++ programscan have this too• commercial/free products• never delete anything, don’t implement destructors● Who is responsible for deletion?➤ Creator or benefactor? What about singleton?Duke CPS 1085. 8The code doesn’t run● Test classes in isolation, not as part of the complete program➤ each class should (ideally) have its own test suite➤ day.cpp, testday.cpp, wagreader.cpp, testwagreader.cpp,...● Use the debugger: gdb, ddd➤ debugger is much faster than edit/compile/debug➤ important for pointers to find out where the problem is➤ run, break at, where, step (into), next (statement)● Never define a variable, especially a pointer, without giving ita value● Ask questions of prof, TA, UTA, each other● Post salient parts of problem, not “my code doesn’t


View Full Document

Duke CPS 108 - Toward a Design/Programming Methodology

Download Toward a Design/Programming Methodology
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 Toward a Design/Programming Methodology 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 Toward a Design/Programming Methodology 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?