Nested CONCAT problem

Hello
Example link

CONCAT(
  $Surname,
    IF($Name==None,
      None,
      CONCAT(
        " ",
        LEFT($Name),
        "."
      )
    ),
  IF($Mid_name==None,
      None,
      CONCAT(
        " ",
        LEFT($Mid_name),
        "."
      )
    )
)

But If $Name==None or/and $Mid_name==None formula return dots.

Git#196

Hey BiBo!

If you change your code from using None to using “” (just two quotation marks in a row, which means the cell is blank), then it should work :slightly_smiling_face: I tried it out in your example and it seemed to work, anyway!

I’m not an expert, but I suspect that this is because the cell isn’t None-type, it’s just empty.

1 Like

Thank you @Parker_Smale
I added this to column v2. Really works.
Where to use None?

Sometimes in Python it’s helpful to distinguish an empty string from no value at all. E.g. in Grist, a Numeric or Date column may have an empty value, and it makes sense for it to be None rather than the empty string, since it’s not a string of text at all.

A string has various operations (e.g. it’s valid to add it to another string, or get its length, or turn to uppercase). When those operations don’t make sense, a None value helps avoid confusion.

But in other cases, like yours, it can add confusion :slight_smile:

Some rule of thumb I’d recommend:

  1. When comparing values, use

    • if $Value rather than if $Value != None or if $Value != "".
    • if not $Value rather than if $Value == None or if $Value == "".
      Simply using $Value is treated as false when it’s None, empty, or 0, and as true otherwise. Same in other conditions, e.g. IF(not $Name, ...).
  2. Reserve None for exceptional values. When operating with particular types, use that type. E.g. when adding strings, use empty strings (""); for numbers, use zeroes.

By the way, as an aside, Python string formatting is great for cases like yours. E.g. you can replace

CONCAT(
  " ",
  LEFT($Name),
  "."
)

with the shorter

" {}.".format(LEFT($Name))

(the empty curly braces get replaced with the arguments).

Also, the “Conditional Expressions” tip from this newsletter is often helpful. Some examples from there:

$Birthday.year if $Birthday else None
$Total / $Count if $Count else 0
" {}.".format($Name[0]) if $Name else ""    # Your example
2 Likes