---
# System prepended metadata

title: COMP0233 - Day 9 (8th December 2022)
tags: [COMP0233]

---

# COMP0233 - Day 9 (8th December 2022)


:::info
## :tada: Welcome to the ninth day! Construction & design :hammer: :triangular_ruler: 
 

### Today
Today we will look at 
- which factors affect coding choices
- rewriting code to improve it
:::


:::warning 
| Room | groups |
|---   | ---    |
| 1.05 | 4, 6, 9, 11, 13, 16, 21 |
| 1.21 | 1, 5, 8, 10, 15, 19, 20 |
| 4.06 | 2, 3, 7, 12, 14, 18,    |
:::


:::danger
**Only use zoom if you are connecting remotely or need subtitles**
Remember to `rename` yourself by clicking on your zoom name (in the participants list) and change it to:
`SXXX` where XXX is the room number (105, 121 or 406 or 000 for the online attendees).

If you disconnect and connect again, please rename yourself again.

**Use Youtube If you are in class**
⏯ [streaming - youtube](https://youtu.be/2WRGaxAKNd0)
:::


### Review from last week 

- Waterfall and agile methodologies
- Communication and progress monitoring in collaborative projects 

#### What was covered during the week :newspaper: 

- Readability of code
- Refactoring
- Object-oriented design

### Understanding quiz

Help us understand how this week's preparation work went with [mentimeter](https://www.menti.com/6rk6kmb1n4) (or code `5425 2993`)

### Discussion
:::warning
:question: **What else is important for your code, other than correctness?**


Write Notes in the class pad for your group below:
:::


- Group 1:
- Group 2: 
- Group 3: to be understandable, readable, clean, has tests which pass, well-documented,reusable, efficiency, memory optimization, to have a design document, maintainable, to be updated when new features/libraries appear, change :100: 
- Group 4: readable, maintainable, open-closed principle, fits user requirement, execuation cost
- Group 5: 
- Group 6:
- Group 7: 
- Group 8: 
- Group 9: Clear structure to help efficiently readable
- Group 10: 
- Group 11: First of all, the code needs to be readable, so that others can easily read your code, and secondly, you need to reduce the repetition of the code
- Group 12: Efficiency and able to understand for others. Clean and good structure. Back up.
- Group 13: Readability of code, efficiency, a good algrithm can decrease the complexity of the code.
- To have a good presentation of the code.
- Group 14: Conciseness
- Group 15:readability, time complexity, storage complexity, documentation, reusability, 
- Group 16: 
- Group 17: 
- Group 18: usability, documentation, efficiency, readability, allows for user flexibility, easy to maintain, robust, memory efficient
- Group 19: 
- Group 20:Making sure its easy to read, have  example of input and output  to make its understood 
- Group 21: Efficiency, Readability, Fully documented
- Group X:


###  Exercise: Improving code - Part 1
:::info

For this exercise, we will look at an existing piece of code and see how it can be improved.

Based on your group number, look at the following code:
| Group number | Code sample |
|--: | - |
|1-6 |[Tree plots](https://github.com/UCL-COMP0233-22-23/refactoring-examples/tree/main/trees)|
|7-12|[Sample analysis](https://github.com/UCL-COMP0233-22-23/refactoring-examples/tree/main/analysis) |
|13-21 & X|[The Boids](https://github.com/UCL-COMP0233-22-23/refactoring-examples/tree/main/boids) |

You will find a brief description of each example in its README file.

- First, discuss the code with your group. What problems do you see with it? Why?
- Write down your comments in the space below. For now, focus on the problems, and don't make any changes yet!
:::

Leave your comments here:

- Group 1:
- Group 2: 
- Group 3:  :1234: https://prod.liveshare.vsengsaas.visualstudio.com/join?B2956DA82BBE017573F444DDD417889E9C1B
    1. name variables not suggestive
    2. not looping over an iterator
    3.  the only comment which appears is redundant and not helpful at all 
    4. magic numbers 
    5. create a method instead of random code in a file 
    6. fix numbers in the code, not readable, 
    7. not explaining what is going on  
- Group 4: 
- Group 5: 
- Group 6: need clear variable name to read, need some comments, need clear input and output  
- Group 7: 
- Group 8: 
- Group 9: the import function may not use. Some varible names are confusing.
- Group 10: 
- Group 11: Some function are reused, but just write two pieces of code. Writing an functiona and call it twice can reduce repetition. At the same time, the front part of this code is consistent, we can just use a class and two functions to realize the function of these two pieces of code. 
- Group 12: 
    - The import math function is not used.
    - Highly repetitive operations for reading and writning files
- Group 13: The code is too complex. We can create a class to make the code more concise and improve the readability of the code . Create a function to implement the similar data processing process with the same function. 
- Group 14: 
    1) Do not dupplicate library calls and avoid unecessary linebreaks

    2) A single for loops should used whenever possible and they should all rely on iterators

    3) Spaces need to be added to improve readability

    4) Variables should have evocative names and avoid being dupplicated

    5) Implement classes to simplify everything

