Apologies if this ends up being a stupid question, but I was just wondering why this is done on step 9 of the Notepad Exercise 1 tutorial (http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html) ...
public static final int INSERT_ID = Menu.FIRST;
... and then INSERT_ID
is used everywhere, instead of just using Menu.FIRST
directly instead everywhere?
I was thinking that I don't want to create any new variables if I don't have to, especially when they are static final already.
Is it expensive to create INSERT_ID
?
Because if you used Menu.First everywhere then decided to move that option in the menu so that it was no longer the first item you would need to update all the references. This way you only need to update it in one place and the more sensibly named INSERT_ID will reflect the changes that you have made everywhere else.
As for the performance hit of creating new variables: Yes, creating the variable will use up a (insignificant) amount of CPU time, and yes storing the variable will use up an (insignificant) amount of memory, but you should never put performance before code readablity until you have determined that you have hit a bottleneck - you'd be in a very restricted environment for this to be anything near a problem.
Finally, a lot of compilers will inline the references to INSERT_ID anyway. This means that all references to INSERT_ID will be replaced at compile time with the value in Menu.First and the variable will never actually be created. I don't know enough about the Android compiler to say for sure one way or the other, but I would be surprised if it didn't do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With