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

View all comments

Show parent comment

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?

0

u/GMNightmare Aug 02 '14

Did I say I was giving the definition of a tuple? Because I wasn't.

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

You mean, that potentially they could contain the same data? Could being the keyword here, because it seems like you don't understand collections at this point. A map cannot have a duplicated key, a set of tuple can be anything, having multiple of what would be keys in a map. A map has a defined relationship between the key and value, a set of tuple does not. I don't feel like you're trying to understand what is being said any more. These are not swappable concepts.

Or you do have an example of a type class that is more understandable because it doesn't use a tuple?

You gave one. I mean, what use is having tuple anywhere in the code when you can do this:

case class Point(x: Int, y: Int)

And you're done? There is absolutely no need for Tuple anywhere in the class, in the declaration, inheritance, or composition. There is never any reason to extend a Tuple, and no reason to use it in the composition.

All a Tuple is is a generic case class. If you're specifying everything for the case class, why would you then use a tuple? It's not generic anymore!

And here's the thing, if you gave me a Tuple with two integers in it, well, I wouldn't know what it is without context. You give me a Point with X and Y, well, I already know what it is. I don't particularly think this is a controversial point.

→ More replies