Live data from Hacker News

Javapocalypse

jz13.java.no

121–129 of 129 posts

Re: Javapocalypse

#121

This is irrelevant, but I just had my first Java experience yesterday. I decided to make an Android app, it's a simple remote control for hobby projects (Raspberry Pi/etc) that gives you a customizable UI with buttons that perform API calls. It'd be pretty simple to do in Python, which is the language I am proficient in, but Android development sounds fun so I wanted to get started in that. Creating the UI was easy e…

AndroidHttpClient has a executor method, which take your HttpGet object and a transformator and the default one will give you a string, which is the body of the request.

That is about 4 lines, and a try finally statement around that and a try catch statement around that again and that is all that is required. 15 lines maybe, a bit more if you want to move it of the main thread (but then you are, IMHO no longer just doing a get request).

Re: Javapocalypse

#122
post #67

Earlier quoted context omitted.

Type inference = the compiler being able to figure out what types your variables, function arguments, and return values are without you having to explicitly tell it. Polymorphism = the ability to write one function that works for multiple types. So for example, if you want to implement a sorting algorithm, you can write one function that is capable of sorting ints, floats, doubles, chars, and Strings. Java doesn't ha…

What you are describing (calling it overloading,) having different functions of the same name implement different algorithms based on types, is called "ad-hoc" polymorphism, but Java isn't limited to that, and it isn't the best way to do things, because you invariably end up duplicating parts of the algorithm. Java is very capable of writing parametrically polymorphic functions — on object types. They are called Gene…

BTW, Haskell only has (bounded) parametric polymorphism.

Don't confuse the two:

Bounded = Ad-hoc = Haskell type-classes.

Parametric = Universal = unconstrained type variables.

E.g. of parametric polymorphism: fst :: (a,b) -> a

E.g. of bounded polymorphism: show :: Show a -> String

Re: Javapocalypse

#123
post #68

This is irrelevant, but I just had my first Java experience yesterday. I decided to make an Android app, it's a simple remote control for hobby projects (Raspberry Pi/etc) that gives you a customizable UI with buttons that perform API calls. It'd be pretty simple to do in Python, which is the language I am proficient in, but Android development sounds fun so I wanted to get started in that. Creating the UI was easy e…

