principles of programming
TRANSCRIPT
![Page 1: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/1.jpg)
Principles of Programming and
Software Engineering
1-1
![Page 2: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/2.jpg)
Coding without a solution design increases debugging time
A team of programmers for a large software development project involvesAn overall planOrganizationCommunication
Software engineeringProvides techniques to facilitate the
development of computer programs
1-2
Problem Solving and Software Engineering
![Page 3: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/3.jpg)
Problem solvingThe process of taking the statement of a
problem and developing a computer program that solves that problem
A solution consists of:Algorithms
Algorithm: a step-by-step specification of a function to solve a problem within a finite amount of time
Ways to store data
1-3
What is Problem Solving?
![Page 4: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/4.jpg)
1-4
The Life Cycle of Software
Figure 1.1The life cycle of software as a waterwheel that can rotate from one phase to any other phase
![Page 5: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/5.jpg)
Phase 1: SpecificationAspects of the problem which must be
specified:What is the input data?What data is valid and what data is invalid?Who will use the software, and what user interface
should be used?What error detection and error messages are
desirable?What assumptions are possible?Are there special cases?What is the form of the output?What documentation is necessary?What enhancements to the program are likely in the
future?
1-5
The Life Cycle of Software
![Page 6: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/6.jpg)
Phase 1: Specification (Continued)Prototype program
A program that simulates the behavior of portions of the desired software product
Phase 2: DesignIncludes:
Dividing the program into modulesSpecifying the purpose of each moduleSpecifying the data flow among modules
1-6
The Life Cycle of Software
![Page 7: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/7.jpg)
Phase 2: Design (Continued)Modules
Self-contained units of codeShould be designed to be:
Loosely coupledHighly cohesive
InterfacesCommunication mechanisms among modules
1-7
The Life Cycle of Software
![Page 8: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/8.jpg)
Phase 2: Design (Continued)Specifications of a function
A contract between the function and the module that calls it
Should not commit the function to a particular way of performing its task
Include the function’s preconditions and postconditions
Phase 3: Risk AnalysisBuilding software entails risksTechniques exist to identify, assess, and
manage the risks of creating a software product
1-8
The Life Cycle of Software
![Page 9: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/9.jpg)
Phase 4: VerificationFormal methods can be used to prove that an
algorithm is correctAssertion
A statement about a particular condition at a certain point in an algorithm
InvariantA condition that is always true at a particular point
in an algorithm
1-9
The Life Cycle of Software
![Page 10: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/10.jpg)
Phase 4: Verification (Continued)Loop invariant
A condition that is true before and after each execution of an algorithm’s loop
Can be used to detect errors before coding is startedThe invariant for a correct loop is true:
Initially, after any initialization steps, but before the loop begins execution
Before every iteration of the loopAfter every iteration of the loopAfter the loop terminates
1-10
The Life Cycle of Software
![Page 11: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/11.jpg)
Phase 5: Coding (Continued)Involves:
Translating the design into a particular programming language
Removing syntax errorsPhase 6: Testing
Involves:Removing the logical errors
1-11
The Life Cycle of Software
![Page 12: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/12.jpg)
Phase 6: Testing (Continued)Test data should include:
Valid data that leads to a known resultInvalid dataRandom dataActual data
Phase 7: Refining the SolutionMore sophisticated input and output routinesAdditional featuresMore error checks
1-12
The Life Cycle of Software
![Page 13: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/13.jpg)
Phase 8: ProductionInvolves:
Distribution to the intended usersUse by the users
Phase 9: MaintenanceInvolves
Correcting user-detected errorsAdding more featuresModifying existing portions to suit the users better
1-13
The Life Cycle of Software
![Page 14: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/14.jpg)
A solution is good if:The total cost it incurs over all phases of its life
cycle is minimal
1-14
What is a Good Solution?
![Page 15: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/15.jpg)
The cost of a solution includes:Computer resources that the program
consumesDifficulties encountered by usersConsequences of a program that does not
behave correctlyPrograms must be well structured and
documentedEfficiency is one aspect of a solution’s cost
1-15
What is a Good Solution?
![Page 16: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/16.jpg)
AbstractionSeparates the purpose of a module from its
implementationSpecifications for each module are written
before implementationFunctional abstraction
Separates the purpose of a function from its implementation
1-16
Achieving a Modular Design: Abstraction
![Page 17: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/17.jpg)
Data abstractionFocuses of the operations of data, not on the
implementation of the operationsAbstract data type (ADT)
A collection of data and operations on the dataAn ADT’s operations can be used without knowing
how the operations are implemented, if the operations’ specifications are known
1-17
Achieving a Modular Design
![Page 18: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/18.jpg)
Data structureA construct that can be defined within a
programming language to store a collection of data
1-18
Achieving a Modular Design
![Page 19: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/19.jpg)
Information hidingHide details within a moduleEnsure that no other module can tamper with
these hidden detailsPublic view of a module
Described by its specificationsPrivate view of a module
Consists of details which should not be described by the specifications
1-19
Achieving a Modular Design
![Page 20: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/20.jpg)
Principles of object-oriented programming (OOP)Encapsulation
Objects combine data and operationsInheritance
Classes can inherit properties from other classesPolymorphism
Objects can determine appropriate operations at execution time
1-20
Object-Oriented Design
![Page 21: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/21.jpg)
Advantages of an object-oriented approachExisting classes can be reusedProgram maintenance and verification are
easier
1-21
Object-Oriented Design
![Page 22: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/22.jpg)
Object-oriented design (OOD)Produces modular solutions for problems that
primarily involve dataIdentifies objects by focusing on the nouns in
the problem statement
1-22
Top-Down Design
![Page 23: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/23.jpg)
Top-down design (TDD)Produces modular solutions for problems in
which the emphasis is on the algorithmsIdentifies actions by focusing on the verbs in
the problem statementA task is addressed at successively lower levels
of detail
1-23
Top-Down Design
![Page 24: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/24.jpg)
1-24
Top-Down Design
Figure 1.4
A structure chart showing the hierarchy of modules
![Page 25: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/25.jpg)
Use OOD and TDD togetherUse OOD for problems that primarily involve
dataUse TDD to design algorithms for an object’s
operations
1-25
General Design Guidelines
![Page 26: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/26.jpg)
Consider TDD to design solutions to problems that emphasize algorithms over data
Focus on what, not how, when designing both ADTs and algorithms
Consider incorporating previously written software components into your design
1-26
General Design Guidelines
![Page 27: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/27.jpg)
The Unified Modeling Language (UML) is a modeling language used to express object-oriented designsClass diagrams specify the name of the class,
the data members of the class, and the operations
1-27
Modeling Object-Oriented Design Using UML
![Page 28: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/28.jpg)
UML (Continued)Relationships between classes can be shown by
connecting classes with linesInheritance is shown with a line and an open
triangle to the parent classContainment is shown with an arrow to the
containing class UML provides notation to specify visibility,
type, parameter, and default value information
1-28
Modeling Object-Oriented Design Using UML
![Page 29: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/29.jpg)
1-29
UML DiagramsFigure 1.6
UML diagram for
a banking system
![Page 30: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/30.jpg)
Modularity has a favorable impact onConstructing programsDebugging programsReading programsModifying programsEliminating redundant code
1-30
A Summary of Key Issues in Programming
![Page 31: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/31.jpg)
Modifiability is possible through the use ofFunctionNamed constantsThe typedef statement
1-31
A Summary of Key Issues in Programming
![Page 32: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/32.jpg)
Ease of use In an interactive environment, the program
should prompt the user for input in a clear manner
A program should always echo its inputThe output should be well labeled and easy to
read
1-32
A Summary of Key Issues in Programming
![Page 33: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/33.jpg)
Fail-Safe ProgrammingFail-safe programs will perform reasonably no
matter how anyone uses itTest for invalid input data and program logic
errorsHandle errors through exception handling
1-33
A Summary of Key Issues in Programming
![Page 34: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/34.jpg)
Eight issues of personal style in programmingExtensive use of functionsUse of private data fieldsAvoidance of global variables in functionsProper use of reference arguments
1-34
A Summary of Key Issues in Programming
![Page 35: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/35.jpg)
Eight issues of personal style in programming (Continued)Proper use of functionsError handlingReadabilityDocumentation
1-35
A Summary of Key Issues in Programming
![Page 36: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/36.jpg)
DebuggingProgrammer must systematically check a
program’s logic to determine where an error occurs
Tools to use while debugging:WatchesBreakpointscout statementsDump functions
1-36
A Summary of Key Issues in Programming
![Page 37: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/37.jpg)
Software engineering: techniques to facilitate development of programs
Software life cycle consists of nine phasesLoop invariant: property that is true before
and after each iteration of a loopEvaluating the quality of a solution must
consideration a variety of factors
1-37
Summary
![Page 38: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/38.jpg)
A combination of object-oriented and top-down design techniques leads to a modular solution
The final solution should be as easy to modify as possible
A function should be as independent as possible and perform one well-defined task
1-38
Summary
![Page 39: Principles of programming](https://reader035.vdocuments.mx/reader035/viewer/2022070521/58efd51a1a28ab5f7b8b45c3/html5/thumbnails/39.jpg)
Functions should include a comment: purpose, precondition, and postcondition
A program should be as fail-safe as possibleEffective use of available diagnostic aids is
one of the keys to debuggingUse “dump functions” to help to examine and
debug the contents of data structures
1-39
Summary