def antBuilder = new AntBuilder()
antBuilder.tar(basedir: getOutputDirectory(), destfile: getTarFile())
antBuilder.gzip(src: getTarFile(), destfile: getTarGzipFile())
antBuilder.delete(file: getTarFile())
antBuilder.delete(dir: getOutputDirectory(), includes:"*.xml")
I have a method which does the above. As you can see the getTarFile() method is called thrice overall. What is the preferred way of coding it, using getTarFile() thrice OR defining a local variable to hold the value of getTarFile() and using that instead?
You can get as Groovy as you want really…
You could do (for example) wrap it in a closure:
And call that with:
Whatever way you’re doing it, unless getTarFile is doing loads of work (which seems unlikely), you’re not going to gain much more than readability…
Though that can be a win in itself 😉