270 likes | 375 Views
Lec01 Programming Development Introduction. Software Engineering Fall 2005. 2005F. Instructor John D Lin - johndlin@hotlinemail.com Lectures Tues & Thurs 8:00-9:30 RM. 100, Lower Block Office Hours School of Software tel:. Content. Design and implementation
E N D
Lec01 Programming Development Introduction Software Engineering Fall 2005
2005F Instructor John D Lin - johndlin@hotlinemail.com Lectures Tues & Thurs 8:00-9:30 RM. 100, Lower Block Office Hours School of Software tel:
Content Design and implementation Next step after learning a language and algorithms=> How to fit it all together ?=> write code once, reuse it many times=> understand code you wrote years ago=> let someone else use and understand your code=> build something so large you cannot do it just by yourself Build a software architecture that can be re-used, adapted to changing needs, or extended by a third party.
Practice Practice “healthy” programming style:indentation, comments, idioms, constant declaration... Practice abstraction skills:decomposition, encapsulation, generalization Practice cooperation with other developers:requirements, interfaces, group work Practice the use of a design vocabulary:design patterns Practice the design and implementation of a non-trivial prototype in Java
Grading 50 % 5 homework assignments (every 2 weeks) Individual practice of programming & design 50 % group project with demonstration at end of term 10% requirements analysis 10% design workbook 30% final demonstration: complexity: size & difficulty design: design & code quality performance: demo smoothness & impressiveness
Textbook Program Development in Java: Abstraction, Specification, and Object-Oriented Design by Barbara Liskov and John GuttagAddison Wesley 2001 + material on ftp ftp://ftp.cs.sjtu.edu.cn/lin-dz/pd2005f
Programming Techniques Techniques to manage complexity by decomposition and abstraction Modular design Programming in the large High-quality programs are: reliableefficienteasy to understand, modify, maintain => overview of abstraction techniques ….
1.1 Decomposition “divide and rule”(Julius Caesar) Divide the program in modules:small programs interact in simple, well-defined ways => Different people can implement different modules independently Maintain and modify in a controlled manner with limited effect (no spagetti code)
Dividing into subproblems Each subproblems approximately same level of detail Each subproblems can be solved independently The solutions to subproblems can be combined to solve the whole problem sort a list of size n = sort n/2 sort n/2 merge sorted lists
The art of decomposition It is easy to solve subproblems independently The hard part is to combined them Problem: Write a play using n writers Naïve decomposition: each writer takes a character and goes off to write the character’s dialog lines independent from other writers -> incoherent nonsensical resultcounter-productive decomposition
1.2 Abstraction Decompose by changing the level of detail to be considered Problem: Write a play using n writers Abstraction: define acts, plot, different scenes each writer takes a scene and goes off to write -> coherent result but with varying styles (Alexandre Dumas’ daddy)
Abstraction = ignore detail Abstraction = choose to forget information-> treat different things as if they are the same-> one to many-> simplify analysis by separating relevant information from irrelevant information But: relevance depends on context For example: (8/3)*3 =? 5 + 3
Abstraction Hierarchies FishSharkSalmon ReptileFrogSnake MammalRodentCetacean Primate Chimpanzee Human depends on context (e.g. sea-based vs. land-based)
High-level languages Provide abstraction from machine language(one to many), but not enough Code fragments differbut accomplish the same purpose: found = false;for (int I = 0; I < a.length; I++) if (a[I] == e) {z = I; found = true} found = false;for (int I = a.length-1; I >= 0; I--) if (a[I] == e) {z = I; found = true}
Very High-level languages Provide fixed set of general data structures plus powerful set of primitives: found = a.isIn(e);if (found) z = a.indexOf(e); But: no one can predict all abstractions that will be needed (except in very limited domains) => provide the programmer with language mechanisms to construct abstractions
Sidebar 1.1 Abstraction mechanisms • Abstraction by parameterization abstracts from the identity of the data by replacing them with parameters. • Abstraction by specification abstracts from the implementation details to the behaviors that the users can depend on.
Abstraction by parameterization Abstract from the identity of data by replacing instances by parameters. Generalizes modules to be used in more situations. w * w + z * z // add square of w and square of z int squares (int x, y) {return x * x + y * y);} // a function on formal parameters x and y squares (w,z) Describes an infinite number of computations Easy to realize in current programming languages
Abstraction by specification Abstract from the computation described by a procedure to the end that procedure was designed to accomplish. Float sqrt (float coef) {// Requires: coef > 0// Effect: Returns an approximation of square root of coef float ans = coef /2.0;int I = 1;while (I < 7) { ans = (ans + coef / ans ) / 2.0; I++; }return ans;}
Assertions Requires assertion= precondition= a set of conditions sufficient to ensure proper operation of the procedure Effects assertion= postcondition= what is true after completion of the procedure for which the precondition was satisfied
Two sides to assertions If the precondition holds, the postcondition will hold after the execution of the procedure-> users don’t have to look at the procedure body to use the procedure Only properties inferred from postcondition hold-> users cannot rely on code behavior not specified by the postcondition Result => the procedure’s code can change independently from the user’s code
Kinds of abstractions Abstraction by parameterization and abstraction by specification are tools to construct different kinds of abstraction: • Procedural abstraction adding new operations to the language. E.g., sqrt • Data abstraction adding objects andoperations that are meaningful for the objects. E.g., MultiSets • Iteration abstraction iterating over a collection without revealing details of how the items are obtained. • Type hierarchy allow us to abstract from individual data types to families of related types.
Procedural abstraction Procedural abstraction introduces new operations Adds functionality to the machine defined by a high-level language Useful if a problem can be decomposed into independent functional units. Uses both parameterization and specification
Data abstraction Data abstraction introduces new data types Data objects are expressed as sets of operations that are meaningful for those objects:- create objects - get information- modify objects E.g. MultiSets are sets that can store more than one instance of the same element: empty insert delete numberOf size
MultiSets The MultiSet defines abstractions by statements such as: • size of MultiSet insert(s,e) = size(s)+1 • For all ethe numberOf times e occurs in the MultiSet empty() = 0 These statements deal with more than one operation => data abstraction is more than a set of independent operations
Iteration abstraction Iteration abstraction allows us to iterate over items in a collection without revealing details of how the items are obtained i = s.iteration(); While (i.hasMoreElements()) { e = i.nextElement();e.doSomething(); } The order in which the elements are visited is abstracted from
Type hierarchies Type hierarchies allow us to abstract from individual types to families of related types The common operations are defined in a supertype Sub types define extra operations (and can themselves be ancestors to a family of subtypes) Example: the following types can be read from StreamFile BinaryFile TextFileKeyboardSocket
Summary Decomposition and abstraction are techniques to construct large programs that are easy to understand, maintain and modify Abstraction = ignore details Parameterization generalizes to wider applicability Specification separates implementation details from the behavior a user can depend on Four kinds of abstraction: • Procedural abstraction • Data abstraction • Iteration abstraction • Type hierarchy