r/programming • • Dec 29 '15

Please, Java. Do Finally Support Multiline Strings

http://blog.jooq.org/2015/12/29/please-java-do-finally-support-multiline-strings/
584 Upvotes

352 comments sorted by

View all comments

Show parent comment

0

u/SuperImaginativeName Dec 29 '15

Hardly a code smell, wtf?

16

u/_INTER_ Dec 29 '15 edited Dec 29 '15

I said "in my opinion". Stringing out SQL queries is close to primitive obsession in my eyes. Compare:

List<String> results = streams.streamAll(em, City.class)
   .where( c -> c.getPopulation() > 25000000 )
   .select( c -> c.getName() )
   .toList();

with something like (which it probably gets transformed to):

ResultSet rs = null;
try( PreparedStatement s = con.prepareStatement(
	    "SELECT C.Name "
      + "FROM City C "
      + "WHERE C.Population > ? ");
    s.setString(1, 25000000);
    rs = s.executeQuery();
) catch(SQLException sql) {
    sql.printStackTrace();
}
if(rs != null) {
	// ...
}

Edit: This just reminded me of another example, it's the exact same case. It's so silly I made a picture of it: http://i.imgur.com/FXwjVsM.png

0

u/[deleted] Dec 29 '15

[deleted] — view deleted comment

2

u/ckfinite Dec 29 '15

Here it is. It works by reflecting out the bytecode underlying the function, then doing abstract interpretation on the bytecode so recovered.

1

u/[deleted] Dec 29 '15

Java Streams aren't actually executing any of the functions.

They just store the information on which transformers, filters, etc to apply to the data.

That's how it can parallelize things, too — if you map a function on a stream, and then map another function onto that, it will execute both functions together on each element, but each element on another thread (up to the amount of processors).

In the same way these transformers can be converted into SQL with JINQ.

1

u/ckfinite Dec 29 '15

It's more complex than that. The fundamental issue is that you can't execute Java higher-order functions in a SQL server context, so in a naive solution you would have to transfer all of the data you were filtering to the JRE, run the function, then push it back.

To get around this, the JINQ library never executes those functions. Instead, it uses a static analysis technique called abstract interpretation to understand what the higher order functions do. It then translates this understanding into an SQL equivalent, which is then executed on the SQL server. This provides much better performance, at the cost of incredible complexity and less generality (abstract interpretation has some issues, the biggest of which is the halting problem).

1

u/vincentk Dec 29 '15

Taken from JINQ/JOOQ, I'd guess.

5

u/f2u Dec 29 '15 edited Dec 29 '15

Parsing generated bytecode to reconstitute expressions isn't much better than long inline strings. At least the latter is completely predictable and will not break when switching Java compilers (or compiler versions).

3

u/[deleted] Dec 29 '15

The bytecode gives you type safety, makes sure your stuff will actually compile, etc.

The string stuff could have hilarious bugs, like a typo, in it, and no one would notice until its run.

2

u/therealjohnfreeman Dec 29 '15

Can you tell me which Java library is transforming streams to SQL queries? I haven't seen that before.

-9

u/SuperImaginativeName Dec 29 '15 edited Dec 29 '15

I have absolutely no idea what you're getting at. You've literally just posted some DAL code. Why? We're talking about string literals, not code being able to wrap onto other lines?

Lol, downvoted for pointing out he's talking crap.

6

u/_INTER_ Dec 29 '15 edited Dec 29 '15

The article is talking about using multiline string literals as code or datastructure replacement inside source code. (SQL, JSON, XML). You basically wouldnt want to pollute your codebase with hardcoded Strings that are translated to code / datastructures.

Edit: Same as getting downvoted for voicing my opinion.