Some people like it short, and that is especially true for users in this script forum. And Java Script is very good in that. There are several articles that praise the "beauty" of nice to read and short boolean expressions like:
Neither selection (an array) nor selection.length (an integer) nor selection[0] (an object) is a boolean value but though it is evaluated to true in the right way:
This also comes handy when scripts are checking and evaluating simple attributes or single references. An example:
// he is a hazardous event (corrected!)
var he = ...
if (he && he.hazard && he.hazard.name == 'Foo') {
alert("Got ya");
}
Again, nice and short: we can sum up paranoia checks (he is defined) with reference check (hazard is set) and the final comparison in a single expression.
While that is somewhat obvious for single references, it may not be as obvious for zero-to-many references or attributes. Main reason is that they are never undefined.
So for example:
// again he is any hazardous event
if (he && he.malfunctions) {
alert("You have malfunction set, all good");
}
No this won't work, because malfunctions in the meta model is zero-to-many so may have multiple entries, hence a list is returned which is always defined and thus evaluated to true. Note: in the HARA editor, you can only set a single malfunction but the metamodel is already prepared to reference more than one.
So for these cases you can use List.isEmpty() or List.size():
// again he is any hazardous event
if (he && !he.malfunctions.isEmpty()) {
alert("You have malfunction set, all good");
}
// alternative
if (he && he.malfunctions.size()) {
alert("You have malfunction set, all good");
}
Hope this article helps you to write nice, short and beautiful scripts!
Happy Scripting
Jan