16 ms·
Compared to the currently fashionable Go, Java has quite little boilerplate. Java 8+ has a number of quite powerful APIs in the standard library, some limited t
by nine_k 7y ago
Compared to the currently fashionable Go, Java has quite little boilerplate. Java 8+ has a number of quite powerful APIs in the standard library, some limited type inference, reasonable interfaces that allow default methods. The compiler has a well-defined custom processing interface (@annotations) which allows for powerful boilerplate-reducing things, from Lombok to Spring to JUnit.
80% boilerplate is a large exaggeration.
- icholy 7y agoIdiomatic Java has much more boilerplate than idiomatic Go.
- bdamm 7y agoif e3 != nil { return _, e3 } over and over and over..
- zzbzq 7y agoControl flow is not boilerplate, that's the application logic.
- nicoburns 7y agoIt's boilerplate compared to languages with exceptions where this code isn't necessary, or Rust where the equivalent code is: e3? ^ Go could very easily add this syntax, but haven't for some reason.
- deleted 7y ago[deleted]
- randomidiot666 7y agoSounds like you're not aware of the changes since Java 8 (2014). This is idiomatic Java nowadays: http://sparkjava.com http://sparkjava.com
- excursionist 7y agoIdioms change, java can be much less boilerplate today.
- disintegore 7y agoJava has generics
- ronnier 7y agoI write lots of Java and Go. I much prefer writing Java where I have things like "sets" and lambdas. I also find the type system to be a lot better in Java. In Go I'm often left wondering what I'm looking at and don't know unless I dive down into the source.
- johnisgood 7y ago> I also find the type system to be a lot better in Java. In Go I'm often left wondering what I'm looking at and don't know unless I dive down into the source. Could you please provide an example in both Go and Java? I am not quite sure that it has to do with the type system itself but your familiarity with the language and its libraries, and/or lack of basic functionality that should be in your text editor when you write Go.
- ronnier 7y agoGo: value := getValue() Java: int value = getValue()
- johnisgood 7y agoYou could do: var value int = getValue() Alternatively, use your text editor. For me, even Emacs displays the type when my cursor is on or around value, and if you put it on or around getValue(), you get its function prototype that includes its return type.
- ronnier 7y agonobody does that though. And that was the simplest example. Once you get into more complicated types things get even harder to understand.
- lossolo 7y agoFrom Java 10 this is valid Java also: var value = getValue() So it's almost exactly the same in both languages. It's not a problem for most IDEs, just hover your mouse over the value or use keyboard shortcut to get the type. This was never an issue for me in Java or Go.
- chopin 7y agoException handling and closing resources keep to be a major pain in the ass. Worst, the lambdas do not fit well with checked exceptions and the standard library is full of them. Another major pain are closing methods which throw checked exceptions themselves. This makes wrapping this into a decent lambda painful. I'd have preferred much lambdas just exposing checked exceptions they experience inside. I can't imagine why the compiler shouldn't be able to do this. Maybe one would have to come up with a fancy definition of functional interfaces (although I think this could be tackled with an annotation if the language doesn't provide this for lack of another keyword a la "throwsall").
- Quekid5 7y agoChecked exceptions should just be removed entirely from Java. It's a completely failed experiment. If you really want something like checked exceptions then (anonymous) intersection types or row polymorphism would be much better approaches than the adhoc thing Java does... but I suspect that that wouldn't well with the variance system in Java. (Thankfully it's only the Java compiler itself which imposes checked-ness, so other languages on the JVM thankfully don't have to deal with the insanity.)
- watwut 7y agoThere is nothing failed about checked exception. It is better then finding out about runtime exceptions as they happen.
- nine_k 7y agoIf an exception should be expected and handled, it's not an exceptional case, it's a part of the function's signature. The lack of sum types and pattern matching notwithstanding, such a signature can be implemented reasonably in Java.
- chopin 7y agoI am not against checked exceptions per se. Their excessive use of them in the standard library and especially the inability of lambdas in handling them are a problem imho. Take the stream close methods: There is absolutely no use in having those throwing a checked exception because there is nothing you can do. If closing is not possible you have problems with the underlying OS. That would rather warrant an error instead of an exception.
- throwaway894345 7y agoI suspect people see a lot of enterprise-y boilerplate (IAbstractFactoryBeans, for example), absurd inheritance hierarchies, and other things like getters and setters--they think these things are "part of writing Java" in a way that isn't "part of writing Go". In some sense, they're right--Java has more of a boilerplate culture even though it technically requires less boilerplate than Go. You can break from that culture (and as I understand it, Java is moving away from that boilerplate culture) to a certain extent, but you're still likely to have to use libraries that encourage it (by extending a ridiculous inheritance hierarchy, for example) and you'll probably end up working on teams that require a certain amount of boilerplate (e.g., mandates like "getters and setters for everything!").
- grey-area 7y agoSince Go doesn't even have inheritance or getters and setters, they may have a point (though getters and setters are pretty easy to do I guess if you want them, it's not part of the culture). Culture is really important in programming languages and I'm not sure it is something you can ever separate from the language itself, as for example the standard library dictates in large part the structure of your own code. I do agree though that there is no great reason Java has to be more verbose or more confusing than say Go.
- 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.
- catdog 7y ago> 80% boilerplate is a large exaggeration. If you restrict yourself fully to the standard library it's still not that largly exaggerated, even with Java 8. > The compiler has a well-defined custom processing interface (@annotations) which allows for powerful boilerplate-reducing things, from Lombok to Spring to JUnit. If you add Lombok + sth. like guava or apache-commons and in general choose modern libraries/frameworks it's really OK now. The only downside of Lombok is that it really maxes out what compiler(s) allow you to do which is not always without issues and it requires an IDE plugin to be installed.
- eeZah7Ux 7y agoDon't rattle the cage. Go is fashionable here thanks to Google's marketing.
- jerf 7y agoIf you're writing a lot of boilerplate in Go, you're either doing it wrong or chose it for something wildly out of its wheelhouse (i.e., numeric computing). If you're writing a lot of boilerplate in Java, you're either doing it wrong, chose it for something wildly out of its wheelhouse, or you choose frameworks or libraries that have either forced or afforded high boiler-plate code. The latter is probably the major difference that is what people are feeling. On paper Go and Java are virtually the same language, but just a few differences in the spec and a bit of focus by the developer community means that Go just generally has less annotations, external XML files for this, weird build systems, and a whole bunch of other frippery. Although I do think that it's easy to underestimate how much cleaner it turns out to be to do interfaces the way Go does rather than Java. It seems like such a small change, but it has a big effect on how much OO boilerplate you need. You don't have to guess what interfaces someone may someday need and write them all out in advance. And, I mean, in both languages, let's not underestimate the amount of "doing it wrong". There's a lot of us whose exposure to Java is being on the receiving end of a pile of Java code that is not written with maximal skill and precision. That's not really Java's fault. (To the extent it is, it is not uniquely so; I can tell that story about a lot of languages.)
- apta 7y ago> If you're writing a lot of boilerplate in Go, you're either doing it wrong or chose it for something wildly out of its wheelhouse Like shoehorning a set use case into a map? Or trying to get the list of keys or values in a map without writing out 5 lines of code? I find golang code to be much more verbose than Java. You find code littered with one off functions that amount to nothing more than map/filter/etc. operations which are 1 liners in Java.
- jerf 7y agoI find my code occasionally has those things, and that it is a minor inconvenience, not that it is littered with it. If it is littered with it, you are probably in the "doing it wrong" category. Do not try to force Go to do "functional programming". Or you're way out of scope. (I have lost count of the number of people I've counseled to not use Go if their first interest is numeric programming, for instance.) (I am underwhelmed by the crowd that insists map/reduce/filter is so important anyhow. I've used a substantial amount of Haskell. In Haskell, map/reduce/filter is the beginning of the functional style, not the completion of it. It goes beyond just "an alternate way to write for loops", which is most of what I see non-Haskell usage doing with it. In Go... just write the for loops. It's the same thing, just spelled differently. In Haskell, it's not the same thing.)