B
B
beginer1232017-09-14 17:01:27
Programming
beginer123, 2017-09-14 17:01:27

A simple example of the same code in OOP, functional and procedural style?

Hello, please give some simple example of comparing the same code in OOP, FP and procedural style.
I did not find on the Internet that these 3 approaches were compared.
I would like to understand the difference between these 3 styles in a specific example

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivan Bogachev, 2017-09-14
@beginer123

(All this is wildly not academic, not correct and generally, but it should be clear):
Procedural approach: there is a sequence of commands. Each of them can contain a sequence of commands.

возьми_кофемашину();
налей_воды_в_кофемашину();
нажми_кнопку();
подставь_кружку();
вылей_кофе_в_кружку();
наслаждайся();

FP - emphasis on action. One action leads to another.
наслаждайся(
    своим_кофе(
        которое_сделала_кофемашина(
            в_которую_налили_воды())))

OOP - emphasis on objects. Any action performs a specific object
есть кофемашина;
есть кружка;
есть ты;

ты.имеешь(кружку)
кофемашина.имеет(кружку)
кофемашина.поехали();
ты.возьми_свою_кружку();
ты.наслаждайся();

L
longclaps, 2017-09-14
@longclaps

addition in OOP:
addition in FP:
addition in procedural style:
x = 2 + 2

M
Mercury13, 2017-09-14
@Mercury13

Sum elements std::vector<int>. C++.
Procedural

int sum = 0;
for (int v : vec)
  sum += v;
std::cout << sum << std::endl;

OOP.
class Accumulator
{
public:
  void feed(int value) { fSum += value; }
  int sum() const { return fSum; }
private:
  int fSum = 0;
}

Accumulator acc;
for (int v : vec)
  acc.feed(vec);
std::cout << acc.sum() << std::endl;

Functional (although slightly infused with templates)
int sum = std::accumulate(vec.begin(), vec.end(), 0);
std::cout << sum << std::endl;

UPD. The third one is so small because they found a suitable function in the standard library.
UPD2. Now imagine that you need to calculate not the sum, but something complex - for example, the mean and square deviation. In the procedural one, you will either have to reveal complex formulas, or establish some kind of generalizations. The object changes by one or two. In the functional one, you will have to change the template function that works on the iterator.

S
Sergey, 2017-09-14
@red-barbarian

do not try to understand the difference between styles from the texts. )))
Deal in is that differences in paradigms. differences in how a particular analyst models the subject area.
An example
is payroll
procedural approach - we count the hours worked, calculate the amount according to the tariff, calculate deductions, calculate taxes, and so on and so forth. Procedures consist of sub-procedures.
object approach - there is a report card, there is a tariff rate, there are taxes, payments, deductions. This is something similar to what an accountant works with. therefore, such an approach in this case is close to the given subject area.
functional - can be represented as follows. there is a data stream. we will convert to a new data stream. then we will transform again, etc. there is a clock. we will make a sum out of them, remove deductions from them, remove taxes, again deductions ... at the output we will get the amount of payments.
although in the example the names intersect, it is not a fact that entities in one approach will appear in another. That is why it is not necessary to compare the same subject area in different approaches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question