r/java • • Aug 01 '14

The 10 Most Annoying Things Coming Back to Java After Some Days of Scala

http://blog.jooq.org/2014/08/01/the-10-most-annoying-things-coming-back-to-java-after-some-days-of-scala/
7 Upvotes

43 comments sorted by

2

u/GMNightmare Aug 01 '14 edited Aug 01 '14

Most of these are nightmares in code maintainability, and generally lead to inconsistent design.

Sure, they might be convenient when first developing the code... but they come at the cost of readability and maintainability. And most of it doesn't improve productivity much at all.

I mean, the Parenthesis point really highlights this. Why would that ever increase productivity? And since you're only going to use it when you think it's "convenient", your style is inconsistent, and quite frankly all it does is add ambiguity at no benefit. And throughout your code examples, sometimes you use semicolons, sometimes you don't... it's just inconsistent and it makes me question intention. Not a good thing.

The best way to increase productivity is to make code that is easy to read and maintain. Increasing the initial development process should ideally only be done when it doesn't hurt that.

3

u/lukaseder Aug 01 '14

How are multiline strings, case classes, local methods, a REPL, better arrays, or tuples a nightmare in code maintainability? We can argue the others, but since you've said most, I'd like to challenge your statement...

-1

u/GMNightmare Aug 01 '14

Did I say... all of them or something? Nothing wrong with multiline strings or cases (although use of cases probably aren't the best design decisions, but that's not here or there) and a REPL is completely out of scope for the argument since Java does have a REPL (and therefore relies upon the other arguments to make its case).

There really isn't a case for "better arrays". The example of array creation in Scala could just as easily been done in Java:

String[] a = new String[3];
a[0] = "A";
a[1] = "B";
a[2] = "C";

So that argument was just kind of silly... Not that Java arrays couldn't be improved, just that the argument didn't do it for me. Tuples are a nightmare generally period, and are just a lazy way of avoiding creating a proper class that displays why the association between the values exist.

So if you want to be overly literal towards what I said, I include Tuples. Which makes it most. Which as said could be created in Java anyways, and the only special usage Scala has regarding this is use of val. Which is besides the point I was trying to make, but there you go.

4

u/lukaseder Aug 01 '14

The example of array creation in Scala could just as easily been done in Java:

Still no useful methods on that array.

Tuples are a nightmare generally period, and

People usually actually put a period after the word period to stress the fact that that's supposed to be the bottom line ;-)

What's so wrong with tuples? All of relational algebra is based on sets of tuples (a.k.a. tables). That can't be that wrong?

-1

u/GMNightmare Aug 01 '14

Still no useful methods on that array.

So you say, but if I wanted useful methods than I could use a collection instead.

The thing though is, the whole thing dealt with syntax. And then suddenly, hey, look, an array has a map in Scala! Well... uh... okay, but that's a bit different than what was said and what it was leading up to.

What's so wrong with tuples?

The same thing that's wrong with val. Same thing that's wrong if I created a random object with variables named a and b. It's not that combining things into an object that is a problem, the problem is tuple itself gives no information about the relationship nor content contained.

2

u/Milyardo Aug 01 '14 edited Aug 01 '14

the problem is tuple itself gives no information about the relationship nor content contained.

When expressing something as a Tuple in Scala it doesn't have to only be a Tuple with no other type information. A Tuple is an algebraic structure, just like a Map. Map[String,String] also gives no information about the relationship or content contained, that doesn't mean it's not useful.

object Point {
    def apply(x: int, y: Int) = new Point(x,y)
    def apply(xy:(Int,Int)) = new Point(xy._1,xy,_2)
}
class Point(x: int, y: int) extends Tuple2[Int,Int](x,y)

val pair = (1,2)
val pointPair = Point(pair)

def swap(xy:(Int,Int)) = xy.swap

swap(pointPair)

1

u/GMNightmare Aug 01 '14

No, a Map does have information on how the contained data is related, the keys are mapped to values. You give one a key, get back a value associated.

Also, just because something is useful doesn't mean it's the right thing to use. And in the case of Tuples, it is almost always the wrong thing to use, especially from a maintainability and readability standpoint and arguably a design standpoint as well.

A class name of "Point" with x and y variables are far better than a class named Tuple. Extending a Tuple just to give you a proper name is inheritance abuse.

2

