Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Design: single function performing multiple tasks vs multiple functions performing single task

Tags:

c++

methods

While designing a C++ class, suppose I have following two options:

Option 1: One method with a long argument list and the method performs multiple tasks.
Option 2: Separate method for each task (each method would have smaller argument lists).

Clearly, in general, option 2 is preferable as it produces cleaner code. However, if these methods were for the sole purpose of being "helpers" to some other method then in case of option 2 I would have to incur the overhead of multiple function calls whereas option 1 would have only one function call.

Would the supposed performance gain (due to fewer function calls) justify choosing option 1 in such (extreme) a case?

like image 477
NGambit Avatar asked Mar 25 '26 10:03

NGambit


1 Answers

Would the supposed performance gain (due to fewer function calls) justify choosing option 1 in such (extreme) a case?

IMO, the performance gain here is negligible enough to not sacrifice maintainability of code, particularly in enterprise applications, where functions with large argument lists and lots of lines of code are difficult to modify/debug. Such functions must be broken down into multiple smaller functions where each one performs a well-defined step that is part of a larger task.

like image 108
Vikdor Avatar answered Mar 26 '26 23:03

Vikdor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!