tags:

views:

44

answers:

1

I currently use string literals in my xpath expressions. For example:

paragraphList = root.SelectNodes("paragraph");

I know you should generally avoid using literal values, but I figure this is safe, since the names of your xml nodes will rarely change. Is this bad practice, or am I same?

+1  A: 

Well, the principle behind the concept of avoiding literals is maintainability. So ask yourself what the maintainability ramifications of each decision are.

In this case, changing from

paragraphList = root.SelectNodes("paragraph");

to

paragraphList = root.SelectNodes(PARAGRAPH_TAG);

doesn't seem to introduce a readability issue. So the question comes down to laziness and how many places you use each string literal.

What I'd probably do (assuming it's only you touching the code) is go ahead and use string literals initially, but as soon as you have a second usage of any given string turn it into a constant.

Then again, if you are using the ReSharper plugin to Visual Studio, you can use the "Introduce field" refactoring with a simple key combination, so the laziness doesn't weigh quite so much :-)

Benjamin Cox