tags:

views:

216

answers:

1
int minMagIndex = prefs.getInt(Preferences.PREF_MIN_MAG, 0);
if (minMagIndex < 0)
minMagIndex = 0;
int freqIndex = prefs.getInt(Preferences.PREF_UPDATE_FREQ, 0);
if (freqIndex < 0)
freqIndex = 0;

What is the second parameter in prefs.getInt ?

why is this if condition check done ?

+3  A: 

The second parameter is the default fallback, if not preference "PREF_MIN_MAG" can be found. I.e.: "assign minMaxIndex the value of preferences 'PREF_MIN_MAG', if that exists. If it doesn't, use 0".

After that, there's a check to see if the value that was found in preferences was less than 0. If it is, it's being reset to 0.

David Hedlund
@david thanks for helpbut, my doubt is, why this condition check, that is , what is the possibility of a negative value there.
Sumit M Asok
well, it'll only ever be negative if somebody has saved it with a negative value. you haven't shown us any of the code where this value is being saved so it's impossible to tell what these values are being used for, and what they can possibly be saved as. if it is absolutely clear that they can never be saved with a negative value, then this condition is redundant. perhaps it is legacy code: perhaps at some earlier version of the app, it could have a negative value, and now, if a user upgrades to the new version, and fires up their app, this condition makes sure it doesn't crash.
David Hedlund
@David, i was trying out code from "Professional Android Application Development - WROX" (pg no: 173)It is the part were we learn Preferences - Chapter-6. Any way your answer made my doubt clear.
Sumit M Asok
Hi Sumit. The check exists as a defensive code technique. A negative magnitude or frequency value makes no sense, so I defensively check to make sure that an invalid preference value didn't somehow get saved.
Reto Meier
Hi Reto Meier, *** Thanks, that's a real happy and proud moment to get a response from the author of the book. *** Your teaching skill is superb.
Sumit M Asok