"One can see that the two functions are almost similar" - Kent Beck's refactoring mantra is "Make the change easy (this might be hard), then make the easy change". So the first step is to make the two functions look the same, and the rest is the "easy change" part. If you have an entire block of code that varies between the two (and not having one is a variant), than make that block of code a parameter (a lambda or an object). Such a block of code can itself take parameters and return a value, so you'll need to come up with a general enough signature. 1/2
↧