tags:

views:

47

answers:

2

I usually place my keys on an xml and access them with R.string.key_name but someone make me notice that I could have inline strings in the code.

I feel that I might use that key in different places and if I change its name I would just rename in the xml but perhaps that doesn't make too much sense with keys.

What do you think?

A: 

Inline literal strings will be a massive pain to change if they get scattered through the code. Localizing them in one place with either the strings.xml or a defining a public final static variable will probably save you a headache later.

jqpubliq
+1  A: 

Your question conflates two different questions:

  1. Does it make sense to have a single definition of your key?
  2. Does it make sense for this single definition to be within an XML file?

The answer to point 1 is clearly "yes". Duplicating strings used as keys (which need to be the same everywhere for your code to function correctly) is a recipe for pain and heartache.

But what benefit does putting the key in an XML file give you? You're just adding "noise" to your code, and ensuring that whoever reads it has to find, understand and look in at least one additional file.

public static final is the way to go.

Paul Butcher