r/programming • • Feb 22 '15

Java Makes Programmers Want To Do Absolutely Anything Else With Their Time

http://www.forbes.com/sites/quora/2015/02/21/java-makes-programmers-want-to-do-absolutely-anything-else-with-their-time/
35 Upvotes

636 comments sorted by

View all comments

Show parent comments

1

u/btchombre Feb 22 '15 edited Feb 22 '15

Pattern matching just to cast an object that I know is a float to a float? That's ridiculous. Furthermore, making a cast more verbose doesn't change anything, and doesn't remove the "code smell". Pattern matching just makes the problem worse in this case as I have to write significantly more code.

Let me give you an example of the problem I had. I was using spark to read a row of data from a database. It returns an area of objects as the row, and I have to cast each column into its appropriate data type. Doing this in C# or Java or C++ is straightforward and simple, but using pattern matching for this is laughable, and using .AsInstanceOf[Type] is more verbose than bloody Java.

As far as operator overloading goes, that's great that I can do it, but its not very helpful when the standard library never uses the feature. I Should be able to compare Timestamps and Date objects without calling methods and without creating my own operators.

1

u/expatcoder Feb 23 '15

Ah, receiving a row of java Objects is akin to a row of Any, which is a fairly useless type as far as getting at the is-ness of the actual type.

In this case, if you know the real types, you could do something like:

val q = "some spark query"
q.exec.map{ // or however you invoke a query in spark
  case x @ (a:Long,b:Float,c:...) =>
}

this assumes that your spark query returns a List[TupleN]. You could also map match query results to case classes. Can't imagine the C#/C++/Java versions are much more concise; if so, please provide example(s), curious to see.

In Scala it's usually not the case that something can't be done, it's that there are usually multiple ways to do it (i.e. the kitchen sink problem).

As for comparing dates, dunno, use JodaTime if you can...