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

1

PROGRAMMING FUNDAMENTALS 2

Basics

CHAPTER
steffen.rothkugel

In some way or another programming is somewhat comparable to cooking. Well, maybe not …

From Cooking …
… to Programming
types operators
variables
expressions
classes
methods
objects
The Oxford Dictionary of the Internet defines an algorithm as “A documented series of steps
Algorithm which leads to the transformation of some data”.

A documented series of steps which leads to the transformation


of some data. [Oxford Reference]

In the same context, the Oxford Dictionary of the Internet defines a computer program as “a
Computer Program manifestation of algorithms which allow them to be executed very quickly”. As a side note, the
package lu.uni.intro2prog;
last part of the statement, i.e. “very quickly” should always be a clear objective followed by any
public class Bill {
software developer.
public static void main(String[] args) {
int vat = 17;
double net = 10.5;
double gross;

gross = net * (1 + vat / 100.0);


System.out.println(net + " plus " + vat + "% VAT = " + gross);
}

Computer programs are a manifestation of algorithms which


allow them to be executed very quickly. [Oxford Reference]
Computer programs are expressed, respectively implemented, in some programming
language. Multiple different types of programming languages exist, In order to get a—pretty
rough—feeling of the global picture in the realm of programming languages, let’s briefly
discuss a few types of languages based on some examples.

Programming Language Types

PROGRAM Add (input, output);




Imperative languages, like plain vanilla C or Pascal, basically could be described as allowing
to write programs as sequence of actions. There is a set of procedures and functions, which
VAR number1, number2, total: integer;


PROCEDURE Obtain;

BEGIN

WRITE('Please input the first number: ');
 can be combined to form higher-level constructs. Those pieces of code operate on some data,
which represent the so-called state of the application.
READLN(number1);

WRITE('Please input the second number: ');

READLN(number2);

END;


PROCEDURE Calculate;

BEGIN

total := number1 + number2;

END;


PROCEDURE Display;

BEGIN

WRITELN('The total is: ',total);


Imperative:
END;

BEGIN

obtain;


Sequence of Actions
calculate;

display;

END.
(defun toh (height start-peg-name extra-peg-name end-peg-name)
(cond ((= height 0) nil)
Functional programming in turn revolves around, well, functions. In contrast to imperative
((= height 1) (list (cons start-peg-name end-peg-name)))
(t (append (toh (1- height) start-peg-name end-peg-name extra-peg-name)
programming, which relies mainly of changing the state of variables, functional programming
(toh 1 start-peg-name extra-peg-name end-peg-name)
(toh (1- height) extra-peg-name start-peg-name end-peg-name))))) 

is based on the evaluation of mathematical functions. This type of programming language is
not that widespread for, say, business-related software.

Functional Programming

Logic programming in yet another pretty different type of language. It is based on facts and
Facts rules. Facts represent knowledge. Rules in turn operate on facts and other rules in order to
reason about the knowledge. Once again, this pretty special type of programming language is
father(sam,john).
father(john,jimmy).

Rule
rarely used in the realm of implementing “business software”.
grandpa(X,Y) :- father(X,Z),father(Z,Y)

Queries
[user] ?- grandpa(sam,jimmy)
[system] yes

[user] ?- father(X,jimmy).
[system] X = john

Logic Programming
That’s where the object-oriented programming paradigm joins the picture. For the time being,
it can be considered the type of programming language mostly used in the context of
“business software”.

Object-Oriented
Programming

One main reason to prefer the object-oriented paradigm over the imperative one is due to the
ever-increasing complexity of software systems.

Software Complexity
In the past years (and decades), that complexity grew tremendously fast. Let’s discuss why
object-orientation is better suited to copy with that high degree of complexity, particularly in
comparison to the imperative paradigm, which has mainly been used before.

Complexity Growth

Software systems typically need to realise pretty complex set of functionalities, with many
interrelationships among the data and set of operations provided, which together form a
complex network. As a matter of fact, thinking in networks is a complex task, and thus error-
prone …

Thinking in Networks is too Complex


Humans are much better at thinking in a step-by-step fashion. This is going to reduce the
complexity radically, compared to an approach which does look at the entire network.

Thinking Step-by-Step!

As you may recall, the well-known imperative approach is composed of two “parts”: data and
code …

Imperative Approach
The code is operating on the data, thereby changing the state of the data, finally producing
results. However, any piece of code basically can use and modify any piece of data. There are
a few ways to control those access paths, but they are rather limited. Especially when it
comes to global data, it is hard to restrict and regulate access in a clean way. The result
typically and unfortunately must be called an unmanageable mess!

Unmanageable Mess!

A main problem with this entire approach is that it is somehow technocentric. There is data.
And code, operating on that data. Algorithms are in the center of software design.

Technocentric View
Taking a much more anthropocentric point of view and approach is the gist of object-
Anthropocentric View orientation!

In the OO-paradigm, it is possible to model real-world entities and concepts.

Real-World Entities & Concepts


In the example shown, an object-oriented language allows to introduce entities such as
Person, Car, and Flower. They will become first-class citizens of the software—both in terms
of the software design, as well as when it comes to the implementation.

Car
Flower
Person
Real-World Entities & Concepts

Each of those entities, like for instance the Car, is finally composed of both data as well as
Properties code. The data represents the properties (often also called attributes) of the real-world
‣ manufacturer
concept of a Car. Examples include the manufacturer, the model, the horsepower, the fuel
Data

‣ model
‣ horsepower consumption, and much more. The code in turn represents the behaviour of a Car, such as to

&
‣ fuelConsumption
‣… start the engine, to accelerate and to brake, and many more operations.
Please note that this is only a simple example, omitting quite some important details. Those
Behaviour will be discussed during the entire lecture, however …
‣ startEngine()
Code

‣ accelerate()
‣ brake()
‣ tootTooooot()
‣…
After having modelled individual entities, such as Person, Car, and Flower in the small
Natural Interaction example, those can interact. A Person for instance might use the behaviour of the Car to
accelerate. The Car in turn might for example then knock over a Flower. We will need to
discuss how those interaction between different objects can be performed. A very important
aspect will be to properly control such access, since not every entity should be allowed to
access any behaviour of any other entity!
e kno
rat ckO
ele ver
acc

Overall, an object-oriented software system finally is composed of a set of communicating


objects. Please note that in object-oriented design we model individual entities, and after that
have them interacting with each other. This closely resembles the discussion from before,
namely to think in a step-by-step fashion—in order to design individual objects—and then to
create the entire network—by having objects communicating with each other!

Communicating Objects
Benefits of Object-Orientation Object-orientation thus provides a couple of benefits, as shown here …
Extensibility
Expressiveness
Maintainability
Reuse
Resilience
Modularity

Object-oriented modelling and design can be considered an art. It takes quite some practice to
do it “in the right way” …

Object-Oriented Modelling is an Art


There is No Moreover, there is no such thing as “the best OO design”. For virtually every application there
will be many different possible designs, good and bad ones …
Single Truth

You might also like