views:

596

answers:

6

I have a variable in code that can have file path or url as value. Examples:

http://someDomain/someFile.dat
file://c:\files\someFile.dat
c:\files\someFile.dat

So there are two ways to represent a file and I can't ignore any of them. What is the correct name for such a variable: path, url, location?

I'm using a 3rd party api so I can't change semantics or separate to more variables.

A: 

if the path you are using includes the protocol "file://" then it is in fact a url.

jconlin
+4  A: 

The first two are URLs, the third is a file path. Of course, the file:/// protocol is only referring to a file also.

When using the Uri class, you can use the IsFile and the LocalPath properties to handle file:/// Uris, and in that case you should also name it like that.

Lucero
+2  A: 

Personally, I'd call the variable in question "fileName"

Stephen Wrighton
+1  A: 

Pick one that you'll be using internally to start with. If you need to support URLs, use URLs internally everywhere, and have any method that can set the variable check if it got a file path, and coerce it to an URL immediately.

Sii
+2  A: 

in fact a formal URL will be file:///c|/files/someFile.dat

urls always starts with protocol:// and then path + names, with '/' as seperator. evil windows IE sometimes use '\' to replace '/', but the formal usage is '/'.

Francis
A: 

If the values are not opaque to your application you may find it better to model them as a class. Otherwise, whenever you are going to act upon the values you may find yourself writing code like this:

if (variable.StartsWith("http://") || variable.StartsWith("file://")) {
  // Handle url
}
else {
  // Handle file path
}

You may fold some of the functionality regarding treatment of the values into your class, but it is properly better to treat it as an immutable value type.

Use a descriptive name for your class like FileLocation or whatever fits your nomenclature. It will then be very natural to declare FileLocation variables named fileLocation or inputFileLocation or even fl if you are sloppy.

Martin Liversage