You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Checkout the week09 branch and go to the week09/refactoring directory.
Change average_group as above, and the other functions of group.py in a similar way.
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.
Run your file to make sure the asserts still pass.
Commit your changes!
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
Think of the benefits and drawbacks of this approach compared to the original version.
If you have time, think of other changes you consider useful and try them.
The text was updated successfully, but these errors were encountered:
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:
Your task:
week09
branch and go to theweek09/refactoring
directory.average_group
as above, and the other functions ofgroup.py
in a similar way.if __name__ == "__main__"
) to create the sample dictionarythere, and running of the functions that alter it.
Answers UCL-COMP0233-22-23/RSE-Classwork#51
The text was updated successfully, but these errors were encountered: