DEV Community

Discussion on: Gain flexibility with generic shared preferences for Android

Collapse
 
devit951 profile image
Ildarov

I have some questions:
1) What do you expect from the method public void set(String key, T value) if I invoke with this argument set("someKey", new Object()), yes then nothing happens, but it is the wrong approach.
If I create this method, I would throw an IllegalArgumentException("This " + value + " doesn't exist, please specify it.") in the last else statement.

2)The same question to the method public T get(String key, T defaultValue). And in this method would be better instead of return null, there should be throw IllegalStateException("The "+ defaultValue + " with key " + key + " doesn't exist"

Collapse
 
alialp profile image
Ali Alp

First of all you are absolutely right :)
The reason that I haven't included the Exception handling was to transfer the idea of using the generic in shared preferences not the whole implementation but thanks to your comment I have included the Exception handling as well and now it looks like much more complete :)