Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Structure Programming Chapter 2

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

Structured programming in

software project

1
Covered topics

• Overview

• Structure of a program

• Separation of concerns

2
Objectives

• After this lesson, students will be able to:


– Recall the term of structured programming

– Summarize the relationships between software qualities,


algorithms and the structure of program.

– Formulate the separation of concerns.

3
I. OVERVIEWS

1. Introduction
2. Concepts of structured programming
3. Advantages
4. Disadvantages

7
1. Introduction

• Software engineering = problem solving activities


– 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, bandwith, etc.)

à Need of a structured approach

8
2.1. What is structured programming?

• Initially: programming without the use of the


GOTO statement

• Then: a method of writing a computer program to


minimize the problem complexity:
– top-down analysis for problem solving

– modularization for program structure and organization

– structured code for the individual modules

9
2.1. What is structured programming?

• Now: a method of designing software


components / program elements and their
relationships to:
– Minimize complexity

– Adapt to change (identify modifications for additional


functionalities or correcting errors)

– Improve the reliability and clarity of programs

10
2.2. Structured programming:
structured design of software

• Conceptualizing a problem into several well-organized


elements of solution (mostly based on “divide and
conquer” strategy)
• Maintaining the unified structure at different levels
– Problem solving: top-down, bottom-up, middle-out
– Program abstraction and organization: modules, services,
functions, objects, …
– Program elements: structured code

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.

• Structures are self-documenting and, therefore, easy to read.


• Structures are easy to describe in flowcharts, syntax diagrams,
pseudo code, and so on.
• Structured programming results in increased programmer
productivity-programs can be written faster.

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

1. Structure of computer programs


2. Control structures
3. Data structures
4. Functions and procedures as program elements

14
• How is the structure of a computer program
represented ?

• Given a set of programming languages and


programs that are written by using these
languages. How do these programs differ from ?

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

• 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

17
2.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

18
2.1. Sequence structure

• A statement may be:


– Assignment statement

– Input /output statement

– Composite statement

19
2.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

20
2.2. Selection structure
Single input – Single input – Single input –
single output double output multiple output

conditio conditio
value
n n

TRUE TRUE FALSE v1 vn

S1 S1 S2 S1 … Sn

21
2.2. Selection structure

22
2.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

23
2.3. Repetition structure

conditio FALSE S1
n ..
Sn
TRUE
S1 conditio
.. n
Sn
FALSE
TRUE

24
2.3. Repetition structure

• Case 1: The repetition number is known in


advance

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

• 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

27
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

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

• C, C++, Java : no distinct


• Pascal, .NET:
– function returns value
– procedure doesn’t return value.
• DBMS:
– procedures (SPROCs) : stored compiled queries
– functions (UDFs): built-in piece of expressions used to
build queries

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

• 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

33
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

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

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

38
Example:
Banking system’s 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
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

• Now let’s go over what you have learned through


this lesson by taking a quiz.

• When you’re ready, press Start button to take the


quiz

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

• 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

43
3. Exercises

• The XYZ Wear Corporation plans to develop (from system


analysis to operation testing) the web site for selling wears
through Internet and to form the new whole system as
follow:
Selling Web Site
Main System

Member
Internet

Administration
Administration
Search
Product

Of
Selling
DB Selling, Products,
Logistics, Dealing
Order
Customer

Transportation

Operation
Customer

44
3. Exercises

• Please help users in clarification of the website’s core


functionalities ( > = 5 ) .
• Create the image of software quality by nominating the
assessment criteria of website (common qualities, process
qualities, application - specific qualities ).
• Write down the users’ requirements on the following
aspects of website: functional, QoS, policy, system and
organizational.
• Separate the concerns accordingly to the following criteria:
quality, time, view and size.

45
Expected results

• Document (2-5 A4 pages) with table of contents,


diagram/figure, references
– What are the core functionalities?
• 5-7 functionalities (input, action/process, output) among the identified
must-have functionalities

– Why do you think they are core functionalities?


• External view point: how these functionalities represent the application
logic of the website / the users’ requirements?
• Internal view point: how these functionalities compose your code ?
How they represent the implementation/Deployment requirements?

à Recheck the requirements you write down in the next.

46
4. 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 (JSP diagram)
– At least 2 different programming languages

47

You might also like