How can I split a long function into separate steps while maintaining the relationship between said steps?

Viewed 871

I have a very long function func which takes a browser handle and performs a bunch of requests and reads a bunch of responses in a specific order:

def func(browser):
    # make sure we are logged in otherwise log in
    # make request to /search and check that the page has loaded
    # fill form in /search and submit it
    # read table of response and return the result as list of objects

Each operation require a large amount of code due to the complexity of the DOM and they tend to grow really fast.

What would be the best way to refactor this function into smaller components so that the following properties still hold:

  • the execution flow of the operations and/or their preconditions is guaranteed just like in the current version
  • the preconditions are not checked with asserts against the state, as this is a very costly operation
  • func can be called multiple times on the browser

?

5 Answers
Related