News for hackers. Stuff that matters.
Re:Kotlin vs. Java by jermaustin1 on Monday December 16, 2019 @04:45PM (#21807604)
I don't know if I like the "Elvis Operator" - I feel its one too many conditions on a single line.

And the ternary operator is just odd when there is a more terse "standard" approach to it.

Re:Kotlin vs. Java by tjohns on Monday December 16, 2019 @04:51PM (#21807666)
The Elvis Operator is particularly useful because it's such a common pattern in Kotlin, since the language enforces null safety. When you're using a legacy Java API without nullability annotations, the language forces you to explicitly handle a null result.
Re:Kotlin vs. Java by benibela on Tuesday December 17, 2019 @06:18AM (#21812524)
No, in that case it does not enforce it

The Java types become platform types

Re:Kotlin vs. Java by vinceve on Monday December 16, 2019 @05:47PM (#21808305)
It masks the complexity of your statement. I know in ruby we had the same issues with the “try()” method. Our rule was, do or do not, there is no try. :)

More info: https://karolgalanciak.com/blog/2017/09/24/do-or-do-not-ther...

Re:Kotlin vs. Java by zmmmmm on Monday December 16, 2019 @05:53PM (#21808385)
The beauty of the ternary operator is that it isn't one. It's just taking advantage of the fact that if/else is an expression in Kotlin and it turns out that when if/else yields a value, a dedicated ternary operator is redundant. It's one of a small number of things that I like better about Kotlin than Groovy.
Re:Kotlin vs. Java by Someone1234 on Monday December 16, 2019 @06:02PM (#21808477)
That's actually a really clever (yet simple) design. Have other languages done this before?

Now I cannot "unsee" that concept. Why isn't if/else an expression in other languages that just yields a value?! That's a powerful idea.

Re:Kotlin vs. Java by virtualwhys on Monday December 16, 2019 @11:11PM (#21810471)
> Have other languages done this before?

Well, yeah, Scala obviously, the primary language from which Kotlin drew inspiration for its syntax and features.

Re:Kotlin vs. Java by Drakar1903 on Tuesday December 17, 2019 @06:18AM (#21812522)
I wonder why people don't frame Kotlin as the middleman that takes the good parts of both Java and Scala.
Re:Kotlin vs. Java by cesarb on Monday December 16, 2019 @06:19PM (#21808636)
> Have other languages done this before?

LISP. For a more recent example, Rust.

Re:Kotlin vs. Java by int_19h on Monday December 16, 2019 @06:30PM (#21808705)
It's very common for languages to adopt an everything-is-an-expression concept, and then make if/else the conditional operator.
Re:Kotlin vs. Java by umanwizard on Tuesday December 17, 2019 @08:48AM (#21813364)
Rust has this, as well as some other nice ML-inspired[0] features, like structured bindings.

That’s one thing I like about Rust: it fuses functional and imperative styles very naturally.

[0] “ML” here meaning the family of languages including SML and OCaml, which inspired (to varying degrees) Haskell, Rust, some parts of modern C++, and surely others. Not “machine learning”.

Re:Kotlin vs. Java by nepeckman on Monday December 16, 2019 @11:01PM (#21810410)
Nim does this as well. Almost any block statement (including creating a new scope with `block`) can be put on the right hand side of an assignment. Whatever value is returned from the block is the result of the expression. All lisp inspired, of course, but with a Pythonic syntax.
Re:Kotlin vs. Java by barryhoodlum on Tuesday December 17, 2019 @02:43AM (#21811519)
Ocaml does this too.
Re:Kotlin vs. Java by saagarjha on Monday December 16, 2019 @04:54PM (#21807717)
On the contrary, I consider the "Elvis operator" to be one logical operation: give me this value, or the other one if this one is invalid. What's the more terse way to write the example with a ternary operator?
Re:Kotlin vs. Java by leeman2016 on Tuesday December 17, 2019 @04:41AM (#21812088)
One obvious advantage of the "if-else" approach is using indefinite "else if" conditions rather than just one "else"
Re:Kotlin vs. Java by dajohnson89 on Monday December 16, 2019 @04:47PM (#21807624)
It feels like a common sentiment that concise code == good code
Re:Kotlin vs. Java by mjcohen on Monday December 16, 2019 @05:29PM (#21808110)
APL may be a counterexample, at least when trying to understand what you have written a few months later.
Re:Kotlin vs. Java by driver733 on Monday December 16, 2019 @04:52PM (#21807673)
To be more precise, I would say that expressive code == good code
Re:Kotlin vs. Java by martin1975 on Monday December 16, 2019 @04:53PM (#21807704)
As long as expressive doesn't mean too terse and somewhat readable to most...lest you commit perl's sins.
Re:Kotlin vs. Java by dajohnson89 on Monday December 16, 2019 @04:55PM (#21807729)
Yeah. This was the point I was halfheartedly making -- concise code for the sake of concise code is a dogma that needs to go away.
Re:Kotlin vs. Java by rongenre on Monday December 16, 2019 @04:57PM (#21807749)
Well, more to the point - bugs / LOC is remarkably constant in real code across coding languages, so one of the best things you can do is reduce LOC.
Re:Kotlin vs. Java by pkolaczk on Tuesday December 17, 2019 @01:39AM (#21811236)
You're missing the fact that the constant factor differs between languages. The density (bugs per LOC) of memory handling bugs is much different in Rust than in C.
Re:Kotlin vs. Java by rongenre on Wednesday December 18, 2019 @02:29PM (#21828374)
Re:Kotlin vs. Java by pkolaczk on Wednesday December 18, 2019 @02:35PM (#21828433)
The link you gave confirms what I've said here.

> The quote is either wrong or outdated. In the second edition, it's on page 521: "Industry average experience is about 1 - 25 errors per 1000 lines of code for delivered software. The software has usually been developed using a hodgepodge of techniques.

1-25 is not a single number - it is a range.

Re:Kotlin vs. Java by ivolimmen on Tuesday December 17, 2019 @01:00AM (#21811070)
Well: less code means less bugs doesn't it?
Re:Kotlin vs. Java by smt88 on Monday December 16, 2019 @04:52PM (#21807668)
That's true, and Kotlin definitely leans into it, but extremely concise code basically ends up looking like Brainfuck -- lots of symbols jammed together and hard to read.