0

How can I improve the "flow" of my code?

Profile picture
Final year BSc CS Student at Taro Community23 days ago

Hi, I've been stuck with my current project and am hoping someone can give me a lead here.

I have different actions which need to be run depending on a variable.

Sometimes, I might have to do an action more than one time in the same "if/else" section, e.g. calling actionX() twice (X->X), or different actions in a sequence: ActionX()->`ActionY()`->`ActionZ()`.

The best approach I have come up with so far is like so:

create a sequence, sequence, which holds all the actions which need to be ran inside of it. Then use a for loop to determine which function to call.

while status is less than 2:
user_input = get_user_input()

processed_input = process_input(user_input)

action_sequence = []



# Determine which sequence to execute based on processed_input

if processed_input is 1:

    action_sequence = ["X", "X"]  # Example: X->X sequence

elseif processed_input is 2:

    action_sequence = ["X", "Y", "Z"]  # Example: X->Y->Z sequence

endif



# Execute the sequence of actions

foreach action in action_sequence:

    if action is "X":

        actionX(processed_input)

    elseif action is "Y":

        actionY(processed_input)

    elseif action is "Z":

        actionZ(processed_input)

    endif

endfor
endwhile

Unfortunately this approach is going to become troublesome real quick.

  1. What is this sort of topic called? Claude is saying System Design or Control Flow, I'm thinking it's a turing machine of some sort but don't know the software design term.
  2. Is there a better way to do this? For example:
    1. What if the actions need to be modified or cancelled? e.g. For the chain X->Y->Z, Action X runs and the loop goes to the next iteration. After Action Y runs it then decides Action X needs to be ran again instead of Z.
    2. The loop runs and then ActionY errors out. I need some way of knowing which actions were successful, which ones errored, which ones failed etc. Or even a count on how many actions took place in the sequence. Perhaps, if it errors, then the chain is dumped and actionE runs instead or something like that.

Sorry if it's a long post, this has been stalling my progress for quite a while now and I just don't know the best way to approach this.

49
1

Discussion

(1 comment)
  • 1
    Profile picture
    Tech Lead @ Robinhood, Meta, Course Hero
    21 days ago

    What is this sort of topic called? Claude is saying System Design or Control Flow, I'm thinking it's a turing machine of some sort but don't know the software design term.

    System design feels too high-level for this and control flow specifically refers to branching logic due to conditionals (i.e. if-statements). There really isn't a good term for this. I would use one of these terms:

    • Code structure
    • Code flow
    • Code design

    If you're using an OOP language, this can also fall under OOP design.

    What if the actions need to be modified or cancelled? e.g. For the chain X->Y->Z, Action X runs and the loop goes to the next iteration. After Action Y runs it then decides Action X needs to be ran again instead of Z.

    You can create some sort of ActionProcessor class that takes in a queue of Action objects. Make the queue mutable, so the execution logic can change the contents of it (push/pop certain actions) if necessary. In your case, it looks like you may need to push certain things based on action outcomes.

    The loop runs and then ActionY errors out. I need some way of knowing which actions were successful, which ones errored, which ones failed etc. Or even a count on how many actions took place in the sequence. Perhaps, if it errors, then the chain is dumped and actionE runs instead or something like that.

    Make your processActions() method return some sort of results object like ActionResults. It can have fields like:

    • FailedActions
    • SucceededActions
    • NumActions