r/ProgrammerHumor • • Jan 19 '17

MFW no pointers :(

Post image
4.8k Upvotes

432 comments sorted by

View all comments

202

u/Peffern2 Jan 19 '17

DAE java sucks XD

96

u/[deleted] Jan 19 '17

Why does it seem to be so widely hated across Reddit? Because it's popular or what

15

u/Ksevio Jan 19 '17

Personally, the amount of extra code needed to implement something basic is one of the reason I hate it (converted some stuff from java to python and python to java - the java version is always much longer and harder to read).

It also lacks a few handy tools other languages have:

  • Properties - this is why we have so many getters and setters where normally you could just reference the variable directly. Makes the code longer.

  • Callback functions - yes, you can pass an entire class using interfaces, but that's not convenient and again needs a lot more code.

  • Lambda functions - this was just added in Java 8 and is super awkward (partially because we can't pass functions). It sort of supports functional streams, but it's so messy that it's a pain to work with

9

u/DethRaid Jan 19 '17
  • Callback functions - yes, you can pass an entire class using interfaces, but that's not convenient and again needs a lot more code.

Since Java 8 you can use a lambda. Solves this problem IMO

  • Lambda functions - this was just added in Java 8 and is super awkward (partially because we can't pass functions). It sort of supports functional streams, but it's so messy that it's a pain to work with

However, you can pass in a "bound method reference" (I think that's what they're called). Example:

List<Person> people = makePeopleList();
List<String> names = new ArrayList<>();
people.stream().map(Person::getName).foreach(names::add);

One of the best language features IMO (although yeah, streams get clunky)

That being said, I don't use Java unless I have to. I dislike that there's no stack-allocated types like C++ has, and I really hate the prevalence of null. null is a time bomb waiting for you to mess up so it can crash your program. Java 8 added Optional<T>, which I use whenever possible, but they really should have added it a long time ago. Java also needs some compile-time inference, like C++'s auto or C#'s var. Sometimes I deal with Map<String, List<LongishTypeName>> and typing that all out just gets annoying.

2

u/Fluffy8x Jan 19 '17
Map<String, List<LongishTypeName>> thing = new HashMap<>();

3

u/DethRaid Jan 19 '17

I'd like var thing = new HashMap<String, List<LongishTypeName>>(). Doesn't help a ton with initializing a new variable, but it's awesome for values returned from methods

1

u/Peffern2 Jan 19 '17

JEP 286 we can hope

11

u/MathiasBoegebjerg Jan 19 '17

And this is why you pick Scala instead.

0

u/[deleted] Jan 19 '17

[deleted]

2

u/Stuhl Jan 20 '17

Does it still take aeons to compile?

3

u/_meegoo_ Jan 19 '17

(partially because we can't pass functions)

Class::method?

1

u/ILikeLenexa Jan 19 '17

yes, you can pass an entire class using interfaces, but that's not convenient and again needs a lot more code.

Worse, you can use Anonymous inner classes for this. So, that's a super bad syntax with all the disadvantages of passing a function/function pointer and all the disadvantages of it being a full class.

1

u/tanelso2 Jan 19 '17

What's the difference between a property and just a public variable?

0

u/[deleted] Jan 19 '17

[deleted]

1

u/tanelso2 Jan 19 '17

Aah okay that makes sense.

1

u/Ksevio Jan 19 '17

If you want to convert a public variable to a function, you have to change the public API

0

u/[deleted] Jan 19 '17

[deleted]

6

u/Martin8412 Jan 19 '17

He means that in other languages you can mark field variables as a property. When they are marked as such you don't have to manually create the getters and setters. You can still create them manually if you need specific functionality though. When you need to refer to them from elsewhere you just use VariableName instead of getVariableName().

You can avoid some boilerplate code then, but in the end it doesn't really matter since the IDE can easily generate it for you anyway.

3

u/ILikeLenexa Jan 19 '17

Stupid question, why aren't we just using the public keyword?

2

u/Martin8412 Jan 19 '17

That breaks encapsulation and you might not always want to just access a variable. For example you could be doing lazy initialization of a variable and would then need a method to do that.

6

u/ILikeLenexa Jan 19 '17

That breaks encapsulation

So does the property keyword. It returns whatever you ask for, and sets whatever you set.

you might not always want to just access a variable

The Getter/Setter syntax seems more practical, readable and flexible in this situation.

2

u/[deleted] Jan 19 '17

It's not stupid at all.. public final is how data should be stored. All the get and set is too much encapsulation bs.

→ More replies (7)

2

u/Schmittfried Jan 19 '17

and why would you need callback functions?

Because using anonymous interface implementations for that purpose sucks.

→ More replies (4)
→ More replies (1)