Null management

One of the more cumbersome aspects of typical programming is handling null objects. Tests must be done in the code to ensure that a null value is not returned which would cause exceptions in any further processing. These checks make the real logic of the code difficult to see.

MQL handles the null objects so that you do not have to code this logic yourself. For example, in Java if we want to test a stereotype before doing some processing, we would have code similar to the following.

/*--------------- JAVA code ---------------*/
com.sodius.mdw.metamodel.uml13.Class myClass = ...;

// retieves the stereotype name
String sterotypeName = null;
if (myClass.getSterotype() != null){
    sterotypeName = myClass.getSterotype().getName();
}

// checks the stereotype is Enumeration
if (sterotypeName != null && sterotypeName.equals("Enumeration") {
    // do something
}   

With MQL, this is reduced to the following code.

/*--------------- MQL code ---------------*/
var myClass : uml13.Class = ....;

if (myClass.sterotype.name == "Enumeration") {
    // do something
}

If a property or method returns null at any level of a chain, then the result will be null (rather than throwing an exception). However the default value can be specified using the '?' character. This is particulary useful to avoid a bunch of if statements.

Example:

// Returns the string "unnamed" for any untyped attribute
myAttr.type.package.name.toUppercase() ? "Unnamed"