What Should I Be Using For If(Value.isNotBlank(), ..., _NoOp())

It’s pretty common that I need to write a filter like:

If(categorySelector.isNotBlank(),
  [My Table].[Category].Contains(categorySelector)
  _NoOp()
)

This strikes me as an ugly pattern, however I don’t know a way to get around it.

I know how to handle the opposite:

categorySelector.IfBlank(...)

But there is no IfNotBlank formula. Also, _NoOp() is technically not supported, even though I use it in every doc at least 20 times. Anyone know a more elegant solution?

@Paul_Danyliuk? @Federico_Stefanato?

1 Like

I guess I’ve asked about this before on my other account a couple times, haha.

Hi @cnr ,
I second you: it’s not the best pattern ever and I use it a lot, too.

I didn’t find any better alternative, yet.
While not super-urgent, I’d like to see some shortcuts of else-less-if(s).
Something like DoIf() or DoUnless() (or naming variations like RunIf() etc…) that consider the default action is a _Noop().

Happy to know your idea on this, btw.
Cheers!

3 Likes

Hi,
New here and no expert - so I could very much be misunderstanding your question. but isn’t it as simple as writing the following formula in your filter:
categorySelector.isBlank() OR [My Table ].[Category].Contains(categorySelector)

the OR operator means that the formula will always return true if the categorySelector is blank and thus the table will not filtered in that case. You can use a single formula to filter based on multiple controls using the same pattern and and outer AND operator:
(
someSelector.isBlank()
OR
[My Table].[someColumn].Contains(…)
)
AND
(
anotherSelector.isBlank()
OR
[My Table].[anotherColumn.Contains(…)
)

2 Likes

Hi @Nad ,
and welcome to Coda Community! :handshake:

You are right and this actually let me understand I misread @cnr’s first post.
As a matter of fact, composition of conditions - as complex as you like - should allow to have a unique boolean output.
(@cnr: are we missing something, here?)

I took it for another anti-pattern I keep on running into, which is conditional actions:
If(<condition>, RunActions(...), _Noop()).

This is the only way to achieve it, so far and it’s quite ugly to my sensibility :slight_smile: without even taking into consideration _Noop() formula isn’t official.

Thank you!

1 Like

@Nad, great point!

categorySelector.isBlank() OR [My Table ].[Category].Contains(categorySelector)

Is an elegant solution. I’d still argue that someSelector.ifNotBlank(...) is a bit more readable, but I’ll be using your approach until we have that.

@Federico_Stefanato couldn’t we use SwitchIf(<condition>, RunActions(...))?

Seems to work here.

Edit:
The only reservation I have about this approach is that I find it more complex to parse at a glance what a condition is doing when it contains many OR and AND tokens. I suppose with some formatting it’s a bit more readable.

(
  someSelector.isBlank()
  OR
  [My Table].[someColumn].Contains(…) 
) 
AND 
(
  anotherSelector.isBlank()
  OR
  [My Table].[anotherColumn].Contains(…)
)

If logical operators could be chained then this might be an interesting pattern. Not sure it’s much more readable but at least operator preference is explicit.

(
  someSelector.isBlank().Or(
    [My Table].[someColumn].Contains(…)
  )
).And(
  anotherSelector.isBlank().Or(
    [My Table].[anotherColumn].Contains(…)
  )
)
3 Likes

And you are right @cnr! :+1:t2:
I was convinced SwitchIf() had a non optional default.

Thanks!

1 Like

Thanks for this! I’m new-ish to Coda and having from the drudgery of Excel, I had gotten myself used to accepting long horribly unreadable formulas, since excel doesn’t accept any formatting. I’m very happy to see just now that Coda’s formula editor preserves formatting - Yeay !

1 Like

I think it used to, I’m sure I’ve tried it before. But maybe they changed it.

Yeah! It’s great. Thanks again for improving my approach!

Would you be interested in collecting:

(
  someSelector.isBlank()
  OR
  [My Table].[someColumn].Contains(…) 
) 

and

SwitchIf(<condition>, RunActions(...))

into a single answer and I’ll mark it as your first solution in the Coda community? I think it would be useful to future readers to have them all in one post.