Say I have a method called 'functionA' that is called by a service and carries out a single piece of functionality, it is easy to unit test as it is does one thing only.
If a few months later an new requirement comes in that requires another method 'functionB' to be called directly after 'functionA' has executed. What is the better approach to add functionB to my code.
to call 'functionB' at the end of the 'functionA' method:
functionA() { //do something.. functionB(); } functionB() { //do somehting... } Problem with this is that when I unit test functionA it will call functionB that will also be part of the test (so now it is more of an integration test).
Or refactor the code with a new method 'functionX' that calls 'functionA' and then 'functionB'?
functionX() { functionA(); functionB(); } functionA() { //do something.. } functionB() { //do somehting... } I can unit test both methods individually but now I have changed the API.
I see the first approach so much in legacy code, is it best practice to always make methods closed for modification or are there examples when it is OK to use the first approach?