The standard method for getting around this in the java world is to create 'sane' libraries (in the case of android http, something like http://loopj.com/android-async-http/ ) and then just use the library. The strong point of Java is just how many libraries there are - you often don't need to touch the actual built in Java APIs. On the server side (non-android) this has gone even further through the use of new annot…

You can even get rid of getter/setters with annotations: http://projectlombok.org/

Re: Javapocalypse

#124
post #93

Earlier quoted context omitted.

DI will defer the injection to runtime instead of compile time, so you don't get the benefit of type checking during the compile. I've been bitten by this in the past with SpringMVC. It usually ripples up quickly and has never been an issue in a released app.

You must be talking about xml-configured DI. cf. Guice, which is compile-time checked: https://code.google.com/p/google-guice/wiki/SpringComparison

I really need to try that some time, thanks!

Re: Javapocalypse

#126

Earlier quoted context omitted.

Yeah, it's not a big risk to production, but it just makes the development loop that much more frustrating. There's nothing more annoying than building, deploying, starting, before discovering that you mispelt a bean name...

Ah! I see where my confusion lies. As others have commented, it sounds like you're talking about using a tool that does dependency injection for you; perhaps something configured in XML. I see how a tool like that would subvert a compiler's type checker. My mental model for dependency injection is a little different. I think of dependency injection as a technique instead of a tool. The only tool I used to do DI is a…

I don't think this can be called DI, because you're not injecting them, you're passing them to the constructor. The problem with this model is that you're polluting your constructors with non-functional arguments (for example the logging provider), and that if you want to change one of these, you have to change all your constructor calls!

Re: Javapocalypse

#127

Earlier quoted context omitted.

Ah! I see where my confusion lies. As others have commented, it sounds like you're talking about using a tool that does dependency injection for you; perhaps something configured in XML. I see how a tool like that would subvert a compiler's type checker. My mental model for dependency injection is a little different. I think of dependency injection as a technique instead of a tool. The only tool I used to do DI is a…

I don't think this can be called DI, because you're not injecting them, you're passing them to the constructor. The problem with this model is that you're polluting your constructors with non-functional arguments (for example the logging provider), and that if you want to change one of these, you have to change all your constructor calls!

> I don't think this can be called DI

Sure it can: http://en.wikipedia.org/wiki/Dependency_injection#Manually_i... http://www.martinfowler.com/articles/injection.html#Construc...

> because you're not injecting them, you're passing them to the constructor

Injection simply means that the dependency is sent from outside the class. That's exactly what's happening in the constructor example.

> The problem with this model is that you're polluting your constructors with non-functional arguments

I don't consider it "pollution." Who says that IDependency is non-functional? I don't think I've ever injected a non-functional dependency.

> if you want to change one of these, you have to change all your constructor calls!

This is a red herring. If you have to change all your calls, you failed to make your code DRY. That's the programmer's fault. Use a factory or default arguments.

95% of the time, the reason for injecting a dependency is that you want to use a fake implementation in your unit tests, but a particular concrete example in your production code. Have the default constructor setup the concrete dependency and use the extra constructor for your unit tests. This works most of the time for me.

    interface IDependency {
       void doSomething();
    }

    class Foo {
      Foo(IDependency) { ... } // for unit tests
      Foo() { this(new ConcreteDependency()); } // for production
    }
Doing DI manually requires a little bit of skill, but not much. I actually find that it teaches design principles more than it requires apriori knowledge of them. Having never used a configuration-based DI tool, it wouldn't be fair for me to conclude with any comparison between the approaches. However, given that this conversation started when you complained that DI tools subvert a static type system, I'm inclined to believe that the DI tools introduce accidental complexities that outweigh their benefits in the simplest use cases.

Re: Javapocalypse

#128

Earlier quoted context omitted.

I don't think this can be called DI, because you're not injecting them, you're passing them to the constructor. The problem with this model is that you're polluting your constructors with non-functional arguments (for example the logging provider), and that if you want to change one of these, you have to change all your constructor calls!

> I don't think this can be called DI Sure it can: http://en.wikipedia.org/wiki/Dependency_injection#Manually_i... http://www.martinfowler.com/articles/injection.html#Construc... > because you're not injecting them, you're passing them to the constructor Injection simply means that the dependency is sent from outside the class. That's exactly what's happening in the constructor example. > The problem with this model…

> I don't think I've ever injected a non-functional dependency

Logging is the classic example. Or the data service provider, or CXF-type stuff

> Use a factory or default arguments

I think that's the main point. Framework-managed DI puts a stop to the factory madness that you always ended up with back in the old Java or C++ world.

> Have the default constructor setup the concrete dependency and use the extra constructor for your unit tests

This will work for a while, but when you start needing to test complex flows, you're going to end up having classes calling classes calling classes, having multiple constructors will become unmanageable.

> DI tools introduce accidental complexities that outweigh their benefits in the simplest use cases

I totally, fully, definitely agree with you! The heavy tools such as Spring or EE6 are ugly, frustrating behemoths with absolutely no advantages for small or even medium developments. But as soon as it starts becoming a 40MY+ project I find you can't avoid them :(

Re: Javapocalypse

#129
post #79

The sad irony is that it is the termination of COBOL that could actually bring society to its knees. If Java died, it wouldn't take long for all that code to be rewritten in Go. But how many COBOL programmers do you know?

"...If Java died, it wouldn't take long for all that code to be rewritten in Go..." Guy... You don't just rewrite code that's been through the FDA approval process. It doesn't work like that. If you want to replace a java or c based medical imaging viewer... you can... but you have to prove it won't kill anybody. Or make any doctors cut off the left leg instead of the right one because a transform is flipped due to a…

I made the assumption that we would still have access to the original source code. So it wouldn't be so hard to translate Java to another modern language, e.g. one of the most modern being Go. On the other hand, so few people know COBOL these days that even with source code, translating those systems would take considerably longer.
Post reply on HN