Understanding How Local Variables Hide Object Fields

If you define a local variable whose name is the same as the name of a field in your object, then be aware that this local variable will take precedence over the current object's field name when evaluated.

For example, assuming an object has a field named Status, then consider the following object validation script:

// Assuming current object has a Status field, define local variable of the same name
def Status = 'Closed'
/* 
 *    :
 * Imagine pages full of complex code here
 *    :
 */
// If the object's current status is Open, then change it to 'Pending'
// ------------------
// POTENTIAL BUG HERE: The Status local variable takes precedence
// ------------------  so the Status field value is not used!
//
if (Status == 'Open') {
  Status = 'Pending'
}

At the top of the example, a variable named Status is defined. After pages full of complex code, later in the script the author references the custom field named Status without remembering that there is also a local variable named Status defined above. Since the local variable named Status will always take precedence, the script will never enter into the conditional block here, regardless of the current value of the Status field in the current object. As a rule of thumb, use a naming scheme for your local variables to ensure their names never clash with object field names.