2

Philosophical Question:

An issue that occurred to me is that especially when using frameworks like Spring or Hibernate ( which are everywhere in industry), we have annotate everything.

This is better obviously then the declarative XML statements... but it also means that we are engaging in declarative programming since the engine (framework) is taking care of doing something for us. The engine itself may be written in Java (OOP), but we are essentially programming using declarations.

E.g. @Autowire - Inject my Component here, I don't care how you do it or what goes on in the background.

So is this development through annotations herecy? Or is it just the same thing as using configuration files (XML and properties).

Background Information:

"Declarative Programming in Java" - http://www.onjava.com/pub/a/onjava/2004/04/21/declarative.html

Vincent Savard
  • 1,906
  • 1
  • 17
  • 16
Menelaos
  • 267
  • 3
  • 14
  • Funny story. I never really used the XML annotations much, but obviously many did. When I was first learning JEE6 (I think that's the version when they went whole hog to annotations) I really liked the simplicity. But most of the class complained that they wanted "to code it". But by " code it" they meant "write the XML. Anyway, I'm more of an OO " purist" than many, and I think declarative programming, reasonably used, is absolutely O.K. and often wonderful. – user949300 Mar 31 '16 at 15:44
  • Among other things, OO Programming is about encapsulation, high cohesion, loose coupling, reusable components, programming to interfaces, and maintainable software design. I don't see anything about those annotations which violates any of that. Declarative programming as a technique is very much in-line with common OO principles because when used appropriately it can enhance many of the OO 'design values'. – Ben Cottrell Mar 31 '16 at 15:50
  • You should probably clarify that you mean using annotations to control application behavior. Annotations are used for a lot more than that in Java e.g. `@Override` `@SuppressWarnings` – JimmyJames Mar 31 '16 at 16:00
  • 4
    If you are a purist with regards to anything in programming then you should only stick to it as a hobby. Real problems are always going to be messy. – whatsisname Mar 31 '16 at 16:22
  • You may find this article about annotations interesting: [Java Annotations Are a Big Mistake](http://www.yegor256.com/2016/04/12/java-annotations-are-evil.html) – yegor256 Aug 02 '16 at 18:37

1 Answers1

4

I see no fundamental incompatibility between annotations or OOP. Annotations can be declarative (e.g. @Nullable - 'this thing has such and such properties'), which is not different from what interfaces or type systems do. Or they can define wiring/IOC (e.g. @Autowire) - which is built on top of OOP concepts in the first place.

Which isn't to say that annotations are always good - you can misuse them.

In particular, having 'hard coded' wiring with annotations ('give me that particular instance of an object') is no different (and no better) than a 'new': a tight coupling between two objects, and a code smell.

Code wisely.

ptyx
  • 5,851
  • 2
  • 22
  • 21
  • 2
    Are you saying that you write code that is completely free of the `new` keyword? – JimmyJames Mar 31 '16 at 16:13
  • 1
    @JimmyJames Of course not. Coupling is inevitable. Unnecessary coupling is bad. My point is that hiding unnecessary coupling behind annotations doesn't make it better. (Also, one of the ideas beyond code smells is that because it smells funny doesn't necessarily means it's anathema or that there is a better option). – ptyx Mar 31 '16 at 18:06
  • OK, I guess I have a different (maybe wrong) understanding of 'code smell'. – JimmyJames Mar 31 '16 at 18:10
  • 2
    @JimmyJames - in my view, all code smells - but you only have to fix it when it is ripe ;) To put it another way, coupling with new (or annotations) could be an indicator that there is a design problem, but not always. – HorusKol Mar 31 '16 at 23:24
  • 1
    @HorusKol You have a point. I've rarely seen code that I didn't think could be improved and I mostly look at my own code. – JimmyJames Apr 01 '16 at 00:16