SP02

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Structure of programs

@2012-2013

Introduction
Software engineering = problem solving activity:
Understanding the problem
Designing an algorithm as a solution
Implementing the algorithm in a computer program

Algorithm: sequence of steps that take from the


input to the output for solving a problem.
Correct: provide a correct solution according to the
specifications
Finite : terminate
General: work for every instance of a problem
Efficient : use few resources (time, memory, etc.)

Structure of computer programs


Program
Library, package
File, class
Function, procedure,
method
Block
Statement
Expression
Word,
token

Book
Part
Chapter
Section
Paragraph
Sentence
Phrase
Word

Covered topics

Control structures
Data structures
Functions and procedures
Separation of concerns

I. CONTROL STRUCTURES

1. Sequence
2. Selection
3. Repetition
5

Introduction
Computer program represents an algorithm
resolving a given problem.
All computer programs, no matter how simple or
how complex, are written using one or more of
three basic structures:
Sequence
Selection
Repetition

These structures are called control structures or


logic structures, because they control the
program logic
6

1. Sequence structure
The sequence structure in a computer program
directs the computer to process the statements
one after another, in the order listed in the
program

1. Sequence structure
A statement may be:
Assignment statement
Input /output statement
Composite statement

2. Selection structure
Make a decision, and then take an appropriate
action based on that decision
Provide the appropriate action to take based on
the result of that decision
The decision depends on various condition values

2. Selection structure
Single input
single output

Single input
double output

condition

Single input
multiple output

condition

TRUE
S1

TRUE
S1

value

FALSE
S2

v1
S1

vn

Sn

10

2. Selection structure

11

3. Repetition structure
Allow the programmer to specify that an action
should be repeated, depending on the condition
value
When used in a program, the repetition structure,
also referred to as a loop, directs the computer to
repeat one or more statements until some
condition is met, at which time the computer
should stop repeating the statements

12

3. Repetition structure

FALSE

S1
..
Sn

condition

TRUE
S1
..
Sn

condition

FALSE

TRUE

13

3. Repetition structure
Case 1: The repetition number is known in
advance

14

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

15

II. Data structures


How to choose or devise the appropriate data
structures for a problem ?
Algorithms will have to manipulate data in some way
The way we choose to store and organize our data (i.e.
data structure) directly affects the efficiency of our
algorithm
Classic data structures : design, implementation and use

16

Type
Primitive type:
Integer, Boolean, String,

Composite type
Tuple

Abstract data type: data structure that is defined


indirectly by the operations that may be performed on
it, and the mathematical properties of those
operations

Array
List
Tree
Hash
Graph

17

III. Functions and procedures


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.
18

Example: Functions and procedures


C, C++, Java : no distinct

Pascal, .NET:
function returns value
procedure doesnt return value.

DBMS:
procedures (SPROCs) : stored compiled queries
functions (UDFs): built-in piece of expressions used to
build queries

19

IV. SEPARATION OF
CONCERNS

1.
2.
3.
4.

Principles
Concerns
Types of separation
Stakeholders of concerns
20

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.
21

2. Concerns
A concern is an area of interest or focus in a
system.
Concerns are the primary criteria for
decomposing software into smaller, more
manageable and comprehensible parts that have
meaning to a software engineer.
Procedural programming, describing concerns as
procedures
Object-oriented programming, describing concerns as
objects

22

3. Types of separation
Quality: deal separately different quality aspects
of the system
E.g.: security

Time: plan the activity of a system


E.g.: software life cycle

View: consider & analyze separately the system


E.g.: control flow, data flow

Size: dominate the system complexity


E.g.: component

23

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

24

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
25

Example:
Banking systems core concerns
Core concerns: functional concerns relating to the
primary purpose of a system

Transfer
Transfer
concern

Deposit
Deposit
concern

Withdraw
Withdraw
concern
26

Example: Transfer concern


Core concern allows bank customers to transfer
an amount of money from a given account to
another account
void transfer(Account fromAccount, Account toAccount, int amount) {
if (fromAccount.getBalance() < amount) {
throw new InsufficientFundsException();

}
fromAccount.withdraw(amount);
toAccount.deposit(amount);
}

27

Example:
Banking systems secondary concerns
Secondary concerns: functional concerns that
reflect non-functional and QoS requirements

Authentication Concern

Authentication Concern

Logging Concern

Logging Concern

Transfer
Transfer

Deposit
Deposit

concern

concern

Transaction Concern

Transaction Concern

Authentication Concern
Logging Concern

Withdraw

Withdraw
al
concern
Transaction Concern
28

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;
}

CONCLUSION

30

Key points: control structures


Control structures:
Sequence
Selection
Repetition

Data structures
Abstracted by means of operations that can be
performed on a domain of values

Functions and procedures


Operations representation, depending on different
contexts of use: programming languages, technologies,
etc.

31

Key points: separation of concerns


Dominate complexity
Separate the issues to concentrate on one at a
time
Divide & conquer
Support parallelization of efforts and separation
of responsibilities

32

Discussion
What is a well structured program ?

Correct control structures


Adequate data structures
Concise functions and procedures
Easy to read, understand, modify and verify

How to write a well structured program ?


Algorithm
Code
Documentation

33

Exercises
Write a well structured program that
reads a set of floating point values from the user in the
range [0, 100]
computes and prints the average,
finds and prints the maximum value,
finds and prints the minimum value,
prints the values in sorted order

Using
Sequence, selection, and repetition structures
At least 2 different programming languages

34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy