English 中文(简体)
Is it bad practice to use string literals in xpath expressions?
原标题:

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?

最佳回答

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 :-)

问题回答

My general rule of thumb is to NEVER use string literals. I think there are many benefits to not using string literals, a couple of which were already mentioned by Benjamin Cox.

  1. Centralization: If all your string literals are replaced by static string constants (ideally all grouped together in one place, perhaps at the file level), then you know exactly where to find all your string references. Plus some compilers offer convenient functions like "Jump to Definition" to access them faster.

  2. Maintainability: As a result of 1, it s easy to see that, should the need arise, you can change string references in one place and have those changed immediately reflected throughout your code.

  3. Efficiency: Memory is only allocated once for each unique string reference you create, so you aren t constantly allocating new strings into memory like you do when you use the same string literal all over the place. (Some languages, like Ruby, can automatically handle this case by encouraging the use of symbols instead of strings.)

  4. Readability: Especially if your string literals are a little cryptic, it s easier to read an appropriately named static variable: kCapturedImageDirectoryFormat as opposed to "capture-%.0f.tmp"





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签