PricingDocsAcademy
Bluesky ...
Wed, Dec 11, 10:18 PM

Checking type of input best practice

  • /attachments/1293856387088257045/1293856388413390859/toddle.dev_projects_visus_branches_delta_components_delta-test_leftpaneldesigncanvas-width800canvas-height800rightpaneleventsselectionworkflows.qDJDbY.actions.5.data.png

    Tom Ireland

    2 months ago

    Hey, community and toddle-hero's. I discovered an issue in my workflow earlier where I was checking if an array was empty to then set a variable value. The problem was that this kept returning the variable value as undefined. After some time and head scratching, I found out that the array I was trying to check was actually an object. 🤦‍♂️

    The array check I'm doing is actually a formula that accepts an input, which means I can use this formula to check if any array I pass in is empty or not and then apply some additional logic.

    Now, I know I can use Type of to check what the input type is but I'm trying to figure out where this best belongs. Should I be checking this within the formula itself for what is passed in as the argument or should I be checking this when using the formula? The logic is that I can have some kind of internal error checking to ensure that I'm setting everything correctly.

    Video explainer: https://www.youtube.com/watch?v=hAcCbPrYi-o

    See image for output based on my formula, which I think works well and will prevent me adding the wrong input type.
  • Tom Ireland

    2 months ago

    I guess in this case, checking if an array is empty using a formula could be considered as extra work? I could just check the size and type when setting the variable, which is what I'm doing with the formula. All the formula is doing is checking stuff in a concise way that will always return a consistent output. But is it actually needed? 🤔
  • Tom Ireland

    2 months ago

    Answered my own question. I'd have to apply the same logic as in the formula to get the output I want, so it does actually save time. Otherwise, I'd have to apply the same logic every time, which is repetitive and laborious. Also, if I forget to check the type and just get the size, I will actually get a result of whatever input I'm passing to size, which could yield results I don't want. So, moral of the story: formula is good, type check in formula is good and results are consistent.

    Open to feedback on that.

Stop scrolling. Start building.

toddle is a visual web app builder that rivals custom code — but accessible to your entire team!

Try toddle — it's free!

© Copyright 2024 toddle. All rights reserved.