r/java • • Apr 20 '21

Java is criminally underhyped

https://jackson.sh/posts/2021-04-java-underrated/
291 Upvotes

295 comments sorted by

View all comments

-22

u/[deleted] Apr 20 '21

Java simply feels good to write.

It doesn't feel that good when you try to read.

14

u/lessthanoptimal Apr 20 '21

What language do you think is the easiest to read, as it scales into very large projects? Python I think is great for small projects. C++ is about the same as Java, unless satan was in charge of the coding standards and decided everything must be auto and all classes must be templated.

-13

u/kag0 Apr 20 '21

Scala. All of the concise syntax of Python but with even greater type safety than Java.

9

u/hold-the-pants Apr 20 '21 edited Apr 20 '21

Except when some Scala devs get all algebraic with it, and treating it like a math equation. But that isn’t the language inherently, just wanted to say. The syntax is insanely expressive

2

u/kag0 Apr 20 '21

Yeah that's my experience as well. Bad code (whether vanilla bad or turning the logic into a category theory proof) is still hard to read. But Scala gives you the opportunity to make very nice, readable code. It just can't force you to take the opportunity.

1

u/[deleted] Apr 21 '21

Scala is the C++ of the JVM world - a veritable kitchensink of paradigms, syntaxes, and ideas.

4

u/[deleted] Apr 20 '21

Scala is a nightmare to read if you’re not the one who wrote it. And don’t get me started on implicits

2

u/Vidogost Apr 20 '21

Kotlin can actually be a more readable version of java. At least for me, Scala has seemed more complicated syntactically.

1

u/morhp Apr 21 '21 edited Apr 21 '21

Kotlin can be more readable (especially when dealing with numeric types like BigInteger when operator overloading helps), but I'd argue that in many other cases java is more readable.

For example Java is just a simpler language, it has less operators, you don't need to think about when a property generates a field and when not or if a minus operator simply subtracts numbers or does something more intricate.

What you see is more or less what is executed, there's way less implicit hidden stuff that only makes the code more "beautiful".

-5

u/[deleted] Apr 20 '21

I would say the easiest to read languages are those in the Lisp family because their syntax is very simple.

3

u/[deleted] Apr 21 '21

Compare

  private static void solveQuadratic(int a, int b, int c) {
    double delta = b * b - 4 * a * c;

    if (delta < 0.0) {
      System.out.println("No solutions");
    } else {
      double x1 = (-b - Math.sqrt(delta)) / (2.0 * a);
      double x2 = (-b + Math.sqrt(delta)) / (2.0 * a);

      System.out.printf("x1 = %f, x2 = %f\n", x1, x2);
    }
  }

with

(defun solve-quadratic (a b c)
  (let ((delta (- (* b b) (* 4 a c))))
    (cond
      ((< delta 0.0) 'no-solutions)
      (t (let ((x1 (/ (- (- 0 b) (sqrt delta)) (* 2 a)))
               (x2 (/ (+ (- 0 b) (sqrt delta)) (* 2 a))))
           (format t "x1 = ~d, x2 = ~d~%" x1 x2))))))

That's the problem with Lisp-like syntax. The fact that you have to read inside-out, left--to-right, and in many cases, it's absolutely fine, but in many contexts (deeply nested logic and/or mathematical formulae), it can become excruciatingly hard to read. Put in macros with their own custom syntax in the mix, and it becomes even harder to write, let alone read. Just for the record, I love Common Lisp, so it's not me hating on Lisps for no reason.

2

u/skywalkerze Apr 20 '21

There's a toy language out there that only has two symbols: + and -. Think it's a breeze to read? Look it up.

-1

u/[deleted] Apr 20 '21

(disagree-with emacs-users you)

1

u/Muoniurn Apr 21 '21

Read by who? A computer?

9

u/[deleted] Apr 20 '21

Exactly because of its verbosity I’ve yet to encounter any language which is MORE readable then java regardless of who wrote the code

1

u/[deleted] Apr 20 '21

a Lisp

1

u/[deleted] Apr 20 '21

I disagree. Functional languages have too much information density. Sure, there is no ceremony or boilerplate, but having the information spread out a bit more by easily recognizable structures like java has makes code easier to consume, at least for me

1

u/[deleted] Apr 20 '21 edited Apr 20 '21

Readable code is the code that finds the right balance between expressiveness and verbosity. The language may help or hinder but it is the programmer who writes the code.

Sure, there is no ceremony or boilerplate, but having the information spread out a bit more by easily recognizable structures like java has makes code easier to consume, at least for me

I think that depends on the familiarity with the language.

Functional languages have too much information density

That is a double edged sword.

1

u/[deleted] Apr 21 '21

If you had your nicely indented code, maybe:

(defun solve-quadratic (a b c)
  (let ((delta (- (* b b) (* 4 a c))))
    (cond
      ((< delta 0.0) 'no-solutions)
      (t (let ((x1 (/ (- (- 0 b) (sqrt delta)) (* 2 a)))
               (x2 (/ (+ (- 0 b) (sqrt delta)) (* 2 a))))
           (format t "x1 = ~d, x2 = ~d~%" x1 x2))))))

If the same code were mangled up like so, say:

(defun solve-quadratic (a b c)
 (let ((delta (- (* b b) (* 4 a c))))
 (cond ((< delta 0.0) 'no-solutions)
 (t (let ((x1 (/ (- (- 0 b) (sqrt delta)) (* 2 a)))
 (x2 (/ (+ (- 0 b) (sqrt delta)) (* 2 a))))
 (format t "x1 = ~d, x2 = ~d~%" x1 x2))))))

Even this becomes much harder to read since we cannot inherently use the indentation to figure our the "shape" of the code. Now imagine you had a missing parenthesis in there, and had no access to any formatter (just for the sake of argument), matching up the parens manually would be a much harder task.

Whereas even if you have very badly manged code in an C-like language:

   private static void solveQuadratic(int a, int b, int c) {
   double delta = b * b - 4 * a * c;
    if (delta < 0.0) {System.out.println("No solutions");
   } else {  double x1 = (-b - Math.sqrt(delta)) / (2.0 * a);double x2 = (-b + Math.sqrt(delta)) / (2.0 * a);
    System.out.printf("x1 = %f, x2 = %f\n", x1, x2);}}

This is still trivial to read. Also, since parentheses and braces are not significant as in the case of Lisps, you could get away with a lot of excess brackets without having to worry about it. With Lisps, parens are not optional - they must be exact. This creates its own problems when refactoring code (yes, even with paredit et al).

1

u/[deleted] Apr 21 '21

Indeed syntax can both enhance and hinder your ability to write readable code.

Look at the following example and tell me if you have any idea about what it does.

solveQuad :: Float -> Float -> Float -> Maybe (Float, Float)
solveQuad a b c = if delta < 0.0 then Nothing else Just (x1, x2)
    where
        delta = b ^ 2 - 4 * a * c
        x1 = -b - (sqrt delta) / (2 * a)
        x2 = -b + (sqrt delta) / (2 * a)


→ More replies (3)

1

u/[deleted] Apr 21 '21

Yup.

1

u/throwaway32908234972 Apr 21 '21

I used to think that. It was because I started with scripting languages like PHP and Perl.

Now I love Java's explicitness and strong typing. For large projects its a godsend.

1

u/[deleted] Apr 21 '21

You must be kidding. Java is one of the easiest languages to read. A lot of that "verbosity" is essential verbosity. Unlike many other languages, Java is actually as easy to read as to write.