Professional Documents
Culture Documents
01 Basics
01 Basics
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”.
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;
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 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!
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
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” …