- Group 15:
    - The function is doing everything, its function should be splitted into subparts
    - the calculation part can be extracted to an equation function
    - better namin
    - use configuration file 
    - comments
    - loop instead of using 
- Group 16: 
- Group 17: 
- Group 18: 
    - nested loops with same range :(
    - could store len(xs) in a variable since used so much
    - unused imports at top of code :(
    - use of tuple :(
    - use of magic numbers :(
    - more descriptive variable names
    - vague comments :(
    - https://prod.liveshare.vsengsaas.visualstudio.com/join?96CEC79C27B835D4A49200AB7BEB4CC5F51A
- Group 19: 
- Group 20:
- Group 21: 
    - 1. nested for loop 
    - 2. unclear input variable 
    - 3. unclear output variable 
    - 4. Sparse and unclear document
    - 5. more meaningful variables could be applied
- Group X: 
    - replace 4x for-loops with single = 4x savings; 
    - improve var naming;
    - break up into modular functions; five lines per func;
    - magic numbers need replacement;
    - frequently used vars (e.g. len(xs))

###  Exercise: Improving code - Part 2
:::info

Based on the problems you identified and the previous discussion, how can you improve the code?

1. **First**, take around 5 minutes to plan your changes.
1. **Once that is done**, start making changes according to your plan.

To get the files on your computer and push them back:
1. Fork the repository
1. Clone your fork
3. (make changes, commit...)
4. Push back to your fork

You can submit a Pull Request for [the original repository](https://github.com/UCL-COMP0233-22-23/refactoring-examples) when you have made some change.
- You don't have to wait until they're all done! 
- Target the `main` branch of the original repository
- Indicate the example you have worked on in the title of the PR, e.g. "Boids: rename variables"
- If you have more time, add more commits to your branch so they are included in the pull request
:::


### Understanding quiz

Go back to [mentimeter](https://www.menti.com/6rk6kmb1n4) (or code `5425 2993` ) for some final questions 

### Real life examples of design patterns:

- Factory method:[from_file method](https://github.com/SilverLabUCL/PySilverLabNWB/blob/358c551192cc77ad0fb7070d42424785f1010b14/src/silverlabnwb/header.py#L25)
- Builder: [HiddenMarkovModel](https://github.com/biopython/biopython/blob/4d46e7ea623660cef601fab58e60f3c5042bab73/Bio/HMM/MarkovModel.py#L68) and usage in [HiddenMarkovModel tests](https://github.com/biopython/biopython/blob/4d46e7ea623660cef601fab58e60f3c5042bab73/Tests/test_HMMGeneral.py#L148)
- Strategy: [affine_transform](https://github.com/sunpy/sunpy/blob/6f616caf08d5b680d9b70b45160c3f81f9db9db8/sunpy/image/transform.py#L93) shows an example application of the strategy pattern by delegating the transformation method to the `method` that is determined from the argument

### Homework


- Finish [week 9: gradual refactoring preparation work](https://moodle.ucl.ac.uk/mod/url/view.php?id=4463010) if you haven't already
- Commit and push, then create a PR for the classwork on refactoring
- Preparation for week 10: performance

###### tags: `COMP0233` 
