|
| 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 | + |
0 commit comments