Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring - Part 1 #51

Open
stefpiatek opened this issue Dec 1, 2022 · 0 comments
Open

Refactoring - Part 1 #51

stefpiatek opened this issue Dec 1, 2022 · 0 comments
Labels
preparation Exercises to do before the class week09 Structure and design

Comments

@stefpiatek
Copy link

stefpiatek commented Dec 1, 2022

For this exercise, we will look at how to rewrite (refactor) existing code in different ways, and what benefits each new structure offers.

We will work with some code that describes a group of acquaintances, as we saw in a previous exercise (issue #9).

Stage 1: Remove global variables

Look at the initial version of the file, which defines a specific group using a dictionary and offers some functions for modifying and processing it.

You may notice that the dictionary is a global variable: all the functions refer to it but do not take it as a parameter.
This situation can lead to difficulties (why?), so we will restructure the code to avoid it.

Rewrite the functions so that they take in the dictionary that they work on as an argument.
For example, the function that computes the average age should now look like:

def average_age(group):
    all_ages = [person["age"] for person in group.values()]
    return sum(all_ages) / len(group)

Your task:

  1. Fork the friend group if you haven't already
  2. Checkout the week09 branch and go to the week09/refactoring directory.
  3. Change average_group as above, and the other functions of group.py in a similar way.
  4. Update the section at the end of the file (after if __name__ == "__main__" ) to create the sample dictionary
    there, and running of the functions that alter it.
  5. Run your file to make sure the asserts still pass.
  6. Commit your changes!
  7. Create a pull request from your branch to the original friend-group repository and use the text in the description to link your PR to this issue Answers UCL-COMP0233-22-23/RSE-Classwork#51
  8. Think of the benefits and drawbacks of this approach compared to the original version.
  9. If you have time, think of other changes you consider useful and try them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preparation Exercises to do before the class week09 Structure and design
Projects
None yet
Development

No branches or pull requests

1 participant