3 ms·
My biggest beef with idiomatic Java code is that it is so annotation-based. For example, I recently had to do a small CLI app in Java, and the command line pars
by joshlemer 7y ago
My biggest beef with idiomatic Java code is that it is so annotation-based. For example, I recently had to do a small CLI app in Java, and the command line parser library that came up in searches was picocli. Now, I mean no offence to the creators of picocli, it's pretty typical of most java code in my experience, but why does it have to do everything via annotations? Take their example for instance: https://github.com/remkop/picocli#example https://github.com/remkop/picocli#example
Not sure why a "just plain data" approach couldn't have been taken so that instead of:
@Option(names = { "-v", "--verbose" },
description = "Verbose mode.")
private boolean[] verbose = new boolean[0];
You could've had something more...
Option<boolean[]> verbose =
Option.<boolean[]>newBuilder()
.names("-v", "--verbose")
.description("Verbose mode.")
.default(() -> new boolean[0])
.build();
- eitland 7y agoBecause then people like the ones tearing down their strawman "Java is verbose" would be closer to having a point. Annotations work really well: they are easy to write, easy to write and typesafe enough.
- joshlemer 7y agoBut they are in my opinion really difficult to understand, test or customize. In the example above, the reified Option<boolean[]> can be passed around, transformed, inspected, invoked in tests, etc. The annotation approach weaves together the aggregate class (the overarching config class), the specific field of the class (`verbose`) and how that field should be parsed from command line arguments.
- eitland 7y agoOk. I can try to help: In this particular case, when you test you just set the field to what you want. In other cases where you actually want and need to test that the annotations works at runtime you use a test container (Arquillian was the big thing for integration testing JavaEE last time it was relevant to me, and since we are talking enterprise Java there's a fair chance that Arquillian will still work and maybe even still be the most popular solution.)
- mrkeen 7y agoI just cannot give any credence to annotations. @Transactional? @Stateless? @NotNull? In each case (and presumably many more) you can just go and write a unit test for each of these disproving them.
- huherto 7y agoAnnotations are frequently abused. They are useful in some cases but in other cases it would be easier to just call a function. It is more transparent and easy to understand, you don't have to figure out how the magic works when something fails.
- mrkeen 7y agoPlus you can bring the rest of your abstraction toolkit with you with plain functions. E.g. you can't put annotations into loops or conditions.
- vips7L 7y agoYou could have just kept searching? argparse4j has existed forever and its a port of python's argparse [0]. [0] https://argparse4j.github.io/ https://argparse4j.github.io/