Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Notepad Exercise 1 tutorial and Menu.FIRST

Tags:

android

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?

like image 843
Adrian Romanelli Avatar asked Sep 01 '10 07:09

Adrian Romanelli


1 Answers

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.

like image 183
Martin Harris Avatar answered Nov 20 '22 10:11

Martin Harris