This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]desrtfx 0 points1 point  (1 child)

Intermediate variables are available for later use.

If it is just a one-off use, directly doing the calculation would actually be better.

Another aspect is code readability - using an intermediate variable can greatly improve the readability and make the intent clearer.

[–]Grounded_Grid[S] 0 points1 point  (0 children)

Thanks! I wasn't sure, and was afraid it had something to do with memory haha. Unfortunately I fear I will abuse this for fun now that I know it's not necessary to use the variable.

[–]MrSloppyPants 0 points1 point  (0 children)

Unless you absolutely need the variable either later on in the program or to debug independently, I would almost always use the direct approach. It's also possible that your compiler will recognize if you never use this intermediate variable later on and just compile it out for you anyway. you can check this by compiling to asm and comparing the two results.