Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 985869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:13:53+00:00 2026-05-16T05:13:53+00:00

I would like to know if there are any significant disadvantages to creating a

  • 0

I would like to know if there are any significant disadvantages to creating a temporary variable for a value that is accessed multiple times within a small scope.

eg:

filep->waypt[rp->leg[j]].ID

or

(*(filep->route + filep->nroutes - 1))->number

These are examples from a recent project where I forced myself to avoid almost any simplification for variable references in order to improve my skills with C pointers (it was painful). However, the habit seemed to stick. I find that the more I try to introduce variables to simplify my code (readability and typing volume) the more confusing it becomes to remember exactly what each new variable references.

I’ve only ever had my code reviewed in an educational setting, and I would like to know what others find easier to digest and where are the performance tradeoffs (if any).

When would assigning a value who’s memory address would require several arithmetic operations to calculate it’s own variable start to cause performance issues? Would it make a difference if done once in a loop? What about once in a nested loop? What about a value assigned to it’s own variable in a loop, but is then accessed in an inner loop?

How would this change in an interpreted language?
Say in PHP (please excuse syntactical errors, I’m new to PHP):

$employees[$i][$phone]['Home']['number'];

vs

$home = $employees[$i][$phone]['Home']['number'];

And finally, if it’s not too subjective (code readability shouldn’t be!), which is considered the best practice for writing readable code? I write code that is as self-documenting as possible, but if variables begin to get too contrived and are referenced multiple times, I will assign them their own variable. However, the reason this works for me could be that I’ve gotten used to my own style.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:13:54+00:00Added an answer on May 16, 2026 at 5:13 am

    There’s a fairly good chance that any decent compiler will detect common sub-expressions and optimise them for you.

    However, I would still opt for a temporary variable if it improved the readability of my code, despite the fact that it introduced extra storage (though minimal).

    I consider maintainability of code far more important than a tiny storage hit. By way of (contrived) example, I would probably replace a whole lot of:

    item[last_item-1].id = 14860;
    item[last_item-1].name.first = "pax";
    item[last_item-1].name.last = "diablo";
    

    calls with:

    tItem lastItem = &(item[last_item-1]);
    lastItem->id = 14860;
    : : :
    // and so on.
    

    As to interpreted languages, I have less information on that. I know quite a bit about how to optimise assembly code (though nowhere near as much as the demigods that write compilers, which is why I normally leave it to them). I know somewhat less about the virtual machines running inside most interpreters.

    But I would still code for readability. I don’t want to see a bucketload of $employees[$i][$phone]['Home']['number'] snippets stuck in my code. I would rather add

    $homePhone = $employees[$i][$phone]['Home']['number'];
    

    in there somewhere and just use $homePhone from that point onward.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if there's any way to detect that a pinch
I would like to know is there any difference in performance between these two
I would like to know if there any shorter expression exists, than this: '0000-00-00
I'm new to PowerBuilder 12, and would like to know is there any way
I would like to know if there exists any kind of library or workaround
I would like to know if there is any kind of tool to monitor
I would like to know if there are any good resources (online or books)
I would like to know if there is any way to find the actual
I would like to know if there are any xml coding standards. <?xml version=1.0?>
I would like to know if there are any templates for doing this in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.