News for hackers. Stuff that matters.
Re:Indirection Is Not Abstraction by zlynx on Thursday November 01, 2018 @03:00PM (#18357257)
Great, so now it's magic and no one knows how it works.

The last time I had to work on Java (more than ten years ago) it was easier to load the app into the debugger than to understand the source code.

That's f'd up.

Re:Indirection Is Not Abstraction by vbsteven on Thursday November 01, 2018 @03:21PM (#18357466)
And that was 10 years ago. We've come a long long way since then. Nowadays it would look something like this

    @Configuration
    public class ThreeConfiguration {
        
        @Bean @Profile("dev")
        public ThreeProvider devThree() {
            return new DevThreeProvider();
        }
        
        @Bean @Profile("production")
        public ThreeProvider prodThree() {
            return new ProdThreeProvider();
        }
        
    }
    
    @Component
    public class Sample {
        private ThreeProvider threeProvider;
        
        @Autowired
        public Sample(ThreeProvider threeProvider) {
            this.threeProvider = threeProvider;
        }
        
        public int addThree(int val) {
            return val + threeProvider.getThree();
        }
    }
 
And that's about it. The Sample class does not need to worry where it ThreeProvider comes from. And the configuration about all the different implementations and in which cases they will be used are bundled together in one class.
Re:Indirection Is Not Abstraction by TickleSteve on Thursday November 01, 2018 @03:38PM (#18357641)
...and the whole (non-java) software world cringes while the Java guys look puzzled wondering whats wrong with it.
Re:Indirection Is Not Abstraction by vbsteven on Thursday November 01, 2018 @04:04PM (#18357880)
Can you explain why this is so cringeworthy to non-java devs?
Re:Indirection Is Not Abstraction by JustSomeNobody on Thursday November 01, 2018 @04:18PM (#18358008)
1. There's Magic.

2. It is derived from the desire to create not applications, but application frameworks that can handle any conceivable change request that the PO might come with. This is architecture astronaut territory. It's frustrating to work with. It's hateful to debug.

Re:Indirection Is Not Abstraction by vbsteven on Thursday November 01, 2018 @04:52PM (#18358328)
1) where is the magic in the aforementioned code?

2) the ThreeProvider example is contrived indeed. In real projects this is used to provide different implementations for loggers, or sms gateways or other things that can differ between environments (prod, dev, test)

And to decouple the construction of objects from their usage. For example a rest api controller should not care how to instantiate a database connection and associated code, it should just require an implementation of a Repository interface for the model it uses.

Re:Indirection Is Not Abstraction by culturedsystems on Thursday November 01, 2018 @06:32PM (#18359062)
The magic is in all the code that interprets those annotations. So you have to understand a large and complex framework to understand which objects are being injected (in Spring, for example, you have to keep in mind the rules about which beans are instantiated and then which of the instantiated beans are chosen for injection into a particular class), and to understand what actually gets injected (is it actually the object returned from the annotated method, or some proxy or chain of proxies adding additional behaviour to those objects?).

The principle of dependency injection is a good one, but dependency injection frameworks are an unnecessary and overcomplicated way of achieving it. XML-based DI frameworks at least had a rationale - the idea was to externalise configuration, so you could change the dependencies without changing the code. It turns out this kind of externalisation isn't actually valuable in most cases (changing the dependencies is just as complicated and risky as changing code, and often needs to be coordinated with more substantive code changes, so the separation is artificial).

Once you determine that you should wire up your dependencies in code, though, there's no need for dependency injection frameworks - you can just directly write the code that wires up the dependencies.

Re:Indirection Is Not Abstraction by kovrik on Thursday November 01, 2018 @09:08PM (#18359939)
Can't see where this 'magic' comes from. IoC frameworks are not rocket science. Everything is very-well documented and pretty straightforward.

> dependency injection frameworks are an unnecessary and overcomplicated way of achieving it.

But why? I don't want to write my own DI framework for each new project.

I'd better add one dependency, put some @Autowired (and other well-documented) annotations and continue writing business logic.

Aforementioned 'magic' is basically creating a context, scanning all annotations, registering beans, instantiating them (calling constructors), then injecting them.

Re:Indirection Is Not Abstraction by smolder on Friday November 02, 2018 @09:14AM (#18362754)
As someone who recently worked on a spring project for the first time, it's pretty clear what magic we're talking about. I can't look at the code and know what the application does without first learning the meaning of a ton of different annotations and the behaviors connected with them. Annotations and the associated code are complex. Now that I understand what they all do, it's easy to reason about the app, but when I run it my mental model is still:

1. I launch the app

2. Spring does a bunch of stuff I only vaguely understand and can't understand from looking at the code

3. My actual code starts running

On top of that, all the spring stuff makes the memory footprint and startup time awful.

Re:Indirection Is Not Abstraction by drdeca on Friday November 02, 2018 @12:25AM (#18360691)
I wouldn't mind having things be created through autowiring and the like, if it didn't make it harder to make the things explicitly, without autowiring.

Ideally, making something automated should not make it harder to do the same thing manually.

Re:Indirection Is Not Abstraction by lmm on Friday November 02, 2018 @12:33AM (#18360720)
Modern Java does fix that much. If you look at the `Sample` class from the example, it's a normal class that you can instantiate the normal way (passing its dependencies into its constructor) and ignore the annotations on, unlike the bad old days of afterPropertiesSet().
Re:Indirection Is Not Abstraction by ummonk on Thursday November 01, 2018 @07:32PM (#18359434)
And here I thought your comment about how far we have come was sarcastic...
Re:Indirection Is Not Abstraction by culturedsystems on Thursday November 01, 2018 @06:01PM (#18358880)
Luckily, with modern dependency injection frameworks wrapping every object in layers of generated proxies, it's now impossible to understand it in the debugger, too.
Re:Indirection Is Not Abstraction by Splines on Thursday November 01, 2018 @04:16PM (#18357986)
Magic source is the worst. At the very least if you have something that is autogenerated from another file the name of the thing in that other file should be the same name that is used to do something with it.

Nothing worse than breaking into the debugger and seeing a "ThingAdjectiveVerb" and you need to actually look for "ThingAdjective" and look at the "VerbFoo" entry under it.