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

13

u/[deleted] Feb 22 '15

I'm glad you mentioned! It is a really powerful language! I think everybody should try Scala and its community to feel the higher-level easy development and break the slave chains of big companies.

6

u/superPwnzorMegaMan Feb 22 '15

It has quite the learning curve though. I thought I'd just start with it and see where it takes me similar to my groovy experience, but it took me quite a whilst longer to create functionality.

I really like the type inference and the immutability, so I'll stick with it for bigger projects in favor of java.

What I did not like is that scala is currently impossible to mix with groovy, it can be mixed with java, but not groovy (at least not in the IDE). You can however let your scala classes handle interpreted groovy scripts, which is pretty cool. So I don't think it should be that hard to fix this problem, I might take a look at it when I have time.

1

u/btchombre Feb 22 '15

I like a lot of things about Scala, especially the imperative-functional balance that it has, but some things are just idiotic in my mind. Variable name, Colon, Variable Type is stupid and forces me to type an extra colon all over the place, which is contrary to their usual design pattern of "remove unnecessary bloat" like semi-colons. Also, casting from one type to another is painful using the .AsInstanceOf[Type] method, as this also breaks the "lets not be verbose!" mantra. Also, refusing to have operator overloading is another pain in the ass. Also, compiler errors are completely cryptic and not very descriptive at times. Once I switched to IntelliJ I realized that a large portion of my frustration with the language was due to Eclipse, but I feel like a few simple changes would make the language 100x better.

13

u/expatcoder Feb 22 '15

.asInstanceOf[Foo] is intentionally verbose. Why? Because it's a code smell, you're throwing type safety out the window. Pattern match instead.

refusing to have operator overloading is another pain in the ass

what on earth are you talking about? In Scala you can operator overload as you please: def >>(q: Query) = ..., def <*>[T](l: List[T]) = ..., etc.

Spend some time with the language, your complaints will turn to, "oh, now I see" instead of, "arggghhh, why isn't it like Java?"

p.s. Scala tooling and compile times suck compared to Java, no shortage of frustration there when starting out.

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...

3

u/[deleted] Feb 22 '15

Variable name, Colon, Variable Type is stupid and forces me to type an extra colon all over the place, which is contrary to their usual design pattern of "remove unnecessary bloat" like semi-colons

Like "val x = List(1, 2)"? Or "val rand = new Random()" What semicolons? :D

...using the .AsInstanceOf[Type] method...

implicit class As(a: Any) { def as[T] = a.asInstanceOf[T] } // Problem solved

5

u/azth Feb 22 '15

Like "val x = List(1, 2)"? Or "val rand = new Random()" What semicolons? :D

I presume he's referring to

def foo(x: Int, y: String): Bar

though I personally prefer having the colon there as it is a visually distinctive marker.

0

u/btchombre Feb 22 '15

I find the colon does nothing for me. You could use the same logic to say that Scala should mandate semi-colons at the end of statements as a "visual marker". I love that they made the semicolon optional, but then they just replace it with an equally redundant colon. It's not needed at all and makes the code visually more complex than it could to be.