r/programming • u/nfrankel • May 09 '21
Teeing, a hidden gem in the Java API
https://blog.frankel.ch/teeing-java-api/57
u/Aurora_egg May 09 '21
Seems to be available in JDK 12. *cries in Java 8, just now being updated to 11*
32
May 09 '21 edited May 09 '21
Imagine how horrible it must be for the people who work on JDK<8 .
13
May 10 '21
Laughs in JDK 2 legacy application
4
May 10 '21
[removed] — view removed comment
1
u/BufferUnderpants May 10 '21
Or poor if it’s because they actually have no other prospects all things considered
43
May 09 '21
I didn't like Java until I discovered streams.
15
u/jared__ May 10 '21
The only problem with streams is readability. When done properly, it can be really clean; however, when overused, it becomes a chained nightmare.
11
2
May 10 '21
To be honest, I think that's just familiarlty. I hear the same argument about list comprehensions in Python and the debate basically splits around functional programmers & mathematicians on one side saying they can read it just fine, and everyone else on the other saying they can't read it.
5
u/de__R May 10 '21
I've written custom collectors using streams, and it's nothing to do with familiarity. It's to do with the fact that Java makes you do a lot of work with keeping track of classes and functions when the compiler could actually do a better job of it. If you want to custom collector functionality, it shouldn't be necessary to write 2-3 additional classes just to wrap the logic. It's extra work for the programmer that the compiler easily handle itself by generating the necessary code. They started to do this when they added lambdas in the first place, but why stop there?
1
-4
12
0
0
May 09 '21
[deleted]
15
u/nutrecht May 10 '21
I don't really get comments like these. You can just as easily switch to Kotlin if you want a more modern language without having to swap out the entire underlying ecosystem.
0
May 10 '21
[deleted]
13
u/Theemuts May 10 '21
Ah yes, because LINQ is famously compatible with Java /s
0
May 10 '21
[deleted]
5
u/Theemuts May 10 '21
Oh yes, just implement it, LINQ is also a famously tiny library...
0
-27
May 09 '21
I'll look at C# when it's properly cross platform so I don't have to use Unity for GUI.
14
May 09 '21 edited May 10 '21
MAUI (this is official)
Eto.Forms
Windows Forms with Mono (bad but it exists)
Avalonia (already mentioned)
2
0
0
May 09 '21
[deleted]
8
May 10 '21 edited May 10 '21
Huh,
haven'thaving never seen this before, it looks extremely impressive.Edit: uhhh, english, or something
0
May 09 '21
[deleted]
2
u/butt_fun May 10 '21
ReactiveX is honestly great. I'm really surprised I don't see it more often than I do, especially considering it exists in so many languages
0
May 10 '21
[deleted]
0
May 10 '21
[deleted]
7
May 10 '21 edited May 10 '21
How can I use Rx to write a query that pushes the filter down to the database as SQL so that only the relevant results are returned?
You're just mixing up two different concepts and complaining how they aren't the same. They're not the same by design. Your argument sounds a bit like: "Doors are much better than closets. I can walk through a door. Show me how I can do that with a closet. Closets have no place in a modern home."
5
u/iwasdisconnected May 10 '21
LINQ is two things and people are mostly occupied with the enumerable extension methods which I don't think is what's interesting about LINQ at all.
IQueryableis a lot more interesting as it has a lot of use cases outside of collection comprehensions. You can make a query provider that compiles a LINQ query into a SPIR-V shader. It's more commonly used to produce SQL though.LINQ, the "Language Integrated" part, is based on a feature of C# where a C# expression can be compiled into a abstract syntax tree.
Expression<Func<string, int>> stringLength = s => s.Length;This is valid C# code.
1
u/Kuinox May 10 '21
A local field expose an IEnumerable.
Well do a property that filter this IEnumerable
17
u/DoctorGester May 09 '21 edited May 09 '21
I understand the appeal and myself use streams extensively, but I’ve read the specific example given (the repository is linked in the article) and the same thing could be accomplished using the same amount/less lines of code and a for loop. Also the public constructor from a Map.Entry, ugh! I understand the example is meant to be simple, but it’s not exactly selling it imo.
-4
u/Cube00 May 10 '21
Streams are also slightly less performant then loops too, I used to use them everywhere until I discovered that so back to for loops for me in most cases.
20
May 10 '21 edited May 10 '21
This is a terrible reason not to use streams and indicative of cargo cult engineering. The overhead is almost guaranteed not to matter in your average Java application. Profile then optimize.
Here's Brian Goetz in 2014:
Of course, HotSpot has undergone major improvements in the meantime, narrowing the gap between loops and streams even further.
7
u/Muoniurn May 10 '21
Also, with big enough data, parallel() may easily overcome the slightly larger overhead, something which would be hard to do for a for loop.
0
May 10 '21
[deleted]
14
u/JoshiRaez May 10 '21 edited May 10 '21
The key lies in that it limits the way you can handle the data
A for loop can be straight forward, but just like a goto. The thing is that a stream just lets you deal with data in a certain way, and you are bound by these operations that ensure you will be able to do most of the possible work you would need, ensuring inmutability (if done right) in top of that. The legibility is just the icing on the cake
If you let people use for loops, theyll start doing stuff they can do in a for loop, are bad practices, but wouldnt be able to do in a stream
On top of that, it becomes just much more easier to reason and code streams rather than "freeform" loops. And this goes for the team, as well
0
u/Muoniurn May 10 '21
But is it really more legible when there is an if inside it inside another mini-for-loop which has 4 switches and it has some terrible attempt at logging because why not?
I mean, I get it, and sometimes a for loop is what you need indeed; but in practise whenever I stumble upon one of these massive for-monsters, I just wish they were stream, that line-by-line would be perhaps slightly slower to read, but it will usually be easier to understand the great picture.
24
u/rhbvkleef May 09 '21
Java's STL is really great. 90% of the time when you need something, it's in the STL, a further 9% will be available as packages.
54
u/rentar42 May 09 '21 edited May 09 '21
I've never heard Java class library referred to as STL before.
Also there are a couple of drawbacks in the java APIs as well. Time handling is one example that they attempted to solve three times (first Date, then Calendar and finally the java.time package) and only got right on the third attempt.
Edit: and I forgot my personal pet peeve: I'm convinced that java.util.Scanner was built with the express goal of looking useful but have just enough pitfalls that it will be guaranteed to haunt new developers in homework forever.
It looks like a simple way to get user input or parse text at first glance, but behaves so unintuitive in many ways that even experiences developers who do read the docs find it annoying.
28
u/chabala May 09 '21
I'm convinced that java.util.Scanner was built with the express goal of looking useful but have just enough pitfalls that it will be guaranteed to haunt new developers in homework forever.
I only see it used in undergrad assignments and automated coding challenges. There are probably better third party libraries for any use case you'd consider using Scanner for.
4
u/fatherseamus May 10 '21
High school teacher here. What should I teach them to use instead?
1
u/chabala May 10 '21
Depends on your use case. For simple user input in a beginners course, Scanner may be the right choice. But that use case doesn't really exist for professionals. You'd build a UI for user input, or just make CLI flags. If you're parsing command line arguments, there are libraries for that. The same goes for processing files and data piped in from standard input.
As an aside, do you teach your students about source control, or using Maven to integrate third party libraries?
15
u/vytah May 10 '21
Time handling is one example that they attempted to solve three times (first Date, then Calendar and finally the java.time package) and only got right on the third attempt.
That's miles better than languages that didn't even bother and remain broken.
Date/time support in JavaScript? Broken, it's basically java.util.Date.
Date/time support in C#? Broken, there's one DateTime type that's basically either java.util.Date or java.util.Date interpreted in a local time zone, and you can't even distinguish the two at compile-time.
Date/time in Go? If it was just like java.util.Date, then it would be less broken that it actually is. Behold this magnificent clusterfuck, the relevant part starts with words "How did Go solve that problem?".
Date/time in C? At least it can distinguish instants from their date/time representation, can convert time between UTC and the local time zone correctly, and has good formatting capabilities, but literally cannot do anything else. Something as basic as converting time between two timezones? Nope.
12
u/bloody-albatross May 09 '21
and only got right on the third attempt.
By AFAIK more or less copying Joda time. :D
34
u/rentar42 May 09 '21
Well, the author of joda time was one of the Co authors of java.time so no surprise there.
0
May 10 '21
[deleted]
1
u/midoBB May 10 '21
Shame some of us have to settle with old into new java time in the codebases because of vendor code. I hope my test suite never breaks on that util class because I don't even remember how it was implemented.
0
May 10 '21
[deleted]
1
u/tarkaTheRotter May 11 '21
Then whatever you do don't look at the official AWS lambda events library...
FFS. 🙄
3
u/Muoniurn May 10 '21
Well, it’s goddamn 25 years old, with almost perfect bacwards compatbility. And Dates just simply took the lazy way at the time and mimic POSIX date libs, which are still terrible. (Pun not intended)
2
u/nutrecht May 10 '21
I'm convinced that java.util.Scanner was built with the express goal of looking useful but have just enough pitfalls that it will be guaranteed to haunt new developers in homework forever.
Scanner wasn't meant to read input from the command line at all. Courses started using it because they felt it was more convenient than the regular BufferedReader stuff, but IMHO it really isn't.
Scanner is mostly useful if you're writing a simple parser. It's kinda bad for reading from System.in.
1
u/rentar42 May 10 '21
Scanner wasn't meant to read input from the command line at all. Courses started using it because they felt it was more convenient than the regular BufferedReader stuff, but IMHO it really isn't.
That's pretty much the issue: it's not well suited for that, but since it looks like it might with conveniently-named functions like
nextIntenough people think that's what it's meant to do thatnew Scanner(System.in)will sadly stay with us for a long time.1
u/nutrecht May 10 '21
You're saying that it was built with the purpose to confuse people. It's the teaching material that uses it poorly. It's not the 'fault' of whoever created the Scanner class.
-1
u/rentar42 May 10 '21
It's called making a joke.
Edit: also it is a little bit their fault. If you look at the very first example in the JavaDoc it shows how to instanciate a new Scanner using System.in.
40
u/Ameisen May 09 '21
Given that Java doesn't have templates, calling the Java standard library "STL" seems odd.
21
u/TheWix May 09 '21
I think they think it means "STandard Library" instead of "Standard Template Library"
17
u/well___duh May 09 '21
Probably easier if they used the “stdlib” name instead of confusing everyone by making up their own acronym
11
u/TheWix May 09 '21
Well, if it is out of ignorance then they don't realize they are using it incorrectly. I haven't written a line of C++ in over a decade so it didn't confuse me because I knew the context was Java, not C++.
If you write C++ regularly then I could see it being confusing.
1
-3
u/-Nyarlabrotep- May 10 '21
It's called the JRE.
2
u/rhbvkleef May 10 '21
No, the STL is the API that you can call, and see in the javadoc, the JRE is the runtime, the part that interprets bytecode.
1
u/-Nyarlabrotep- May 12 '21
That's completely wrong. What do you imagine "STL" is an abbreviation for? Do you think this is a concept that Java has defined?
Let's take a look at https://www.ibm.com/cloud/learn/jre, and you can find the same text repeated anywhere else that defines the JRE (emphasis mine).
"The Java Runtime Environment, or JRE, is a software layer that runs on top of a computer’s operating system software and provides the class libraries and other resources that a specific Java program needs to run."
So what you are erroneously referring to as "STL" is in fact the JRE.
1
u/rhbvkleef May 12 '21
No, the STL and JVM are parts of the JRE.
1
u/-Nyarlabrotep- May 12 '21
You don't understand the acronyms you're using. If you want to get really specific, then the class library portion of the JRE is sometimes referred to as the JCL (Java Class Library).
2
u/AttackOfTheThumbs May 10 '21
Common grammatical issue, just fyi.