Skip to content

Commit 6cfeec8

Browse files
committed
tutorial 1 on refactoring to functional style
1 parent f1bbed6 commit 6cfeec8

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
id: refactoring
3+
title: "Refactoring from the Imperative to the Functional Style"
4+
slug: learn/refactoring-to-functional-style
5+
slug_history:
6+
- refactoring
7+
type: tutorial
8+
category: language
9+
category_order: 3
10+
group: refactoring-to-functional-style
11+
layout: learn/tutorial-group-top.html
12+
subheader_select: tutorials
13+
main_css_id: learn
14+
description: "Learning to change code from the Imperative to the Functional Style."
15+
---
16+
17+
This part of the tutorial helps you to learn the functional style equivalent of the imperative style code we often find. As you move forward in your projects, wherever it makes sense, you can change imperative style code to functional style code using the mappings you learn in this tutorial.
18+
19+
In this series we cover the following conversions from the imperative to the functional style:
20+
21+
| Description | Tutorial |Imperative Style | Functional Style Equivalent |
22+
| Simple `for` loop | link to simple loop tutorial | for | range or rangeClosed |
23+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
id: refactoring.simple.loops
3+
title: Converting Simple Loops
4+
slug: learn/refactoring-to-functional-style/simpleloops
5+
slug_history:
6+
- learn/converting-simple-loops-to-functional-style
7+
type: tutorial-group
8+
group: refactoring-to-functional-style
9+
layout: learn/tutorial-group.html
10+
subheader_select: tutorials
11+
main_css_id: learn
12+
toc:
13+
- Imperative vs. Functional Styles {styles}
14+
- Simple for Loops {simplefor}
15+
- Mappings {mappings}
16+
description: "Converting Simple Imperative Loops to Functional Style."
17+
author: ["Venkat Subramaniam"]
18+
last_update: 2023-07-06
19+
---
20+
21+
<a id="styles">&nbsp;</a>
22+
## Imperative vs. Functional Styles
23+
24+
The older versions of Java supported the Object-Oriented paradigm mixed with the imperative style of programming. Starting Java 8, you can also mix the functional style of programming in your code. If your code base was started during the Java 7 or earlier times or later by programmers mostly familiar with older versions of Java, it will be filled with the imperative style code.
25+
26+
Imperative style is where we tell what to do and also how to do it. Functional style is declarative in nature, where we tell what to do and delegate the how or the details to the underlying libraries. Imperative style code may be easier to write since most of us are very familiar with it. However, the code becomes verbose, complex, and hard to read. Functional style may be hard at first, mainly because most programmers are less familiar with it. In general, it's easier to read, understand, and change. With practice, it becomes easier to write as well.
27+
28+
In this tutorial series we will take a look at a number of common imperative style code and find a mapping or an equivalent functional style code that we can use instead. As you work with your code based, when you're ready to fix a bug or make an enhancement, you may find it useful to refactor some of the imperative style code to the functional style. You can use this tutorial as a guide to find the imperative to functional style mappings for some common situations.
29+
30+
In this tutorial we'll focus on simple loops.
31+
32+
<a id="simplefor">&nbsp;</a>
33+
## Simple for Loops
34+
35+
Let's start with the traditional for loop where we perform an action for values of an index over a range.
36+
37+
```java
38+
for(int i = 0; i < 5; i++) {
39+
System.out.println(i);
40+
}
41+
```
42+
43+
In the above code, the essence is the range, from `0` to one less than `5`. The ceremony, the noise, from the __how__, is the syntax plus the increment operation on the index variable `i`. We can keep the essence and remove the ceremony by turning the code to the functional style.
44+
45+
If you like to use the functional style to write this `for` loop, you can do quite easily and the clue is in the sentence before the code: __index over a range__. Since we're iterating over a range, the `range` method of `IntStream` is the direct equivalent for this.
46+
47+
```java
48+
import java.util.stream.IntStream;
49+
50+
...
51+
IntStream.range(0, 5)
52+
.forEach(i -> System.out.println(i));
53+
```
54+
55+
You can further make this concise by using a methor reference for the `println` method.
56+
57+
```java
58+
import java.util.stream.IntStream;
59+
60+
...
61+
IntStream.range(0, 5)
62+
.forEach(System.out::println);
63+
```
64+
65+
The functional style code is more concise, easier to read, and the intention is clearer in this version than in the imperative version.
66+
67+
What if your `for` loop runs to include the ending value, like in the following code, you may wonder.
68+
69+
```java
70+
for(int i = 0; i <= 5; i++) {
71+
System.out.println(i);
72+
}
73+
```
74+
75+
The `IntStream` interface has you covered, it has a `rangeClosed` method exactly for this purpose.
76+
77+
```java
78+
import java.util.stream.IntStream;
79+
80+
...
81+
IntStream.rangeClosed(0, 5)
82+
.forEach(System.out::println);
83+
```
84+
85+
The `rangeClosed` method is useful to iterate from the starting value all the way to include the ending value.
86+
87+
Whether you use the `range` method or the `rangeClosed` method, you get a stream of `int` values over which you can use the internal iterator to perform actions. Later in this series we will look at operations beyond `forEach`.
88+
89+
In the previous code examples, the internal iterator removed the burden of iterating from your shoulders. The stream takes care of stepping over the range of values, one at a time. You only have to focus on what to do for each element, as they're provided to you, in the `forEach` method. In our examples, we merely printed the provided value. You can do just about any operation you like, like saving the information to a database, sending it off to a remote service, etc.
90+
91+
Unlike the external iterator provided by the `for` loop, the code that uses the internal iterator is more concise, has less noise, avoids the need to explicitly mutate the index variable, is easier to read, easier to modify, and more pleasant to work with.
92+
93+
Proceed to look for opportunities in your own code base where you see the traditional `for` loop and modify it to using the `IntStream`'s `range` or `rangeClosed` method. Make sure you verify that the code works as expected after the change, preferably by running automated tests that you may already have.
94+
95+
<a id="mappings">&nbsp;</a>
96+
##Mappings
97+
98+
Anywhere you see a simple `for` loop, you can use either the `range` or the `rangeClosed` method of `IntStream`. Use the `range` method if you want to iterate until but not including the ending value. Use the `rangeClosed` to include the ending value as well in your iteration.
99+

0 commit comments

Comments
 (0)