Answer the question
In order to leave comments, you need to log in
Which way would you go when refactoring?
Hello, I'm trying to figure out SOLID and specifically the open-closed principle, which recommends extending classes to add new functionality.
Task: there is a Balance class , it has a getBalance method that returns the user's balance without rounding those with 10 decimal places.
The project manager decided to return with rounding.
How would you do it and why?
1) Add rounding to the existing getBalance method, run tests on getBalance and after 5 minutes go drink coffee.
2) Add a new getAdvancedBalance method with rounding to the Balance class, and do not touch the current method because we have a bank
and if something breaks it will not be pleasant
3) Make a new class AdvancedBalance inherit from Balance and override the getBalance method,
refactor the entire project to use AdvancedBalance:getBalance instead of Balance:getBalance and pray that nothing is broken
, even though there are tests, but the bank and if someone oligarch has something wrong will be displayed to you 4) Make a new AdvancedBalance class inherit from Balance and create a new getAdvancedBalance
method, refactor
and pray again.
Answer the question
In order to leave comments, you need to log in
None of the above!
Add an optional parameter to set the return value format to the getBalance method and that's it.
3) Make a new class AdvancedBalance inherit from Balance and override the getBalance method
The getBalance method returns one number, the getBalanceOkrugl method will return a different number.
It's like getBalance returning prime numbers, and the manager said that now we need to work with Fibonacci numbers. These are two different sets.
2) Add a new getAdvancedBalance method with rounding to the Balance class, and do not touch the current method because we have a bank and if something breaks it will not be pleasant
An ideal solution as Evgeny Romashkan
wrote
. Make a BalanceFormatter and use it as you wish.
the main thing is not to forget immediately the semantics will be laid down correctly, otherwise people like to shove such things into the Helpers folder and then sit and dig in the trash of "helpers"
why adding a parameter with a default value is bad, explained by Martin in Clean Code. this is a really stupid decision.
The project manager decided to return with rounding.
var balance = Balance.Create(/* агрументы */)
. Well, and accordingly, depending on the values of the incoming arguments, the correct successor will be returned to you. If you want to add a new child, such as AdvancedBalance, you will only change the Create method. This is where the principle of openness/closeness will probably be observed.4) Make a new AdvancedBalance class inherit from Balance and create a new getAdvancedBalance method, refactor
and pray again.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question