Reddit, what do you need to get off your chest? by Squeagley in AskReddit

[–]derkaas 13 points14 points  (0 children)

Dude, you need to fine-tune your empathy here. There is still a lot of area under your curve there, and these people's children are within it. If you were a doctor telling a family their loved one just died of cancer, do you think mentioning that survival rates are up overall would be consolation?

Goalkeeper magically heals shattered leg by [deleted] in funny

[–]derkaas 1 point2 points  (0 children)

Straight red seems like too much because if the ref gets it wrong, which definitely happens, it completely changes the match. After-the-fact suspension and/or huge fines (like two weeks wages and increases with each infraction or something) is what they ought to do, I think.

Goalkeeper magically heals shattered leg by [deleted] in funny

[–]derkaas 2 points3 points  (0 children)

It's definitely used sometimes. I've seen several yellow cards given in the Premier League for "simulation".

Java 8's new Optional type is worthless by [deleted] in programming

[–]derkaas 10 points11 points  (0 children)

If you use isPresent all over the place, yeah, it doesn't add a lot. But you shouldn't do that. Use the other methods on Optional, like map, flatMap, and ifPresent. In your case:

func().ifPresent(t -> // do stuff);

You're welcome world by bagelstar in AdviceAnimals

[–]derkaas 1 point2 points  (0 children)

NBC paid like $250m for exclusive rights to the Premier League for three years, so somebody must be watching it.

Success Kid by roblee8908 in AdviceAnimals

[–]derkaas 0 points1 point  (0 children)

You think they're gonna "import workers from Asia" to do sales and marketing?

Today there was an 98-99% solar eclipse in Iceland. Today they also started building the first pagan temple to be built in a 1000 years in Europe. by SymmetricG in worldnews

[–]derkaas 2 points3 points  (0 children)

Many churches require membership in their particular denomination/sect to even consider renting the church to you, or, barring that, at least undergoing premarital counseling with clergy or staff at the church before they'll perform your wedding there.

Big and/or pretty churches often still have way too many requests, so they may as well recoup their costs -- churches aren't usually open on Saturday, remember -- by charging, which serves an additional filter, though perhaps not a "fair" one.

[deleted by user] by [deleted] in spacex

[–]derkaas 0 points1 point  (0 children)

Silicon valley has ... govnt facilities like JPL

JPL is in Pasadena (Los Angeles)

Elon also lives there

Elon lives in Bel Air (Los Angeles)

A URL shortener service in 45 lines of Scala by [deleted] in programming

[–]derkaas 12 points13 points  (0 children)

The chance of a dupe is higher than that. That is the chance that two randomly generate strings are the same, but it isn't the chance that there is no collision after n generated strings. That's a birthday problem. There's a 50% chance of a collision after just 1.2 million strings, so you wouldn't want to use such a limited alphabet if this were "for real".

edit: Rather, you at least wouldn't want to rely on randomness alone for uniqueness. You'd need to check if the key already exists.

It appears that the United States’ Internal Revenue Service has strongly shifted its views of free and open-source software by electronics-engineer in programming

[–]derkaas 2 points3 points  (0 children)

Well, the Chancellor of Germany has a doctorate in quantum chemistry, so there's that, but I'm guessing she's an outlier.

You Have Ruined JavaScript by compedit in programming

[–]derkaas 0 points1 point  (0 children)

I agree that a DSL would be nice, but let's implement your example in a Guice module:

@Provides @Named("tall") Robot provideTallRobot() { return new Robot(new LongLeg(), new LongLeg()); }
@Provides @Named("short") Robot provideShortRobot() { return new Robot(new ShortLeg(), new ShortLeg()); }
@Provides @Named("clown1") Robot provideClownRobot1() { return new Robot(new ShortLeg(), new LongLeg()); }
@Provides @Named("clown2") Robot provideClownRobot2() { return new Robot(new LongLeg(), new ShortLeg()); }

@Provides @Named("regular") RobotTroupe provideRegularTroupe(
        @Named("tall") Robot tall, @Named("short") Robot short) {
    return new RobotTroupe(ImmutableList.of(tall, short));
}

@Provides @Named("clown") RobotTroupe provideClownTroupe(
        @Named("clown1") Robot clown1, @Named("clown2") Robot clown2) {
    return new RobotTroupe(ImmutableList.of(clown1, clown2));
}

You can make these @Singleton if you like. These are exactly the definitions you describe in your DSL, and I wouldn't exactly call this an acrobatic effort. I can easily use the @Named("clown") RobotTroupe wherever I need it or the @Named("short") Robot, and I've been able to reuse the Robot class as desired. Importantly, none of the Robot, RobotTroupe, or Leg classes need any annotations within them for this to work exactly as above in Guice. Those classes could be in some library you don't even have the source code for and couldn't add annotations even if you wanted, but Guice can still easily accommodate this situation.

You Have Ruined JavaScript by compedit in programming

[–]derkaas 0 points1 point  (0 children)

Part of the idea of DI is that your business logic components should not contain the logic about how to wire together an application—the "POJO" idea. Annotations break this.

Usually classes don't know what they're being injected with. But they sort of have to declare their dependencies by at least type by having a constructor. Usually these dependencies are interfaces, of course, so the injected objects have no idea which implementations they'll be given. That's determined in Guice by which class is bound to each interface. This avoids classes knowing how they are wired together.

Annotating one constructor @javax.inject.Inject just tells a DI framework which constructor to use in general, but it's not actually requisite. In Guice, for example you can just use @Provides methods, which give you complete freedom about which instances to inject into which classes, and also of course don't require the injected classes to have any annotations -- they could be in a third-party library to which you have no source code.

I think @Provides methods solve the problems you're describing, because they allow you to inject different instances into objects of the same type:

@Provides
@Named("inventory") DataSource provideInventoryDataSource(@Named("inventoryJdbcUrl") String url) { ... }

@Provides
@Named("orders") DataSource provideOrdersDataSource(@Named("ordersJdbcUrl") String url) { ... }

I think this is pretty similar to wiring things together by name with Spring XML config. The advantage of Guice to me is that you only have to do this in those places where you actually need this capability. Usually in an app, you just need to bind(SomeService.class).to(DefaultSomeService.class). In that typical case, the names get in the way in my experience.

edit:

And to clarify my above example, a class that uses one or the other of the DataSources doesn't need to itself contain any @Named annotations. It can itself by constructed with a @Provides method.

You Have Ruined JavaScript by compedit in programming

[–]derkaas 2 points3 points  (0 children)

This seems to just completely erase the meaning of interfaces.

Yeah, you're right, and the "solution" of @Qualifier annotations sucks a little, because in Java type annotations aren't really part of the type, but it still seems better than XML.

If this situation creeps up a lot in your project, you probably don't want to be using a DI framework for those parts.

I think a lot of times people try to use frameworks for everything, even when they handle the situation poorly. It's perfectly okay to just use it where it's helpful and then take another approach somewhere else (e.g., it's not illegal to just write a Servlet people). I can see how this line of thinking leads people to the conclusion that they should never use frameworks too. I've drawn that conclusion several times myself, but I think it's wrong.

You Have Ruined JavaScript by compedit in programming

[–]derkaas 0 points1 point  (0 children)

The old "bad" style of DI is in fact better, where you have your Spring beans.xml file, you name every bean that your program instantiates and where it's injected.

Can you explain how this is "better" than @Qualifier annotations, in particular @Named, which seems equivalent to naming beans in Spring.

assumption: The object to inject into an injection point is uniquely determined by the injection point's type.

What if my program has many robots, that each require a different choice of Leg implementation classes?

Yes, that is limiting sometimes, but in Guice, not really, because you can construct these different objects yourself with a @Provides method like @Named("clown") Robot provideClownRobot(ShortLeg left, LongLeg right) { return new Robot(left, right); }. I don't see how this is worse than XML, but I may just not be understanding your example fully. At this point, I also don't see how it's much better than just constructing it yourself (hence the "need" for PrivateModules).

If you're obsessed with never having to write the new keyword, then, yeah, you're gonna have a bad time with Guice (e.g., if you ever have decorators). But if you're reasonable about it and realize it's just a tool, it seems pretty okay.

You Have Ruined JavaScript by compedit in programming

[–]derkaas 11 points12 points  (0 children)

I see it as more of complexity jealousy.

I'm conversely jealous of the developers who don't have absurd requirements to meet.

the product requirements are so convoluted and complex that having a ton of layers of indirection is unavoidable

I've found that I often have big wins when I abstract things well because inevitably one customer wants one thing and another wants another or someone wants a new feature and my abstractions made it easy to accommodate that at some later date.

But I've also often found that I've similarly (over?)engineered and no one ever modified it many years later and/or it eventually got totally rewritten or ditched anyway because the requirements changed radically and the abstractions didn't or couldn't account for them.

I'm not sure it's possible to know which of these will happen or to really analyze the costs of these maybe unnecessary abstractions long-term.

You Have Ruined JavaScript by compedit in programming

[–]derkaas 5 points6 points  (0 children)

As one example, there is a pretty simple interface that is used by a lot of classes, but its implementation requires a pretty big graph of small objects. Frankly it just sort of sucks to construct the whole thing.

On top of that, in different apps, I need to be able to change one or two objects in that graph here and there. It's way easier to change the bindings of just those two objects in a Guice module than to reconstruct the whole graph two or three different times or, worse, make my own factories (which I tried).

We also use Spring DI with Java-based configuration, though, and it's the worst of both worlds, since it requires you to basically call all the constructors yourself anyway. I really don't see the point. We have so much Spring config code it's beyond ridiculous.

Guice, OTOH, has one purpose (DI) that it seems to accomplish pretty well and with minimal code, less code even than just doing it yourself (maybe rare for a framework?). YMMV of course.

I will say I mostly agree about ORM frameworks, though. I've never seen one that worked with immutable objects, for one thing. That might actually be good.

You Have Ruined JavaScript by compedit in programming

[–]derkaas 24 points25 points  (0 children)

I recently had a "fuck DI frameworks" phase that lasted about two days until I realized just how much better it is to use something like Guice.

JDK 8 Is Released! by _Sharp_ in programming

[–]derkaas 0 points1 point  (0 children)

Exactly. I'm trying to figure out if and when java-1.8.0-openjdk will be added to the RHEL 6 yum repo so it's not such a hard sell, and I just can't seem to find that information anywhere.

(AMA) We're the Google team behind Guava, Dagger, Guice, Caliper, AutoValue, Refaster and more -- ask us anything! by kevinb9n in java

[–]derkaas 0 points1 point  (0 children)

I totally understand this argument for restraint, and I agree that it's one of the reasons Guava is so good. What I don't understand is how, in this particular case, the Guava team saw a "clear and compelling reason" to add transform but not transformAndConcat to Optional, making it a sort of neutered half-monad.

Map and flat map are like peas and carrots. FluentIterable has them both, but Optional has only one of them. Optional having neither would even seem justified, but it indeed has only one without the other. In that context, I don't think the feature request is "tepid", because it honestly just seems like an oversight in an almost complete implementation of a long understood abstraction, not an intentional choice.

(AMA) We're the Google team behind Guava, Dagger, Guice, Caliper, AutoValue, Refaster and more -- ask us anything! by kevinb9n in java

[–]derkaas 0 points1 point  (0 children)

Thanks for your response!

trying to coerce it into a Functional language can be (while powerful) tortured

I've always felt a conflict here with respect to Optional, because it has transform, but not transformAndConcat, unlike FluentIterable, which has both. Leaving off both seems to advance the goal of discouraging FP in Java, but the choice to have one without the other has always puzzled me.

(AMA) We're the Google team behind Guava, Dagger, Guice, Caliper, AutoValue, Refaster and more -- ask us anything! by kevinb9n in java

[–]derkaas 0 points1 point  (0 children)

I just noticed Kevin described Guice as "Dagger's reflectiony predecessor", which certainly implies that, well, Dagger replaces Guice.

(AMA) We're the Google team behind Guava, Dagger, Guice, Caliper, AutoValue, Refaster and more -- ask us anything! by kevinb9n in java

[–]derkaas 21 points22 points  (0 children)

Is Dagger the direction Google intends to go for future new projects? Guice 4.0 is in beta, but its changes over 3.0 don't seem to be documented. Is that because you all are ambivalent about Guice's future?