Alternative to multiple nested conditional if-then?

Options
I often find myself in a situation when I need to use multiple nested if-then conditions based on some value in the incoming data. Is there any other way to "branch" out the flow of the functions?

E.g. I have some "segment id" for all the entries coming into the system and I need the endpoint to apply different functions based on that segment id. To do it right now I need to nest a lot of conditional if-then nested under each other, for every segment id value. Anything else I can do to perform such "branching"?

Thanks

Comments

  • David Richardson
    Options
    I'm going to chime in though I don't have a clear answer.. .. .. more a feature request to the Xano team.  In some programming languages including JavaScript there is a "switch" statement. Maybe there is a way to access that functionality in Xano now?

    The other thing that may make your code easier to understand later is to create your APIs as stand alone code blocks.  Then convert the APIs into a function (3 dots - upper right) and find the function later in the library segment of the page on the left.  In your block of multiple conditionals.. .. .. one after the other, insert the function you just created.  Much easier to read your code later.
  • Valentin R
    Valentin R Member
    Options
     good point, thanks David. I'm also breaking things down as much as possible but sometimes it's not convenient — you lose visibility and editability of things in sub-functions.

    I'll look into the "switch" statement. Maybe it's now accessible through lambdas, which are not yet known to me.
  • Christian Bird
    Options
    Hi Valentin,

    If I'm understanding your problem correctly perhaps you need to try the precondition function under "Utility Functions". Preconditions automatically checks and enforces for a condition to be true. This is an easy fix for reducing if-then statements.

    Let me know if this helps!
  • Valentin R
    Valentin R Member
    Options
     saw this thread, yep. Looking forward
  • Valentin R
    Valentin R Member
    Options
     I'm using those where I can, but any failed precondition stops the process. Thus, you can't use multiple to mimic the "switch" statement mentioned by David above. Or am I missing something?
    [image.png]
  • Inayet Hadi
    Inayet Hadi Member
    Options
    Hey guys the try/catch is on the roadmap. If enough of us upvote it, perhaps Xano's leadership can have more focus on it. I gave my vote.

    
    [image.png]
    Hi  and  can you guys make some videos using the lambda features  with Xano? It will give me some ideas of how to do it and learn more to do other stuff that I want to do.