Every now and then, I tweet something like this, just to piss off some clean coders: https://twitter.com/lukaseder/status/885498470542577673 Apart from the obvious trolling factor (why can't I ever resist?), I do think there's something thought provoking in such a tweet. First off, given how rare break and continue statements are in Java code, many people probably … Continue reading Don’t Extract Everything Into a Method
Category: java
This category contains posts about things that are essentially Java-related
How I Incorrectly Fetched JDBC ResultSets. Again.
You know JDBC, right? It's that really easy, concise API that we love to use to work with virtually any database, relational or not. It has essentially three types that you need to care about: ConnectionStatement (and its subtypes)ResultSet All the other types some sort of utilities. Now, with the above three, we can do … Continue reading How I Incorrectly Fetched JDBC ResultSets. Again.
A Basic Programming Pattern: Filter First, Map Later
In recent days, I've seen a bit too much of this: someCollection .stream() .map(e -> someFunction(e)) .collect(Collectors.toList()) .subList(0, 2); Something is very wrong with the above example. Can you see it? No? Let me rename those variables for you. hugeCollection .stream() .map(e -> superExpensiveMapping(e)) .collect(Collectors.toList()) .subList(0, 2); Better now? Exactly. The above algorithm is O(N) … Continue reading A Basic Programming Pattern: Filter First, Map Later
ORMs Should Update “Changed” Values, Not Just “Modified” Ones
In this article, I will establish how the SQL language and its implementations distinguish between changed values and modified values, where a changed value is a value that has been "touched", but not necessarily modified, i.e. the value might be the same before and after the change. Many ORMs, unfortunately, either update all of a … Continue reading ORMs Should Update “Changed” Values, Not Just “Modified” Ones
10 Tips on How to be a Great Programmer
I was recently asked in an interview about my opinion on how to be a great programmer. That's an interesting question, and I think we can all be great programmers, regardless of our talent, if we follow a couple of rules that - I believe - should be common sense. In fact, these rules don't … Continue reading 10 Tips on How to be a Great Programmer
The Open-Closed Principle is Often Not What You Think it Is
jOOQ is a library that loves making everything internal final and package private. We have tons of classes like these: final class Concat extends AbstractFunction<String> { // ... } The class implements the semantics of SQL string concatenation. Clearly, you shouldn't need to tamper with it (or even know about it), because it is "protected" … Continue reading The Open-Closed Principle is Often Not What You Think it Is
A Nice API Design Gem: Strategy Pattern With Lambdas
With Java 8 lambdas being available to us as a programming tool, there is a "new" and elegant way of constructing objects. I put "new" in quotes, because it's not new. It used to be called the strategy pattern, but as I've written on this blog before, many GoF patterns will no longer be implemented … Continue reading A Nice API Design Gem: Strategy Pattern With Lambdas
Should I Implement the Arcane Iterator.remove() Method? Yes You (Probably) Should
An interesting question was asked on reddit's /r/java recently: Should Iterators be used to modify a custom Collection? Paraphrasing the question: The author wondered whether a custom java.util.Iterator that is returned from a mutable Collection.iterator() method should implement the weird Iterator.remove() method. A totally understandable question. What does Iterator.remove() do? Few people ever use this … Continue reading Should I Implement the Arcane Iterator.remove() Method? Yes You (Probably) Should
How to Write a Quick and Dirty Converter in jOOQ
One of jOOQ's most powerful features is the capability of introducing custom data types, pretending the database actually understands them. For instance, when working with SQL TIMESTAMP types, users mostly want to use the new JSR-310 LocalDateTime, rather than the JDBC java.sql.Timestamp type. In jOOQ 3.9+, this is a no brainer, as we've finally introduced … Continue reading How to Write a Quick and Dirty Converter in jOOQ
How to Prevent JDBC Resource Leaks with JDBC and with jOOQ
In a recent consulting gig, I was analysing a client's connection pool issue in a productive system, where during some peak loads, all the Java processes involving database interactions just started queueing up until nothing really worked anymore. No exceptions, though, and when the peak load was gone in the evening, everything returned back to … Continue reading How to Prevent JDBC Resource Leaks with JDBC and with jOOQ