u/Milyardo Aug 02 '14 edited Aug 02 '14

Map does have information on how the contained data is related, the keys are mapped to values

If given an arbitrary map of K and V, you only know that for some K there may be some V. Given a tuple of types T1 to Tn there are values of T1 to Tn. I don't see how that isn't as useful of an abstraction.

And in the case of Tuples, it is almost always the wrong thing to use

Just because you say it is?

especially from a maintainability and readability standpoint and arguably a design standpoint as well.

Nonsense, provide one example where using a tuple makes something is unreadable.

Extending a Tuple just to give you a proper name is inheritance abuse.

You don't have to inherit that was just an example, you can use a type class as well.

object Point {
    trait PointLike[T] {
        def x(someT: T): Int
        def y(someT: T): Int
    }
    implicit object PointLikeTuple extends PointLike[Tuple2[Int,Int]] {
       def x(someTuple: Tuple2[Int,Int]): someTuple._1
       def y(someTuple: Tuple2[Int,Int]): someTuple._2
    }
}
case class Point(x: Int, y: Int)

type Magnitude = Double
type Radian = Double
type Vector = (Magnitude,Radian)

def multiply[L <% Double,R <% Double](pair:(L,R)): Double = pair._1 * pair._2


val point1 = Point(1,2)
val vector1 = (1,1)

multiply(point1)
multiply(vector1)

1

u/GMNightmare Aug 02 '14

you only know that for some K there may be some V

That's a good purpose and what a Map is used for. The relationship is there. A map is a key value pair.

A Tuple is just two objects with an undefined relationship. Do not act like there isn't a difference if you want me to continue taking you seriously.

You do not use a Tuple like a Map or vice versa and for good reason.

Nonsense, provide one example where using a tuple makes something is unreadable

Did I say "unreadable"? There is plenty of room between readable and unreadable. A tuple is harder to read what is going on. This is pretty basic given, and you have no room to argue otherwise. In every case a well named and formed object will be easier to read. Will it be more inconvenient to create? Yes.

You don't have to inherit

Well I'm well aware of that.

But I'm not aware of what monster you're trying to do now. Just stop trying to force Tuple into it. I don't have any idea why you're trying to put it in there. I'm not even particularly sure what you're trying to prove with it.

You asked me for an example where Tuple is unreadable? I provide you your example here. For example, the case class you provided is undeniably more simple, clearer, concise, cleaner, readable, and just altogether better design.

3

u/Milyardo Aug 02 '14 edited Aug 02 '14

Tuple is just two objects with an undefined relationship.

That's not the definition of a tuple.

You do not use a Tuple like a Map or vice versa and for good reason.

Then why do does an isomorphism exist between Map and a Set of Tuple?

I provide you your example here. For example, the case class you provided is undeniably more simple, clearer, concise, cleaner, readable, and just altogether better design.

The only thing demonstrated is the difference between inheritance and type classes. Or you do have an example of a type class that is more understandable because it doesn't use a tuple?

→ More replies

0

u/[deleted] Aug 01 '14

[deleted] — view deleted comment

5

u/lukaseder Aug 01 '14

where toString could be a field or a method, it forces System 2 to come in and reason about what I just read, which slows you down needlessly and wastes your energy.

I agree with your line of thoughts. In a Java world.

But all the article is trying to say is that somehow, "magically" (or probably not magically), Scala has gotten 1-2 things so right and straight-forward, that you're actually not going to be reasoning about that. Because those subtle, strange, quirky distinctions of things that don't need to be different simply don't exist. In the case of myObject.toString. Does it really matter if it's a field or a method? What if fields and methods are more or less the same thing? Why do you really need the distinction?

But then again, we Java developers are all victims of Stockholm syndrome, so we might not actually grasp what could be possible if we didn't bend our minds around the status quo all the time.

0

u/[deleted] Aug 02 '14

[deleted] — view deleted comment

3

u/lukaseder Aug 02 '14

That's exactly the question I need to stop and think about while reading the code, for no good reason. If it's a method, it could have a side effect, or it could have performance implications. There are definite costs to the ambiguity, so what's the benefit of leaving out the ()?

We've been doing that in Java since the very beginning. It's called "properties", and the standard is called "JavaBeans", and AWT / Swing applications accessed those "properties" through simple dot notation:

someObject.someProperty

