Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

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.

Review Suggested Edit

You can't approve or reject suggested edits because you haven't yet earned the Edit Posts ability.

Approved.
This suggested edit was approved and applied to the post about 1 month ago by wagimek‭.

19 / 255
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.

Suggested about 1 month ago by Michael‭