"Translate" booleans to a textual output

Hi all,

I have written some code which “translates” my booleans into text which shows the end user why something has or has not happened after running the script.

This code does work, however I feel like this approach is terrible so I thought I would share my thought process here and ask for some advice on how to do this differently.

You already have booleans, so I would suggest keeping them rather than comparing strings. You could create a bunch of conditions and subconditions to get exactly the output you’ve shown, but I’d recommend simplifying things by making both checks return “standalone” values so that combining them becomes trivial.

You can of course also use Python to simplify your compound condition statements if that’s the route you’d prefer.

Ah yes, way better!

Maintaining your original phrasing just requires additional checks for the specific conditions. The idea is that you can turn each check into a conditional block and then build your final string from those blocks.


There are more succinct ways to write this out, but I think this best shows the concept of building the condition statements independently and forming a combined output.

I changed it into this:

x[0] == false && x[1] == false ?"SupplierId and ConsumerId both do not exist":
x[0] == false ? "SupplierUniqueId does not exist":
x[0] == true ? "ConsumerUniqueId does not exist":"";

That’s certainly the easiest way to do it since you really only have 4 total conditions. If you had more than that it would likely become too complicated to handle individually rather quickly. However I think your last condition may still be incorrect.

I’d also point out that you don’t need to do boolean checks with a boolean. The point of your test condition is to determine whether something passes (true) or fails (false) and you already have those values. You also have control over both the true result and the false result, so there’s no need to check if a boolean is false. You can just check if it’s true and then use the false result as intended. Another option would be to use the not (!) operator.


2 Likes

Whilst in Python it is more direct, I often abuse the fact booleans can be treated as integers (0,1) as well. I’ve used an if statement in the below to convert this in Dynamo’s environment.

From there you can form checksums using numbers which cannot add up to one another in different combinations. You can then align these outcomes to indices in a list. It doesn’t scale for scenarios with 3, 4 booleans etc. but is perfect for dealing with pairs where boolean order is critical, whilst doing away with the need for nested statements. At scale, I tend to work powers of ten, so can verify using order of magnitude and positions of 1’s what order of outcome has occurred. Pretty hard to replicate in Dynamo versus Python, but has a good amount of potential in my experience of using the trick.

Hope that’s an interesting approach also.

3 Likes

Nice one Gavin :wink:

1 Like