https://kotlinlang.org logo
#getting-started
Title
# getting-started
b

blue

10/11/2021, 10:30 PM
Hi folks, I’m playing with elvis(?:) operator and looking for some help, I want to return from the method when my object is null but before returning from the method I want to add a log statements, what is the correct way of doing that?
Copy code
val record = (repository.findById(id) ?: (<http://log.info|log.info>("log message here") return))
😬 1
p

Paul Griffith

10/11/2021, 10:32 PM
IMO if you have more than a single statement you should just use a regular if statement. That said, you can use
run
for this;
Copy code
val record = repository.findById(id) ?: run { 
	<http://log.info|log.info>("log message here") 
	return 
}
👍 2
r

Richard Gomez

10/11/2021, 11:08 PM
I agree with Paul: readable code is better than clever code.
e

ephemient

10/11/2021, 11:35 PM
to be clear:
Copy code
val record = repository.findById(id)
if (id == null) {
    <http://log.info|log.info>("log message here")
    return
}
will work equivalently, as the
val
is smart-cast to non-null after the conditional
👍 5
although if
<http://log.info|log.info>
returns
Unit
you can get away with
Copy code
val record = repository.findById("id") ?: return <http://log.info|log.info>("log message here")
but I do not recommend it; stylistically, I would tend to avoid treating
Unit
as a value where unnecessary
👍 1
m

Michael Böiers

10/12/2021, 5:51 AM
This seems to be one of these situations where the “old-fashioned” ways of doing things work best. Just like in Java you don’t always use Optional, in Kotlin you shouldn’t always use the elvis operator and/or scope functions. Simple > clever.
👍 6
k

Klitos Kyriacou

10/12/2021, 8:45 AM
Do the "clever" examples of syntax above actually compile? They don't for me. The compiler says that a value of type Record is required where I instead return Unit.
s

Stephan Schroeder

10/12/2021, 9:13 AM
you never gave us the return type of the function you're in, so people assumed it's
Unit
😉 Assuming your method returns
Record
(which is what your error message seems to tell us), this leads to the question: So what do you want to return in case
repository.findById(..)
returns
null
? I see three possibilities: • return a nullable Record • throw an exception
Copy code
val record = repository.findById(id) ?: run { 
	<http://log.info|log.info>("log message here") 
	throw NoSuchElementException("no record found with id $id") 
}
• return a default
Copy code
val defaultRecord = ... //should be outside of the method, so you always return the same instance
...
val record = repository.findById(id) ?: run { 
	<http://log.info|log.info>("log message here") 
	return defaultRecord
}
of course you can only return a default value if a logical/useful default value exists. otherwise return null. null in Kotlin is more benevolent than in Java, since it can't surprise you, so use it where appropriate.
m

Michael Böiers

10/12/2021, 9:18 AM
May be beside the point, but the logging you want to do is a little excessive. If not finding the record could be a problem, maybe it would be better to throw an exception and do the logging elsewhere. Another good solution could be to change the signature of the method so that it requires a record to start with. Methods with a lot of guard clauses that do “silent” returns can be difficult to maintain.
2 Views