-3

I am working on C++ on Linux.

I need to remove some duplicated code in two functions.

One function is for computing and another one is for logging.

There are some code that are duplicated in logging(), which is much longer than computing().

The duplicated code is distributed in logging() separately, which means that they are not just copy and paste from computing().

I need to figure out the duplicated part line by line, remove them and then replace the necessary results by passing them as parameters from computing() to logging.

Are there some efficient ways to handle this ?

Paul R
  • 208,748
  • 37
  • 389
  • 560
runner frank
  • 331
  • 1
  • 6
  • 13

2 Answers2

3

Look at the functions side by side, identify common blocks of code, then factor these common blocks out into separate methods/functions.

Paul R
  • 208,748
  • 37
  • 389
  • 560
1

It might not be worth merging them. If you really have to, though, perhaps one common function with an extra bool do_logging parameter.

John
  • 7,301
  • 2
  • 16
  • 23