Answer the question
In order to leave comments, you need to log in
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
(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.
возьми_кофемашину();
налей_воды_в_кофемашину();
нажми_кнопку();
подставь_кружку();
вылей_кофе_в_кружку();
наслаждайся();
наслаждайся(
своим_кофе(
которое_сделала_кофемашина(
в_которую_налили_воды())))
есть кофемашина;
есть кружка;
есть ты;
ты.имеешь(кружку)
кофемашина.имеет(кружку)
кофемашина.поехали();
ты.возьми_свою_кружку();
ты.наслаждайся();
addition in OOP:
addition in FP:
addition in procedural style:x = 2 + 2
Sum elements std::vector<int>
. C++.
Procedural
int sum = 0;
for (int v : vec)
sum += v;
std::cout << sum << std::endl;
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;
int sum = std::accumulate(vec.begin(), vec.end(), 0);
std::cout << sum << std::endl;
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 questionAsk a Question
731 491 924 answers to any question