Structure Programming Chapter 2
Structure Programming Chapter 2
Structure Programming Chapter 2
software project
1
Covered topics
• Overview
• Structure of a program
• Separation of concerns
2
Objectives
3
I. OVERVIEWS
1. Introduction
2. Concepts of structured programming
3. Advantages
4. Disadvantages
7
1. Introduction
8
2.1. What is structured programming?
9
2.1. What is structured programming?
10
2.2. Structured programming:
structured design of software
11
3. Advantages of structured
programming
• The sequence of operations is simple to trace, thus facilitating
debugging.
• There are a finite number of structures with standardized terminology.
– Structures lend themselves easily to building subroutines.
– The set of structures is complete; that is, a programs can be written using these
structures.
12
4. Disadvantages of structured
programming
• Some high-level languages (Pascal, C, Java, Lisp, …) accept
the structures directly; while others require an extra stage
of translation.
• In some cases, structured programs may execute slower
and require more memory than the unstructured
equivalent.
• Some problems (a minority) are more difficult to solve
using only the three structures rather than a brute-force
"spaghetti" approach.
• Nested structures can be difficult to follow.
13/49
II. STRUCTURE OF
PROGRAMS
14
• How is the structure of a computer program
represented ?
15
1. Structure of computer programs
Program Book
• Library, package • Part
– File, class – Chapter
• Function, procedure, • Section
method – Paragraph
– Block » Sentence
» Statement • Phrase
• Expression • Word
• Word,
token
2. Control structures
17
2.1. Sequence structure
18
2.1. Sequence structure
– Composite statement
19
2.2. Selection structure
20
2.2. Selection structure
Single input – Single input – Single input –
single output double output multiple output
conditio conditio
value
n n
S1 S1 S2 S1 … Sn
21
2.2. Selection structure
22
2.3. Repetition structure
23
2.3. Repetition structure
conditio FALSE S1
n ..
Sn
TRUE
S1 conditio
.. n
Sn
FALSE
TRUE
24
2.3. Repetition structure
25
2.3. Repetition structure
• Case 2: The repetition number is not known in advance
• Statements in the body of this repetition structure are executed
repeatedly as long as the loop-continuation test is evaluated to false
– The condition is first evaluated: may be none of these statements is executed
– Otherwise, these statements are executed at least one time
26
3. Data structures
27
Type
• Primitive type:
– Integer, Boolean, String, …
• Composite type
– Tuple
28
4. Functions and procedures as
program elements
• Functions and procedures are part of computer program. They
can be custom-defined.
• A function or a procedure is built out of control structures in
order to manipulate on the determined data structures.
• Functions are really mathematical relations that map every
input to exactly one output. Functions are designed to return
their output value.
• Procedures are recipes for computation that perform side
effects.
• Either function or procedure is used to represent a concern.
29
Example: Functions and procedures
30
III. SEPARATION OF
CONCERNS
1. Principles
2. Concerns
3. Types of separation
4. Stakeholders of concerns
31
1. Principles
• The principle of separation of concerns states that software should be
organized so that each program element does one thing and one thing
only.
• Each program element should therefore be understandable without
reference to other elements.
• Program abstractions (procedures, objects, etc.) support the
separation of concerns.
– Procedural programming languages such as C and Pascal can separate concerns
into procedures.
– Object-oriented programming languages such as Java can separate concerns into
objects.
– Service-oriented architecture can separate concerns into services.
32
2. Concerns
33
3. Types of separation
34
4. Stakeholder concerns
• Functional concerns: related to specific functionalities to be
included in a system
• Quality of service concerns: related to the non-functional
behaviors of a system
• Policy concerns: related to the overall policies that govern
the use of the system
• System concerns: related to attributes of the system as a
whole, such as its maintainability or its configurability
• Organizational concerns: related to organizational goals and
priorities such as:
– producing a system within budget
– making use of existing software assets
– maintaining the reputation of an organization
35
5. Example: Banking system
• Concerns are not program issues but reflect the system
requirements and the priorities of the system stakeholders.
• By reflecting the separation of concerns in a program, there
is clear mapping from requirements to implementation.
Transfer Deposit
Withdraw
36
Example:
Banking system’s core concerns
• Core concerns: functional concerns relating to the
primary purpose of a system
Transfer Deposit
Transfer
concern
Deposit
concern
Withdraw
Withdraw
concern
37
Example: Transfer concern
38
Example:
Banking system’s secondary concerns
• Secondary concerns: functional concerns that
reflect non-functional and QoS requirements
Authentication Concern
Logging Concern
Withdraw
Withdraw
al concern
Transaction Concern
39
Example: authentication, transaction
and logging concerns
void transfer(Account fromAccount, Account toAccount, int amount) throws Exception {
if (!getCurrentUser().canPerform(OP_TRANSFER)) {
Authentication Concern throw new SecurityException();
}
Transaction Concern Transaction tx = database.newTransaction();
try {
if (fromAccount.getBalance() < amount) {
throw new InsufficientFundsException();
Transfer Concern }
fromAccount.withdraw(amount);
toAccount.deposit(amount);
Transaction Concern tx.commit();
Logging Concern systemLog.logOperation(OP_TRANSFER, fromAccount, toAccount, amount);
}
catch(Exception e){
Transaction Concern tx.rollback();
throw e;
}
}
Quiz and Exercises
41
1. Which of the following statements
are true ? Why ?
A. Sequence, selection and repetition are three control structures
dedicated for modeling the relationships between program elements.
B. Data structures are abstracted by means of operations that can be
performed on a domain of values.
C. Functions and procedures are the operations’ representation,
depending on different contexts of use: programming languages,
technologies, etc.
D. Separation of concerns isolates the issues to concentrate on one at a
time.
E. Separation of concerns does not support parallelization of efforts and
separation of responsibilities
42
2. Discussion
43
3. Exercises
Member
Internet
Administration
Administration
Search
Product
Of
Selling
DB Selling, Products,
Logistics, Dealing
Order
Customer
Transportation
Operation
Customer
44
3. Exercises
45
Expected results
46
4. Exercises
• Using
– Sequence, selection, and repetition structures (JSP diagram)
– At least 2 different programming languages
47