Good Practices
There are several good practice conventions in the scripting world, and Groovy within IJC is no exception. Here we have are collecting tips and suggestions for good coding practices, to help out with your code development. This page will continue to grow as we collect more suggestions.
-
Try/Finally Clause : It is a good idea to put the bulk of the 'meat' of a script into a try/finally clause, so that if there is a failure, the script will not hang. See the Groovy documentation for Exceptions . Additionally, it gives you some precise control of the final steps. This is especially helpful for scripts that use several steps and several locks. For example:
def
lock = edp.lockable.obtainLock(
'working'
)
def
envRW = EnvUtils.createDefaultEnvironmentRW(lock,
'working'
, true)
try
{
// code code code
}
finally
{
lock?.release()
envRW?.feedback.finish()
}
Since version 6.0 you can use method
withLock
for locking items safely:
edp.lockable.withLock(
'working'
){ envRW ->;
// code code code
}
-
Asserts : Asserts are very useful for ensuring that variables that are supposed to contain data are not actually null. This can prevent a lot of debugging only to find out that an initial variable was set wrong. For example:
assert
etyStructureField !=
null
If the structure is returned null, the script will immediately stop.