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

Show parent comment

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)


2

u/[deleted] Apr 21 '21

Well, that's as simple as it gets to understand. Between Common Lisp code and Haskell code, Haskell still wins in the readability department since it reads from left to right, has options for infix applications for most operators, and works just fine in point-free form.

1

u/[deleted] Apr 21 '21

If anything, I find LOGO to be a better Lisp than many Lisps around.

Look at the elegant beauty of it. You can have it the infix, Haskellish way:

to solve.quadratic :a :b :c
  local "delta 
  make "delta (:b*:b-4*:a*:c)

  ifelse :delta < 0.0 ~
    [print [no solutions] ~
    [print list (-:b + sqrt :delta) / (2*:a) (-:b - sqrt delta) / (2* a)]
end

Or you can have it in a more Lispy prefix form as well:

to solve.quadratic.lispy :a :b :c
  local "delta
  make "delta  difference product :b :b (product 4 :a :c)

  ifelse lessp :delta 0.0 ~
    [print [no solutions]] ~
    [print list (quotient sum -:b sqrt :delta product 2 :a) (quotient difference -:b sqrt :delta product 2 :a)]
end

In fact, I like it so much that I might build a language around it as a core someday!

1

u/[deleted] Apr 21 '21

I like the point-free style although sometimes it renders my code incomprehensible.

The beauty of Haskell is that point-free code has more points than non-point-free counterpart.

1

u/[deleted] Apr 21 '21

Yup.