Deluge Tips: Reliable Null Checks (strings, numbers, lists, maps, dates)

Tue Jan 21 2025
Deluge · Defensive Coding

Deluge Tips: Reliable Null Checks

In Deluge, responses often contain null, missing keys, or empty strings. Here’s a compact set of patterns to prevent runtime errors and make your logic predictable.

Mostafa Badawy··~6 min read

Rule #1 — Normalize first with ifnull

Before you compare or use a value, coerce it to a safe default. This avoids null comparisons and keeps your types consistent.

Strings

// MAY be null or ""
rawPhone   = someMap.get("Phone");
safePhone  = ifnull(rawPhone, ""); // normalize to empty string

if (safePhone != "")
{
  // phone exists
  info "Phone: " + safePhone;
}
else
{
  info "No phone on file.";
}

Numbers

rawAmount  = someMap.get("Amount");
safeAmount = ifnull(rawAmount, 0); // normalize to 0

if (safeAmount > 0)
{
  info "Amount is positive: " + safeAmount;
}

Booleans

rawActive  = someMap.get("Active");
isActive   = ifnull(rawActive, false); // default false

if (isActive)
{
  info "Record is active.";
}

Dates

rawCreated = someMap.get("Created_On");  // could be null or a Date
// For dates, keep null if missing (don't force "")
createdOn  = ifnull(rawCreated, null);
if (createdOn != null)
{
  info "Created On: " + createdOn.toString("yyyy-MM-dd");
}

Rule #2 — Lists and Maps: default to empty

APIs often return data or records arrays. Always default to an empty list/map before reading to avoid exceptions.

Lists

resp     = /* your API call */;
rows     = ifnull(resp.get("data"), list());  // [] instead of null

if (rows.isEmpty())
{
  info "No rows returned.";
}
else
{
  firstRow = rows.get(0);
  info firstRow;
}

Maps

contact   = ifnull(rows.get(0), Map());  // {} if missing
company   = ifnull(contact.get("Account"), Map());
acctName  = ifnull(company.get("name"), "");

info "Company: " + acctName;

Rule #3 — Check presence before access

When a key may not exist, guard with ContainKey or normalize first.

rec = /* Map from somewhere */;

if (rec.ContainKey("Owner"))
{
  ownerMap = ifnull(rec.get("Owner"), Map());
  ownerEm  = ifnull(ownerMap.get("email"), "");
  info "Owner Email: " + ownerEm;
}
else
{
  info "No Owner key on this record.";
}

Common patterns (copy/paste)

1) Safe equals on strings

codeRaw  = dataMap.get("Status");
codeSafe = ifnull(codeRaw, "");
if (codeSafe == "Closed Won")
{
  // ...
}

2) Safe numeric math

qty      = ifnull(line.get("Quantity"), 0);
unit     = ifnull(line.get("UnitPrice"), 0.0);
subtotal = qty * unit;

3) Iterate only when non-empty

items = ifnull(resp.get("data"), list());
if (!items.isEmpty())
{
  for each it in items
  {
    title = ifnull(it.get("Title"), "");
    info title;
  }
}

4) Nested read with defaults

root    = ifnull(apiResp, Map());
payload = ifnull(root.get("payload"), Map());
meta    = ifnull(payload.get("meta"), Map());
cursor  = ifnull(meta.get("next_token"), "");

What NOT to do

  • Don’t compare a maybe-null string directly to "" without normalizing — use ifnull first.
  • Don’t assume lists/maps exist — always wrap reads with ifnull(..., list()) or ifnull(..., Map()).
  • Don’t force dates into strings too early; keep them as Date objects until you need to display/format.
Summary: Normalize early with ifnull, default collections to empty, and guard nested keys with ContainKey. Your Deluge will be shorter, safer, and easier to maintain.