Hello, has any way to improve this conditional? `r...
# codereview
v
Hello, has any way to improve this conditional?
read.cutOn != meterCut && (meter.cutDate != null || readDate.after(meter.cutDate))
j
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
Right
== null
instead of
!= null
read.cutOn != meterCut && (meter.cutDate == null || readDate.after(meter.cutDate))
j
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
amazing, i didn't know let. Thanks 😉
j
The way your code is structured right now is actually more readable than using
let
👍 2
l
I think you can also use
then
which is a more readable
let
j
@leosan Do you have an example using
then
. Kinda hard Google for an example when it’s such a common word.
👍 1
l
@janvladimirmostert Sorry, I meant use
also
😛 as
Copy code
meter.cutDate?.also{ readDate.after(it) } ?: true
e
I never heard about
then