Duplicating source code
-
- KVRAF
- Topic Starter
- 7402 posts since 17 Feb, 2005
What are some alternatives to writing duplicate code in the source? My specific example is plugin state initialization/reset, where I find the same block of code in 3 different member functions of my plugin.
There's always the venerable C macro
Maybe an IDE plugin that pushes changes in one block of text to different areas?
C++ templates maybe?
Scripts?
There's always the venerable C macro
Maybe an IDE plugin that pushes changes in one block of text to different areas?
C++ templates maybe?
Scripts?
Last edited by camsr on Tue May 23, 2017 10:15 pm, edited 1 time in total.
-
- KVRian
- 1379 posts since 26 Apr, 2004 from UK
The same block three times? So this means that you probably need this state to do something similar, so probably a class that initializes the content in the constructor and 3 different methods that may share similar code paths?
-
- KVRist
- 50 posts since 3 Nov, 2015 from Germany
Hi, why don't you just put the duplicate code into a function and call it? I think this could be a default constructor, too.
-
- KVRAF
- Topic Starter
- 7402 posts since 17 Feb, 2005
It generates unnecessary instructions/indirection.Squidsneeze wrote:Hi, why don't you just put the duplicate code into a function and call it? I think this could be a default constructor, too.
-
- KVRist
- 50 posts since 3 Nov, 2015 from Germany
You could use the "__forceinline" keyword, then it should (don't know if it's a MSVS only keword). Else a bit unconventional, but you could put the code into a file which gets included at the needed positions.
-
- KVRian
- 1000 posts since 1 Dec, 2004
Yes, __forceinline is MSVC only but GCC has the equivalent inline __attribute__((always_inline)) so it's a matter of setting up a #define. Actually I'm not sure why they haven't harmonized those between MSVC and GCC/LLVM, like they did with other stuff like #pragma pack.Squidsneeze wrote:You could use the "__forceinline" keyword, then it should (don't know if it's a MSVS only keword). Else a bit unconventional, but you could put the code into a file which gets included at the needed positions.
-
- KVRAF
- 2256 posts since 29 May, 2012
Not the worst gcc/msvc mismatch. How about this one : http://stackoverflow.com/questions/1537 ... ute-packed Was that really harmonized and I have missed it?Actually I'm not sure why they haven't harmonized those between MSVC and GCC/LLVM, like they did with other stuff like #pragma pack.
~stratum~
-
- KVRAF
- Topic Starter
- 7402 posts since 17 Feb, 2005
I guess what I am really looking for is a way to modify source code by some pre-defined pattern. Like adding new parameters for instance, instead of moping through the source copying/pasting and editing, just be able to define everything required in one area and have it automatically propagated via some scripted pattern. It's 2017, people are even using voice to code
- KVRAF
- 12555 posts since 7 Dec, 2004
A function is the proper solution. In combination with template meta-programming.
A template functor can be used to ensure inlining along with some compiler-specific defines. Generally these won't be needed though.
The biggest issue is that you're using constructors where indirection or function-call overhead could actually make up any significant issue! This is a very, very, very, very, very, very bad sign. I can't type very enough times to express how very bad it is.
If you're writing optimized code that executes in tight loops you shouldn't be anywhere near using the heap or calling constructors.
In every other case however function-call overhead is entirely insignificant.
A template functor can be used to ensure inlining along with some compiler-specific defines. Generally these won't be needed though.
The biggest issue is that you're using constructors where indirection or function-call overhead could actually make up any significant issue! This is a very, very, very, very, very, very bad sign. I can't type very enough times to express how very bad it is.
If you're writing optimized code that executes in tight loops you shouldn't be anywhere near using the heap or calling constructors.
In every other case however function-call overhead is entirely insignificant.
Free plug-ins for Windows, MacOS and Linux. Xhip Synthesizer v8.0 and Xhip Effects Bundle v6.7.
The coder's credo: We believe our work is neither clever nor difficult; it is done because we thought it would be easy.
Work less; get more done.
The coder's credo: We believe our work is neither clever nor difficult; it is done because we thought it would be easy.
Work less; get more done.
-
- KVRian
- 1379 posts since 26 Apr, 2004 from UK
Forced inline through a keyword is a bad idea. The compiler has heuritics for inlining, and you should trust them.
If your initialization is more than a few variables, even if your function is not inlined, it won't matter much. A few registers on the stack, one additional unconditional predictable jump? That's nothing compared to even one branch test.
If your initialization is more than a few variables, even if your function is not inlined, it won't matter much. A few registers on the stack, one additional unconditional predictable jump? That's nothing compared to even one branch test.