The following provides a simple guide to writing Flex scripts, in orrder to make scripts as small and efficient as possible.
Variable declaration
Aim to usedefrather than the class name, as it leads to more compact code (IDEs will still be able to provide auto-completion):
// bad
Long count = 123L
NewAssetPlaceholder assetPl = NewAssetPlaceholder.builder()
.type('media-asset')
.name('demo asset')
.build()
// good
def count = 123L
def assetPl = NewAssetPlaceholder.builder()
.type('media-asset')
.name('demo asset')
.build()
It is also good practise to declare variables as close as possible to where they are used:
// bad
def enabled = isUdoEnabled() && isParentUdo()
def assetPl = NewAssetPlaceholder.builder()
.type('media-asset')
.name('demoasset')
.build()
if (enabled) {
def asset = assetService.createAsset(assetPl)
}
// good
def enabled = isUdoEnabled() && isParentUdo()
if (enabled) {
def assetPl = NewAssetPlaceholder.builder()
.type('media-asset')
.name('demo asset')
.build()
def asset = assetService.createAsset(assetPl)
}
Variable case
Use camel-case for variables:
// bad
def Outputmessage ='my message'
def outputmessage ='my message'
// good
def outputMessage ='my message'
Flex model object instantiation
This model instantiation should be avoided as it is verbose:
// bad
def assetPl = new NewAssetPlaceholder()
assetPl.type = 'media-asset'
assetPl.name = 'demo asset'
// good
def assetPl = NewAssetPlaceholder.builder()
.type('media-asset')
.name('demo asset')
.build()
// also good
def assetPl = new NewAssetPlaceholder([type: 'media-asset', name: 'demo assert"'])
Strings
Single quotes should be used when not performing string interpolation:
// bad
def myValue ="some value"
// good
def myValue ='some value'
It is better to use string interpolation rather than string concatenation, especially with long strings, as it makes it easier to comprehend:
// bad
def myValue = size + " annotations added to assetID " + assetId + " in " + time + "seconds"
// good
def myValue = "$size annotations added to asset ID $assetId in $time seconds"
Semicolons
Semicolons are not necessary in Groovy, and it is recommended not to use them (inline with the [Apache Groovy Style Guide] (http://groovy-lang.org/style-guide.html)).
ifstatements
Avoid the use ofifstatements without curly braces, as it is easy to introduce logic errors if you do not:
// bad
if (somevalue)
callMyFunction()
// good
if (somevalue) {
callMyFunction()
}
Close All Streams
Be sure to close any streams that are opened. Any class that inherits from Closable should be closed. For example, the first example here is bad practice because JVM resources are leaked and the second syntax should be used:
// bad
def execute() {
def fis = new FileInputStream('/flex/flex-enterprise/storage/media/Subtitle.vtt')
def data = IOUtils.toString(fis, Charset.defaultCharset())
}
// good
def execute() {
def data
try (def fis = new FileInputStream('/flex/flex-enterprise/storage/media/Subtitle.vtt')) {
data = IOUtils.toString(fis, Charset.defaultCharset())
}
}
Close All Resources
Close any resouce that has a close() or disconnect() method in a try/finally. e.g.
HttpURLConnection con = (HttpURLConnection) url.openConnection()
try {
int status = con.getResponseCode()
} finally {
con.disconnect()
}
Check for Cancelled Jobs and Exit a Script
To improve performance and stops scripts from running even when a job is cancelled, the option `isCancelled()` is exposed from Groovy SDK to allow JEF SDK to check if a job has been cancelled and exit the script (e.g.: context.executionService.isCancelled(context.executionId)). For example, a script that is blocking execution in a loop is not cancelled when the job is cancelled from Enterprise. Using this method, you can check for the job status and implement a method to exit the script.
def execute(){
while(!context.executionService.isCancelled(context.executionId)){
context.logInfo("running")
Thread.sleep(60000);
context.logInfo("afterSleep")
}
context.logInfo("cancelled")
}
Comments
0 comments
Please sign in to leave a comment.