Resolved: What is the better performance?
What is the better performance in terms of coding in this case?
We can use the solution way that you have used in this video and we can use the product expression into the print function too? If we use the product expression inside the print function we don't need the 3rd variable name "product" but we are doing the product inside the print function and that way in terms of computational efforts and time might not be the better way. I don't really know what is better or if that the same for both.
Thank you for your question!
When writing a larger piece of code, there are a couple of things that you should try to incorporate - reusability of the code, readability, and efficiency. If you need an expression more than once (in this case
num_1*num_2), then it is better to assign it to a new variable. In this way, you avoid computing the expression several times, making your code more efficient. This, in turn, allows you to reuse this variable. Moreover, your code becomes more readable, as you clearly state that this is the product between the two numbers. On the contrary, you don't need to make your code more lengthy by declaring a variable that you are going to use just once. You should find the balance between compactness and readability. So, there is no correct approach - it depends on the code :)
Hope this helps!