Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java Annotations Are a Bad Idea

512 views

Published on

JDK.io 2017; 20 June 2017; Copenhagen, Denmark; video is here: https://youtu.be/cv23Z6xpwDw

Published in: Technology
  • Be the first to comment

Java Annotations Are a Bad Idea

  1. 1. /24@yegor256 1 Yegor Bugayenko Java Annotations Are
 a Bad Idea
  2. 2. /34@yegor256 2 14K+ 1.2K+ 250+ Why Me?
  3. 3. /42@yegor256 3 takes.org eolang.org “Elegant Objects”
  4. 4. /42@yegor256 4 cactoos.org
  5. 5. /24@yegor256 5 Object
  6. 6. /24@yegor256 6 Object Smth
  7. 7. /42@yegor256 7 Maintainability suffers.
  8. 8. /24@yegor256 8 Object
  9. 9. /24@yegor256 9 class Storage { void save(@NotNull Book book) { // } } storage.save(book); w/annotation
  10. 10. /24@yegor256 10 class Storage { void save(Book book) { // } } storage.save(new NotNullBook(book)); w/o annotation
  11. 11. /24@yegor256 11 class Storage { @RetryOnFailure boolean save(Book book) { // } } boolean done = storage.save(book); w/annotation jcabi.com
  12. 12. /24@yegor256 12 class Storage { void save(Book book) { // } } boolean done = new RetryFunc<>( book -> storage.save(book) ); w/o annotation cactoos.org
  13. 13. /24@yegor256 13 class Storage { @Cacheable(forever = true) Book find(int id) { // } } Book book = storage.find(123); storage.find(123); w/annotation jcabi.com
  14. 14. /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
  15. 15. /24@yegor256 15 class Book { @XmlElement void getName() { // } } String xml = marshaller.marshal(book); w/annotation
  16. 16. /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
  17. 17. /24@yegor256 17 class BookResource { @GET String index() { return “Hello, world!”; } } // ??? w/annotation
  18. 18. /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
  19. 19. /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
  20. 20. /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
  21. 21. /24@yegor256 21 @Singleton class Storage { String fetch(String sql) { // .. } } Storage storage = container.get(Storage.class); container.get(Storage.class); w/annotation
  22. 22. /24@yegor256 22 w/o annotation No Singletons!
  23. 23. /24@yegor256 23 Object
  24. 24. /24@yegor256 24

×