Java Annotations Are a Bad Idea

Post on 22-Jan-2018

405 views 1 download

Transcript of Java Annotations Are a Bad Idea

/24@yegor256 1

Yegor Bugayenko

Java Annotations Area Bad Idea

/34@yegor256 2

14K+

1.2K+

250+ Why Me?

/42@yegor256 3

takes.org eolang.org

“Elegant Objects”

/42@yegor256 4

cactoos.org

/24@yegor256 5

Object

/24@yegor256 6

Object

Smth

/42@yegor256 7

Maintainability suffers.

/24@yegor256 8

Object

/24@yegor256 9

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

storage.save(book);

w/annotation

/24@yegor256 10

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

storage.save(new NotNullBook(book));

w/o annotation

/24@yegor256 11

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

boolean done = storage.save(book);

w/annotation

jcabi.com

/24@yegor256 12

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

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

w/o annotation

cactoos.org

/24@yegor256 13

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

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

w/annotation

jcabi.com

/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

/24@yegor256 15

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

String xml = marshaller.marshal(book);

w/annotation

/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

/24@yegor256 17

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

// ???

w/annotation

/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

/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

/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

/24@yegor256 21

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

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

w/annotation

/24@yegor256 22w/o annotation

No Singletons!

/24@yegor256 23

Object

/24@yegor256 24