You work for a leading streaming company competing with Amazon Prime and Apple TV. Like these services, your customers can rent or buy movies and TV shows. Typically, these services expire a download after 24 hours regardless of whether the customer has viewed the programme.
Your company does things slightly differently. It wants to go back to the physical video store idea and allow customers to keep a rental for as long as they like and charge them per day. Customers end thier rental period at any point via the UI (for example, when they've finished watching a movie).
When customers rent a movie, they are charged varying amounts depending on the type of movie and the duration they rent it for.
There is also a frequent renter programme which can be used to apply discounts. A customer accrues a different amount of points each time they rent based on the type of movie.
The system produces a statement which includes charges and frequent renter points.
| Type | Charge per day | After fixed period |
|---|---|---|
| Cinema at Home | £ 15 for 2 days | £ 4.99 per day after |
| New Release | £ 5.99 | N/A |
| Childrens | £ 1.5 for 3 days | £ 1.5 per day after |
| Regular | £ 2 for 2 days | £ 1.5 per day after |
| Low Sellers | £ 0.99 for 2 days | £0.50 every two days |
NB. Low sellers are charged at 50p for two days after the initial fixed period. If the rental is returned mid-period, the minimum charge of 50p is made.
1 point per day except for New Releases which get 2 points for every day after the first day (which is still 1 point).
A new requirement has just come in...
The existing statement is geared up to be sent out in an email at the end of every billing cycle. We think our customers would like to be able to view their statement online or on their device.
As such, we would like to create a HTML version of the statement (whilst keeping the text based one for email).
You have picked up the code base to make some minor changes. You spot that it's not object oriented or functional (it's basically imperative). You can see that there is lots of complexity in one place (violating single responsibility principle) and it just looks plain ugly.
The computer doesn't care how ugly the code is though. It's only when you have to make a change that this matters - when a human gets involved. A poorly designed system is hard to change. If it's hard to figure out where to make changes, it's more likely that mistakes will be made and bugs introduced.
As you look at the existing statement method, its obvious that you won't be able to reuse any of the existing behaviour. What can we do to make it easier to create an alternative statement?
The only option as it stands, is to create a whole new htmlStatement method and duplicate the behaviour. This isn't too bad as in our case, the method is pretty trivial. What happens though when the logic changes, now you'll have to maintain and test it in two places.
- Spend some time understanding the code
- Refactor the code to make it easier to work with
- Share your thoughts with the class
After a while, the instructor will take you through some more formal refactoring steps and you will have a chance to apply them on your own. At the end of the session, we will have a group discussion.
- What will drive your refactoring? What should you change any why?
- As you refactor, think about what end goal are you trying to achieve. What does a particular refactor do for you?
- How will you know that you haven't broken anything?
Write down the name of the refactorings (if you know them) as you apply them and your answers to some of the questions above.
Implement an HTML version of the statement method. Can you do so without cut and pasting (duplicating) the original? What can you reuse? What patterns would help you?
Was this easier after the refactorings? Why?
- Refactoring in IntelliJ IDEA
- Refactoring: Improving the Design of Existing Code Martin Fowler
- Recap video in Java