Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Commit fa60e8e

Browse files
authored
Update readme.md
- added "Best practives"
1 parent 5de892a commit fa60e8e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lambdas/readme.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ You're supposed to be familiar with OOP, have basic knowledge of JDK, and be abl
1111
##
1212
Java is an OOP language, so it always works with classes and **doesn't support standalone functions**. In case you want to **pass some function as a method parameter**, or **store some code into a variable**, you should use a *Functional Interface* and a *Lambda expression*.
1313

14-
* A *Functional Interface* represents a **function signature**. It contains only one abstract method.
14+
* A *Functional Interface (FI)* represents a **function signature**. It contains only one abstract method.
1515
* A *Lambda expression* represents a **function body**. Is an anonymous function that implements the abstract method of the functional interface
1616

1717
The purpose of the lambda and functional interfaces is to **make it easier to create function objects** and provide an **ability to use some functional programming technics in Java.**
@@ -34,3 +34,13 @@ In case you are calling some existing method inside the lambda, you can referenc
3434
```java
3535
accounts.sort(comparing(Account::getFirstName));
3636
```
37+
38+
### Best practices
39+
* use **lambdas instead of anonymous classes**
40+
* **avoid lambda parameter types**, unless it can improve code readability
41+
* **keep lambda expression as small** ( 1 line is the best option)
42+
* when creating a custom functional interface **always use `@FunctionalInterface` annotation**
43+
* **prefer standard predefined functional interfaces** (`java.util.function`)
44+
* create a **custom FI**, in case it has some **specific contract**, and you can **benefit from self-descriptive name** and **default methods**
45+
* **ALWAYS USE SPECIAL FI FOR PRIMITIVES** (e.g. `IntToDoubleFunction` instead of `Function<Integer, Double>`)
46+
* **prefer method reference** in all cases where it helps to improve readability

0 commit comments

Comments
 (0)