How Much Immutability Is Enough?

22
/22 @yegor256 1 How Much Immutability Is Enough Yegor Bugayenko

Transcript of How Much Immutability Is Enough?

Page 1: How Much Immutability Is Enough?

/22@yegor256 1

How Much Immutability Is

EnoughYegor Bugayenko

Page 2: How Much Immutability Is Enough?

/22@yegor256 2

object is a representative

Page 3: How Much Immutability Is Enough?

/22@yegor256 3

File f = new File(“picture.jpg”); if (f.length() > 1000) { f.delete(); }

Page 4: How Much Immutability Is Enough?

/22@yegor256 4

immutability means loyalty

Page 5: How Much Immutability Is Enough?

/22@yegor256 5

class Book { private int isbn; private String title; public setIsbn(int isbn) { this.isbn = isbn; } public setTitle(String title) { this.title = title; } }

Page 6: How Much Immutability Is Enough?

/22@yegor256 6

class Book { private final int isbn; private final String title; public Book(int isbn, String title) { this.isbn = isbn; this.title = title; } }

Page 7: How Much Immutability Is Enough?

/22@yegor256 7

thread-safe

Page 8: How Much Immutability Is Enough?

/22@yegor256 8

List<String> list = new ArrayList<>(); list.add(“x”); // thread A list.add(“y”); // thread B

Page 9: How Much Immutability Is Enough?

/22@yegor256 9

failure atomicity

Page 10: How Much Immutability Is Enough?

/22@yegor256 10

class Numbers { private int[] array; private int position; void add(int x) { this.position++; if (x < 0) { throw new Exception(“only positive”); } this.array[this.position] = x; } }

Page 11: How Much Immutability Is Enough?

/22@yegor256 11

no identity mutability

Page 12: How Much Immutability Is Enough?

/22@yegor256 12

Map<Book, Boolean> map = new HashMap<>(); book.setId(1); map.put(book, true); book.setId(2); map.put(book, true);

Page 13: How Much Immutability Is Enough?

/22@yegor256 13

side effect free

Page 14: How Much Immutability Is Enough?

/22@yegor256 14

void print(Book book) { System.out.println( “book ID is: ” + book.getId() ); book.setId(123); }

Page 15: How Much Immutability Is Enough?

/22@yegor256 15

no temporal coupling

Page 16: How Much Immutability Is Enough?

/22@yegor256 16

request.setURI(“http://google.com”); request.setMethod(“GET”); request.addHeader(“Accept”, “text/html”); String body = request.fetch();

Page 17: How Much Immutability Is Enough?

/22@yegor256 17

trust

Page 18: How Much Immutability Is Enough?

/22@yegor256 18

monster objects

Page 19: How Much Immutability Is Enough?

/22@yegor256 19

reader = new Reader(); reader.setFile(“/tmp/names.csv”); reader.setFormat(“CSV”); reader.setColumn(3); reader.setAllCaps(true); reader.setCache(true); List<String> x = reader.read();

Page 20: How Much Immutability Is Enough?

/22@yegor256 20

object composition

Page 21: How Much Immutability Is Enough?

/22@yegor256 21

List<String> x = new CachedList<>( new AllCaps( // List<String> new OneColumn( // List<String> new CSV( // List<List<String>> new LinesFromStream( // List<String> new FileInputStream( new File(“/tmp/names.csv”) ) ) ), 3 ) ) );

Page 22: How Much Immutability Is Enough?

/22@yegor256 22

www.yegor256.com