4 ms·
> I do agree though that there is no great reason Java has to be more verbose or more confusing than say Go. Not a Go developer, but two things off the top of
by jimbokun 7y ago
> I do agree though that there is no great reason Java has to be more verbose or more confusing than say Go.
Not a Go developer, but two things off the top of my head:
1. Do not need to explicitly declare interfaces a struct conforms to.
2. Do not need to declare types of variables inferred on initialization.
3. Syntax for slice, map and struct literals.
I'm sure there's more...
- throwaway894345 7y agoI'm generally of the opinion that Go is less verbose than Java as well, but I'm of the impression that a lot has changed in Java since I last used it. It now has lambdas and other niceties, for example. Go has error boilerplate and lacks generics. Personally, I think Go's boilerplate is less of a big deal in general (Java's generics, lambdas, and exception handling come with their own issues which are often more significant than Go's boilerplate). Mostly I just don't think a debate about which language has less boilerplate will be very enlightening.
- nine_k 7y agoWith Lmobok, you can use `var` for local variables, and it will infer the type for you. It also can produce default constructors of various kinds. Can't have nice syntax for object literals, maps, slices, etc, though :( I mean, I have no hope for it to be introduced to the language any time soon, even if the desire is there among the language stewards.
- apta 7y ago> With Lmobok, you can use `var` for local variables `var` is part of standard Java, you don't have to use Lombok. > Can't have nice syntax for object literals Could you elaborate on what you mean by object literals in this context?
- nine_k 7y agoSome shops are still not Java 10, but Lombok helps you use `var` even there.
- nine_k 7y agoObject literals: think JavaScript. const foo = new Bar a = 1, b = 2 } With a default constructor or a builder (both provided by Lombok, maybe with other libraries, too) this level of succinctness is quite achievable in Java, too.
- apta 7y ago> Do not need to explicitly declare interfaces a struct conforms to. Which comes with its own (sometimes dangerous) downsides. > 2. Do not need to declare types of variables inferred on initialization. Java has `var` for quite some time now: var foo = new Foo<Bar>(); is valid. > Syntax for slice, map and struct literals I assume you mean overloading `[]` for slices and maps. This is a nice feature (C# and Kotlin already do this). But it doesn't reduce boilerplate by much in practice.
- throwaway894345 7y ago> Which comes with its own (sometimes dangerous) downsides. I don't buy this. I've heard the theoretical arguments to the contrary, but I've never (in 7 years of consistent Go use) heard of anyone being bit by this in practice--certainly not to any significant consequence. On the other hand, implicit interfaces materially benefit nearly every project in the Go ecosystem. I would make this tradeoff all day every day.
- apta 7y agoThere was an actual bug in the golang standard library precisely due to this feature, so it's far from theoretical. Secondly, I found that in practice, it doesn't buy you much, and on the contrary, makes working with code more difficult (even with an IDE). It's valuable information to know what interface(s) a type implements, not to mention finding all types down the hierarchy that implement a given interface (much more tedious and resource hungry with golang).
- throwaway894345 7y agoOne incident doesn't make it "far from theoretical". I concede that it's "vanishingly small", however. > Secondly, I found that in practice, it doesn't buy you much, and on the contrary, makes working with code more difficult (even with an IDE). I don't know about you, but I don't like having to write boilerplate to make a third party class implement a first party interface. > It's valuable information to know what interface(s) a type implements, not to mention finding all types down the hierarchy that implement a given interface (much more tedious and resource hungry with golang). How is that information valuable? The only thing I can think of is "I want to change the interface and consequently I need to update the implementations". In which case, just change the interface and the compiler will tell you what broke. As for "resource hungry", who cares? I'll trade 50ms of computing time per search (note that this is a generous figure) if it means developers don't need to maintain "implements" annotations and the corresponding boilerplate discussed above.
- grey-area 7y agoPersonally I think Java is more verbose because of culture and one big feature go does not have - inheritance. Indeed some regret has been expressed by the Java authors on inheritance. The things you mention don’t bother me as much as pointless ceremony like getters, factories, and insane object hierarchies, all of which are really optional. I quite like Go and use it a lot, but it is not IMO radically different from Java in syntax, except perhaps in what it leaves out (inheritance, exceptions, explicit declarations), and its culture of radical simplicity.