I'm trying to do things in the best "Groovy-way" possible. What's the best way to check the type of an argument (regarding performance and the "Groovy-way)? I have 2 implementations in mind:
def deploy(json) {
if (!(json instanceof String) && (json instanceof File)) {
json = json.text
} else {
throw new IllegalArgumentException('argument type mismatch – \'json\' should be String or File')
}
// TODO
}
or
def deploy(File json) {
deploy(json.text)
}
def deploy(String json) {
// TODO
}
Thanks :)
There's nothing groovy specific in your question, it's more about compile/runtime failures.
In first snippet json
variable has Object
type and allows everything to be passed in. It will fail in runtime if you pass in JSON
object or Map
by mistake.
In second snippet json is restricted to be either File
or String
. I like it more.