Reputation: 1375
I'm using Groovy. What's a more elegant way to do the following?
if (!token) {
token = this.getToken(key, callback)
if(!token) {
return params = null
}
}
So if a token is already present when a user comes into this flow, that token should be used. However, if the token isn't present, we should try to generate one. The service we're using to get the token doesn't throw an exception if none is created, it just returns null. If the token isn't successfully created (is still null) we want to set the params to null.
Is there a cleaner way to do what I'm doing? The null check within a null check seems ugly to me.
Upvotes: 0
Views: 75
Reputation: 66109
The first part can be rewritten using the elvis operator to eliminate the first null check:
token = token ?: this.getToken(key, callback)
if (!token) {
...
}
Upvotes: 2