-1

Is something like this code considered a bad practice? If so, what should I do when func1 duplicates func2's behavior and I need both functions to be present (isn't that considered code redundancy)?!

UPD: Sorry for my bad illustration, I'll try to explain the question more clearly. What i wanted to ask about is that: I'm trying to design an optimized class that heavily calls two methods func1 and func2, func1's implementation uses func2 and i want the two methods calls to be inlined as much as possible, So is it better to call func2 from func1 like this code or to implement both independently.

inline int func2(int x) {
    return x * (x + 2);
}
inline int func1(int x) {
    return x * (x + 1) * func2(x + 2);
}
  • 2
    Are you asking if it's a bad idea for an inline function to call another inline function? Why would you think it's a bad idea? – Mooing Duck Jun 08 '17 at 21:32
  • 4
    If you need two functions, that's by definition not redundant. If they're redundant, you don't need two functions. – Mooing Duck Jun 08 '17 at 21:32
  • 2
    There are so many things wrong with this question I don't even know where to begin. 1) Your title mentions inline functions. Where's the `inline` functions? 2) "Need both functions present". Wait wut? 3) You mention `func1()` duplicating `func2`. Where's the duplication? – Mysticial Jun 08 '17 at 21:35
  • Sorry for my bad illustration, I updated the post. Hope the question is clear now – Abdelrahman Ramadan Jun 08 '17 at 21:54

4 Answers4

4

Writing several small functions is fine if it avoids writing the same code more than once. Some may argue that too many small functions makes code hard to read and that's a matter of opinion.

If you are worried about performance, the compiler will inline if it thinks it will help, you shouldn't worry about it until you've proven that there is a problem. See this question on premature optimization.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
1

There's no problem in a function calling another function. You'll see real programs go much deeper than 2 calls, if you sample them.

As far as inlining, that's also no problem. An optimizing compiler would typically inline func2 (assuming its definition is visible and optimizations are enabled). Many common compilers and optimizers are smart about inlining. They often know when to inline and when not to inline -- all without your assistance.

Writing small functions is not a bad practice. Clarity and intent are typically of a higher importance than micro-optimizations. Under typical circumstances, there's nothing wrong with your example.

justin
  • 104,054
  • 14
  • 179
  • 226
0

If it helps readability of your code, then yes. You should almost always aim for readability of your code. Don't forget to properly name your functions so other people will easily understand what is that function doing. And by other people I mean you too in few weeks or months. As they say you write code once but read it many times.
As for the performance, modern compilers know when to inline the function and you should not worry about it. In cases where it really matters you will just use the profiler to find the hostspot and eventually change it. But it will happen much fewer times than you think. You will almost always find better ways to optimize your code.

Marek Vitek
  • 1,573
  • 9
  • 20
0

If both implemented in same scope, than compile can even do some algebraic optimization without inline. Some time ago I was very surprised when see that compiler sometime replace big and complex structures with simple calls of destination functions (kinda arguments carrying for d3d api). So, if you worry about performance, than just don't... at least yours app benchmarks are really bad.

On the other hand, it's all about relations: if func1 not really logically related to func2, only code\math kinda same, than better to copy func2 into func1. Why? Because func2 may be changed, but you forget about func1 and broke it, because they was related not by internal domain logic.


UPD after UPD

If all about speed and their is only math, than wrote in func1 fully optimized expression and don't rely to compiler. But it's if you really know that performance are on first place.

Green_Wizard
  • 795
  • 5
  • 11