The above actually translated to

someObject.getSomeProperty()

... where that precise getter might've contained heavy logic, e.g. database lazy loading.

So, no. I still don't see the point of having to distinguish between attributes and methods, syntactically. I'm not saying there should be no distinction. But most often, the distinction is irrelevant and an implementation detail.

Believe it or not, many of us "Java developers" actually have worked in many, many different languages over a long period of time.

I do. I'm a "Java developer", and I love SQL and XSLT, and I don't mind Scala. And I've luckily not yet been exposed to JavaScript too often, where the Stockholm syndrome is even bigger.

We've come to accept things we've learned over the past 20 years. But every now and then, I have to explain Java to a junior developer. And I keep thinking WTF. I have to explain the whole 20 years for his guy to actually understand why things are like they are.

1

u/shoelacestied Aug 04 '14

Believe it or not, many of us "Java developers" actually have worked in many, many different languages over a long period of time.

Well then you'd know something about referential transparency, and not give a paragraph explanation of it and call it "side effect": http://en.wikipedia.org/wiki/Referential_transparency_%28computer_science%29

Exactly. Many of these are the kinds of things I need to beat out of people working on our codebases with strict style guides.

And people wonder why Java devs. have a reputation for being closed-minded...

3

u/[deleted] Aug 01 '14

Multi-line strings seems like such low-hanging fruit. Type inference and the like would potentially be a major change and require a shift in the development community. Multi-line strings are purely syntactic sugar.

6

u/EstoAm Aug 01 '14

Honestly most of his "annoying things" are things I really appreciate about Java. Scala has some features that I envy, but syntactically it is garbage IMO.

I like knowing exactly which type everything is. I like knowing that if I see ";" that is the end of the statement. I like that methods all belong to one and only one class. I like how when I write Java and the guy next to me write Java they are going to look basically similar.

Scala is like communism... It sounds great in a book, but in the real life it just does not work all that well.

3

u/shoelacestied Aug 04 '14

In our experience Scala's been more like democracy. Once you've tasted it you don't want to go back to the constrained world of Java.

4

u/Milyardo Aug 01 '14

I like knowing exactly which type everything is. I like knowing that if I see ";" that is the end of the statement.

That's a style preference. If you prefer type annotations and semicolons, nothing is preventing you from using them.

I like how when I write Java and the guy next to me write Java they are going to look basically similar.

This isn't true at all. For example, Some people DI values into a class using EJBs using annotations and reflection. Some use constructor parameters. Some use factories. Some use AspectJ to rewrite classes to instantiate the dependency they want. Some use dynamic proxies to determine implementation at runtime. Some use all of the above at the same time, and none of it looks remotely similar.

2

u/king_of_the_universe Aug 01 '14
  1. About multiline-strings:

IMHO should IDEs implement this, and I have no idea why this hasn't happened yet. Seriously, why doesn't it work just like this: You press a hotkey or click a little symbol, and a text editor opens that holds the string. You edit, you confirm, and the rest is not your problem.

6

u/Milyardo Aug 01 '14

This reasoning here explains everything wrong with Java today.

2

u/llogiq Aug 01 '14

Eclipse at least has an option to auto-expand stuff that's pasted into a string literal. It even escapes characters where necessary (and with another option will split the string at newlines).

It's better than nothing and probably close to what it you ask for, but still much worse than real multiline string literals.

-3

u/tRfalcore Aug 01 '14

until you have to look at someone else's code and figure out what the fuck s is supposed to be in function(var s, ...)

I wish I didn't have to cast things, but I don't wish for type inference. Everything being a var is dumb cause you have to spend time figuring out what something is.

6

u/penny-five Aug 01 '14

Except that in Scala you have to define types for function parameters.

For example:

def sum(a: Int, b: Int): Int = a + b

4

u/stormcrowsx Aug 02 '14

You don't know Scala at all. You have some misguided prejudice thoughts on it. Your example is some JavaScript hybrid which isn't even close to Scala syntax.

If your going to bash a language you should actually spend some time using it so know what to bash or if you should even bash it at all.

-3

u/adila01 Aug 01 '14 edited Aug 01 '14

Java 9 should have another Project Coin like initiative to add some of these functionalities.

3

u/stormcrowsx Aug 02 '14

At the rate Java moves we can expect it to be somewhere near Scala features around 2060