https://kotlinlang.org logo
#codereview
Title
# codereview
v

vinicius.rob.cunha

05/05/2017, 2:16 PM
Hello, has any way to improve this conditional?
read.cutOn != meterCut && (meter.cutDate != null || readDate.after(meter.cutDate))
j

janvladimirmostert

05/05/2017, 2:22 PM
vinicius.rob.cunha: This part:
Copy code
(meter.cutDate != null || readDate.after(meter.cutDate))
Does the logic mean, if
meter.cutDate
is
null
, do a
readDate.after(null)
? Not sure I follow the logic
Was just wondering if that shouldn’t have been an
&&
instead, then it would be possible to simplify it.
v

vinicius.rob.cunha

05/05/2017, 2:25 PM
Right
== null
instead of
!= null
read.cutOn != meterCut && (meter.cutDate == null || readDate.after(meter.cutDate))
j

janvladimirmostert

05/05/2017, 2:34 PM
the part in the brackets , there are several options.
let
should help here: Haven’t tested this code, try it first:
Copy code
meter.cutDate?.let { readDate.after(it) } ?: true
v

vinicius.rob.cunha

05/05/2017, 2:49 PM
amazing, i didn't know let. Thanks 😉
j

janvladimirmostert

05/05/2017, 2:51 PM
The way your code is structured right now is actually more readable than using
let
👍 2
l

leosan

05/05/2017, 2:53 PM
I think you can also use
then
which is a more readable
let
j

janvladimirmostert

05/05/2017, 3:00 PM
@leosan Do you have an example using
then
. Kinda hard Google for an example when it’s such a common word.
👍 1
l

leosan

05/05/2017, 3:35 PM
@janvladimirmostert Sorry, I meant use
also
😛 as
Copy code
meter.cutDate?.also{ readDate.after(it) } ?: true
e

elect

05/08/2017, 12:27 PM
I never heard about
then
7 Views