Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
A class might calculate static variables during instantiation, which are then used by various functions. A common technique to optimize function performance is to precalculate expensive variables u...
Question
c++11
#2: Post edited
Best Practices for Precalculating Expensive Variables in Functions
- A class might calculate static variables during instantiation, which are then used by various functions. A common technique to optimize function performance is to precalculate expensive variables used by those functions.
- However, this approach can lead to several issues:
-It splits the function code across different places, making it harder to maintain.-It can cause the section where the variables are calculated to become crowded with seemingly unrelated code.-After editing one function, it is essential to review the entire class to determine if additional code needs to be adjusted. Related/dependent code isn't kept together.- What are the best practices for keeping the variables and functions together without sacrificing performance? I mean, if somebody edits a function or the variable calculation, they should be able to rapidly know which other code depends on it, so it can be fixed. That's easy if the precalculation code is together with the function, but the constructor of a class tends to initialize all the memoized variables, so it has a lot of orphan code, which actually belongs to the code of diverse functions written somewhere else.
I considered creating a class for each function, (such as PrecalculatingInitializer), and registering these classes in a common place. This way, an initializer function could call all registered PrecalculatingInitializer classes to memoize the necessary variables. However, this approach seems overly complicated and cumbersome. I don't even know if compilers support it.- I'm concerned about maintainability and performance.
- A class might calculate static variables during instantiation, which are then used by various functions. A common technique to optimize function performance is to precalculate expensive variables used by those functions.
- However, this approach can lead to several issues:
- - It splits the function code across different places, making it harder to maintain.
- - It can cause the section where the variables are calculated to become crowded with seemingly unrelated code.
- - After editing one function, it is essential to review the entire class to determine if additional code needs to be adjusted. Related/dependent code isn't kept together.
- What are the best practices for keeping the variables and functions together without sacrificing performance? I mean, if somebody edits a function or the variable calculation, they should be able to rapidly know which other code depends on it, so it can be fixed. That's easy if the precalculation code is together with the function, but the constructor of a class tends to initialize all the memoized variables, so it has a lot of orphan code, which actually belongs to the code of diverse functions written somewhere else.
- I considered creating a class for each function, (such as `PrecalculatingInitializer`), and registering these classes in a common place. This way, an initializer function could call all registered `PrecalculatingInitializer` classes to memoize the necessary variables. However, this approach seems overly complicated and cumbersome. I don't even know if compilers support it.
- I'm concerned about maintainability and performance.
#1: Initial revision
Best Practices for Precalculating Expensive Variables in Functions
A class might calculate static variables during instantiation, which are then used by various functions. A common technique to optimize function performance is to precalculate expensive variables used by those functions. However, this approach can lead to several issues: -It splits the function code across different places, making it harder to maintain. -It can cause the section where the variables are calculated to become crowded with seemingly unrelated code. -After editing one function, it is essential to review the entire class to determine if additional code needs to be adjusted. Related/dependent code isn't kept together. What are the best practices for keeping the variables and functions together without sacrificing performance? I mean, if somebody edits a function or the variable calculation, they should be able to rapidly know which other code depends on it, so it can be fixed. That's easy if the precalculation code is together with the function, but the constructor of a class tends to initialize all the memoized variables, so it has a lot of orphan code, which actually belongs to the code of diverse functions written somewhere else. I considered creating a class for each function, (such as PrecalculatingInitializer), and registering these classes in a common place. This way, an initializer function could call all registered PrecalculatingInitializer classes to memoize the necessary variables. However, this approach seems overly complicated and cumbersome. I don't even know if compilers support it. I'm concerned about maintainability and performance.