Java Annotations Are a Bad Idea

24
/24 @yegor256 1 Yegor Bugayenko Java Annotations Are a Bad Idea

Transcript of Java Annotations Are a Bad Idea

Page 1: Java Annotations Are a Bad Idea

/24@yegor256 1

Yegor Bugayenko

Java Annotations Area Bad Idea

Page 2: Java Annotations Are a Bad Idea

/34@yegor256 2

14K+

1.2K+

250+ Why Me?

Page 3: Java Annotations Are a Bad Idea

/42@yegor256 3

takes.org eolang.org

“Elegant Objects”

Page 4: Java Annotations Are a Bad Idea

/42@yegor256 4

cactoos.org

Page 5: Java Annotations Are a Bad Idea

/24@yegor256 5

Object

Page 6: Java Annotations Are a Bad Idea

/24@yegor256 6

Object

Smth

Page 7: Java Annotations Are a Bad Idea

/42@yegor256 7

Maintainability suffers.

Page 8: Java Annotations Are a Bad Idea

/24@yegor256 8

Object

Page 9: Java Annotations Are a Bad Idea

/24@yegor256 9

class Storage { void save(@NotNull Book book) { // } }

storage.save(book);

w/annotation

Page 10: Java Annotations Are a Bad Idea

/24@yegor256 10

class Storage { void save(Book book) { // } }

storage.save(new NotNullBook(book));

w/o annotation

Page 11: Java Annotations Are a Bad Idea

/24@yegor256 11

class Storage { @RetryOnFailure boolean save(Book book) { // } }

boolean done = storage.save(book);

w/annotation

jcabi.com

Page 12: Java Annotations Are a Bad Idea

/24@yegor256 12

class Storage { void save(Book book) { // } }

boolean done = new RetryFunc<>( book -> storage.save(book) );

w/o annotation

cactoos.org

Page 13: Java Annotations Are a Bad Idea

/24@yegor256 13

class Storage { @Cacheable(forever = true) Book find(int id) { // } }

Book book = storage.find(123); storage.find(123);

w/annotation

jcabi.com

Page 14: Java Annotations Are a Bad Idea

/24@yegor256 14

class Storage { private final Func<Integer, Book> cache = new StickyFunc<>( id -> this.findById(id) ); Book find(int id) { return this.cache.apply(id); } Book findById(int id) { // .. } }

Book book = storage.find(123); storage.find(123);

cactoos.org

w/o annotation

Page 15: Java Annotations Are a Bad Idea

/24@yegor256 15

class Book { @XmlElement void getName() { // } }

String xml = marshaller.marshal(book);

w/annotation

Page 16: Java Annotations Are a Bad Idea

/24@yegor256 16

class Book { private final String name; String asXML() { return new Xembler( new Directives() .add(“book”) .add(“name”) .set(this.name) ).xmlQuietly(); } }

String xml = book.asXML();

w/o annotation

xembly.org

Page 17: Java Annotations Are a Bad Idea

/24@yegor256 17

class BookResource { @GET String index() { return “Hello, world!”; } }

// ???

w/annotation

Page 18: Java Annotations Are a Bad Idea

/24@yegor256 18

class BookFront implements Take { Response act(Request req) { return new RsText(“Hello, world!”); } }

new FtCli(new BookFront()).exec();

w/o annotation

takes.org

Page 19: Java Annotations Are a Bad Idea

/24@yegor256 19

class Book { @Inject private final Storage storage; String name() { return this.storage.sql( “SELECT name FROM book WHERE id=123” ); } }

Book book = container.get(Book.class); String name = book.name();

w/annotation

Page 20: Java Annotations Are a Bad Idea

/24@yegor256 20

class Book { private final Storage storage; Book(Storage s) { this.storage = s; } String name() { return this.storage.sql( “SELECT name FROM book WHERE id=123” ); } }

Book book = new Book(new Storage()); String name = book.name();

w/o annotation

Page 21: Java Annotations Are a Bad Idea

/24@yegor256 21

@Singleton class Storage { String fetch(String sql) { // .. } }

Storage storage = container.get(Storage.class); container.get(Storage.class);

w/annotation

Page 22: Java Annotations Are a Bad Idea

/24@yegor256 22w/o annotation

No Singletons!

Page 23: Java Annotations Are a Bad Idea

/24@yegor256 23

Object

Page 24: Java Annotations Are a Bad Idea

/24@yegor256 24