Skip to content

Commit 4b02dc1

Browse files
committed
#207 - new article added
1 parent 6e477cb commit 4b02dc1

File tree

6 files changed

+97
-9
lines changed

6 files changed

+97
-9
lines changed

_layouts/post.html

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
<!--<meta itemprop="width" content="250"/>-->
2121
<!--</div>-->
2222
</div>
23-
24-
2523
<div class="inline-element-first">
2624
<time
2725
itemprop="datePublished dateModified"
@@ -30,12 +28,8 @@
3028
</time>
3129
</div>
3230
<div class="inline-element">
33-
<a href="{{ site.url }}{{ page.url }}#disqus_thread" class="a-black">comments</a>
31+
<a href="{{ site.url }}{{ page.url }}#disqus_thread" class="a-black" data-proofer-ignore>comments</a>
3432
</div>
35-
36-
37-
38-
3933
<h1 itemprop="name headline mainEntityOfPage">{{ page.title }}</h1>
4034
{{ page.excerpt }}
4135
<figure>

_posts/2019/03/2019-03-18-extension-methods-are-not-oop.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ comments: true
1515
[Extension method] is a special language feature introduced by Kotlin, Swift and
1616
other programming languages, which allows developers to add new methods to
1717
otherwise unchangeable classes. While at first it seems like a great idea, in
18-
reality it contradicts core OOP concepts and ideas.
18+
reality it contradicts the core OOP concepts and ideas.
1919

2020
<!--more-->
2121

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
date: 2020-02-24
3+
title: "Objects and functions - friends or foes? Neither!"
4+
figure: /assets/images/posts/2020/object-and-functions-friends-or-foes-neither/Functional-Programming-vs-OOP.png
5+
figcaption: © educba.com
6+
figalt: Functional programming vs OOP
7+
description: OOP and functional programming are often contrasted, but maybe they should be compared instead?
8+
keywords:
9+
- oop
10+
11+
categories: oop
12+
comments: true
13+
---
14+
15+
Programmers often view [object-oriented] and [functional programming] as two completely different paradigms. As a result,
16+
some developers advocate for one them, pointing out the advantages of their favourite paradigm and criticizing the
17+
"opposing" one. However, I have rarely seen anyone *comparing* OOP with functional programming instead of
18+
*contrasting* them.
19+
20+
<!--more-->
21+
22+
Let's look at this piece of Kotlin code, which filters and then maps the elements of the given list:
23+
24+
```kotlin
25+
val result = listOf("Adam", "Bob", "Boris")
26+
.filter { it.startsWith("B") }
27+
.map { it.length }
28+
```
29+
30+
This is how the code above usually looks like in classic Java, one of the most popular OOP languages:
31+
32+
```java
33+
List<String> originalList = Arrays.asList("Adam", "Bob", "Boris");
34+
List<String> filteredList = new ArrayList<>();
35+
List<String> mappedList = new ArrayList<>();
36+
37+
for (String name : originalList) {
38+
if (name.startsWith("B")) {
39+
filteredList.add(employee);
40+
}
41+
}
42+
43+
for (String name : filteredList) {
44+
mappedList.add(name.length());
45+
}
46+
```
47+
48+
Is there any way we could *compare* this code snippet with the initial one? I don't think so. However, we can
49+
and should rather *contrast* it. This seems quite apparent, right? The reason for it is that this piece of Java code is
50+
not object-oriented, but *imperative*.
51+
52+
The *object-oriented* Java code should like this:
53+
54+
```java
55+
List<String> result = new ListOf<>(
56+
new Mapped<>(
57+
s -> s.length(),
58+
new Filtered<>(
59+
s -> s.startsWith("B"),
60+
new IterableOf<>("Adam", "Bob", "Boris")
61+
),
62+
)
63+
);
64+
// The "ListOf", "Mapped" and "Filtered" classes are from the
65+
// "yegor256/cactoos" java lib
66+
```
67+
68+
This [solution] is very similar to the initial functional one, although the paradigms are completely different, which brings
69+
us to the main point of the article - **OOP and functional programming are two paradigms that solve the given problem
70+
in a *declarative* manner through a *composition* of smaller logical blocks**. In other words, *what* functional and OOP
71+
are doing is the same (a composition of smaller logical blocks), but *how* they are doing it is different
72+
(OOP relies on [objects], while functional programming relies on functions).
73+
74+
I believe that by reapproaching OOP with the idea of **adding new functionality *declaratively*, *incrementally*
75+
and through a *composition* of small logical blocks**,
76+
we can not only improve our code, but also to change the way we analyze the problems we encounter (from imperative to
77+
declarative). In addition, it can enable us to revisit the OOP patterns (such as the *Decorator* pattern demonstrated above)
78+
to see which of them apply to this concept and how.
79+
80+
The next time you use an OOP language to solve a problem, try to do it *declaratively* and *incrementally*, and then
81+
see if what you come up with can *compare* to the code snippets in this article.
82+
83+
84+
[functional programming]: https://en.wikipedia.org/wiki/Functional_programming
85+
[object-oriented]: https://en.wikipedia.org/wiki/Object-oriented_programming
86+
[solution]: https://www.yegor256.com/2015/02/26/composable-decorators.html
87+
[objects]: /2018/07/27/props-file.html
88+
89+
[utility classes]: https://www.yegor256.com/2014/05/05/oop-alternative-to-utility-classes.html
90+
[reducing scope]: https://www.yegor256.com/2019/03/12/data-and-maintainability.html
91+
[fully encapsulated]: https://g4s8.github.io/fully-encapsulated/
92+

_rake/aspell.en.pws

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
personal_ws-1.1 en 0
2+
declaratively
3+
reapproaching
24
Disqus
35
POJO
46
Yegor

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{% for post in paginator.posts %}
88
<article class="article-preview">
99
{{ post.date | date: "%B %-d, %Y" }}
10-
<a href="{{ site.url }}{{ post.url }}#disqus_thread" class="inline-element a-black">comments</a>
10+
<a href="{{ site.url }}{{ post.url }}#disqus_thread" class="inline-element a-black" data-proofer-ignore>comments</a>
1111
<h1>
1212
<a href="{{ site.baseurl }}{{ post.url }}">
1313
{{ post.title }}

0 commit comments

Comments
 (0)